This commit is contained in:
crusader 2018-04-24 16:44:26 +09:00
parent ce575853d5
commit 4abdad479d
6 changed files with 100 additions and 18 deletions

View File

@ -5,7 +5,14 @@ import java.net.SocketAddress;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import com.google.gson.Gson;
import com.loafle.commons.rpc.protocol.RPCServerCodec;
import com.loafle.commons.rpc.protocol.json.JSONRPCServerCodec;
import com.loafle.commons.rpc.registry.RPCRegistry;
import com.loafle.commons.rpc.registry.pojo.POJORPCRegistry;
import com.loafle.commons.server.Server; import com.loafle.commons.server.Server;
import com.loafle.commons.server.socket.handler.codec.SocketServerProtocolHandler;
import com.loafle.overflow.container.server.handler.RPCServerHandler;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -29,9 +36,12 @@ public class ContainerConfiguration {
@Autowired @Autowired
private List<ChannelHandler> pipelineChannelHandlers; private List<ChannelHandler> pipelineChannelHandlers;
@Autowired
private Gson gson;
@Bean @Bean
public List<Class<? extends ServerChannel>> channelClasses() { public Class<? extends ServerChannel> channelClass() {
return Arrays.asList(NioServerSocketChannel.class); return NioServerSocketChannel.class;
} }
@Bean @Bean
@ -40,21 +50,37 @@ public class ContainerConfiguration {
} }
@Bean @Bean
public List<ChannelHandler> channelHandlers() { public ChannelHandler channelHandler() {
return Arrays.asList(new LoggingHandler(LogLevel.INFO)); return new LoggingHandler(LogLevel.INFO);
}
@Bean
public RPCServerCodec rpcServerCodec() {
if (null != gson) {
return new JSONRPCServerCodec(gson);
} else {
return new JSONRPCServerCodec();
}
}
@Bean
public RPCRegistry rpcRegistry() {
return new POJORPCRegistry();
} }
@Bean @Bean
public ChannelInitializer<?> channelInitializer() { public ChannelInitializer<?> channelInitializer() {
return new ChannelInitializer<SocketChannel>() { return new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline cp = ch.pipeline(); ChannelPipeline cp = ch.pipeline();
for (ChannelHandler channelHandler : pipelineChannelHandlers) { cp.addLast(new SocketServerProtocolHandler(true));
cp.addLast(channelHandler); cp.addLast(new RPCServerHandler());
} for (ChannelHandler channelHandler : pipelineChannelHandlers) {
} cp.addLast(channelHandler);
}; }
}
};
} }
@Bean @Bean

View File

@ -10,6 +10,7 @@ import com.loafle.commons.server.socket.handler.codec.TextSocketFrame;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
@ -17,6 +18,7 @@ import io.netty.channel.SimpleChannelInboundHandler;
/** /**
* RPCDecoder * RPCDecoder
*/ */
@Component
public class RPCServerHandler extends SimpleChannelInboundHandler<SocketFrame> { public class RPCServerHandler extends SimpleChannelInboundHandler<SocketFrame> {
private static final Logger logger = LoggerFactory.getLogger(RPCServerHandler.class); private static final Logger logger = LoggerFactory.getLogger(RPCServerHandler.class);
@ -25,9 +27,7 @@ public class RPCServerHandler extends SimpleChannelInboundHandler<SocketFrame> {
@Autowired @Autowired
private RPCInvoker rpcInvoker; private RPCInvoker rpcInvoker;
public RPCServerHandler(RPCServerCodec serverCodec, RPCInvoker rpcInvoker) { public RPCServerHandler() {
this.serverCodec = serverCodec;
this.rpcInvoker = rpcInvoker;
} }
@Override @Override

View File

@ -6,6 +6,7 @@ import javax.annotation.Resource;
import com.loafle.overflow.container.Container; import com.loafle.overflow.container.Container;
import com.loafle.overflow.crawler.Crawler; import com.loafle.overflow.crawler.Crawler;
import com.loafle.overflow.module.core.annotation.RPCService;
import com.loafle.overflow.module.core.exception.OverflowException; import com.loafle.overflow.module.core.exception.OverflowException;
import com.loafle.overflow.module.core.interfaces.Service; import com.loafle.overflow.module.core.interfaces.Service;
import com.loafle.overflow.module.sensorconfig.model.SensorConfig; import com.loafle.overflow.module.sensorconfig.model.SensorConfig;
@ -17,7 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
/** /**
* CrawlerService * CrawlerService
*/ */
@org.springframework.stereotype.Service("CrawlerService") @RPCService("CrawlerService")
public class CrawlerService implements Service { public class CrawlerService implements Service {
private static final Logger logger = LoggerFactory.getLogger(CrawlerService.class); private static final Logger logger = LoggerFactory.getLogger(CrawlerService.class);

View File

@ -1,12 +1,13 @@
package com.loafle.overflow.container.service; package com.loafle.overflow.container.service;
import com.loafle.overflow.module.core.annotation.RPCService;
import com.loafle.overflow.module.core.exception.OverflowException; import com.loafle.overflow.module.core.exception.OverflowException;
import com.loafle.overflow.module.core.interfaces.Service; import com.loafle.overflow.module.core.interfaces.Service;
/** /**
* ProbeService * ProbeService
*/ */
@org.springframework.stereotype.Service("ProbeService") @RPCService("ProbeService")
public class ProbeService implements Service { public class ProbeService implements Service {
@Override @Override

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.loafle.overflow.module.core.annotation.RPCService;
import com.loafle.overflow.module.core.exception.OverflowException; import com.loafle.overflow.module.core.exception.OverflowException;
import com.loafle.overflow.module.core.interfaces.Service; import com.loafle.overflow.module.core.interfaces.Service;
import com.loafle.overflow.module.sensorconfig.model.SensorConfig; import com.loafle.overflow.module.sensorconfig.model.SensorConfig;
@ -14,7 +15,7 @@ import org.slf4j.LoggerFactory;
/** /**
* SensorConfigService * SensorConfigService
*/ */
@org.springframework.stereotype.Service("SensorConfigService") @RPCService("SensorConfigService")
public class SensorConfigService implements Service { public class SensorConfigService implements Service {
private static final Logger logger = LoggerFactory.getLogger(SensorConfigService.class); private static final Logger logger = LoggerFactory.getLogger(SensorConfigService.class);

View File

@ -0,0 +1,53 @@
package com.loafle.overflow.container.service;
import java.util.Map;
import com.loafle.commons.rpc.RPCException;
import com.loafle.commons.rpc.registry.RPCRegistry;
import com.loafle.overflow.module.core.annotation.RPCService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Service
*/
@Component
public class Service implements InitializingBean, ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(Service.class);
private ApplicationContext applicationContext;
@Autowired
private RPCRegistry rpcRegistry;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
Map<String, Object> services = this.applicationContext.getBeansWithAnnotation(RPCService.class);
if (null == services || 0 == services.size()) {
logger.debug("there is not service");
return;
}
services.forEach((name, bean) -> {
logger.debug("bean %s", bean.getClass().getName());
try {
this.rpcRegistry.registerService(bean, bean.getClass(), name);
} catch (RPCException e) {
logger.error("RPCRegistry", e);
}
});
}
}