123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package cn.hhj.server;
- import cn.hhj.annotation.RpcService;
- import cn.hhj.handler.ProcessorHandler;
- import cn.hhj.regiest.IRegistryCenter;
- import cn.hhj.regiest.RegistryCenterWithZk;
- import io.netty.bootstrap.ServerBootstrap;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.ChannelPipeline;
- import io.netty.channel.EventLoopGroup;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.channel.socket.nio.NioServerSocketChannel;
- import io.netty.handler.codec.serialization.ClassResolvers;
- import io.netty.handler.codec.serialization.ObjectDecoder;
- import io.netty.handler.codec.serialization.ObjectEncoder;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.util.StringUtils;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class RpcServer implements ApplicationContextAware, InitializingBean {
- private int port;
- private Map<String,Object> handlerMap=new HashMap();
- private IRegistryCenter registryCenter=new RegistryCenterWithZk();
- public RpcServer(int port) {
- this.port = port;
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- EventLoopGroup bossGroup=new NioEventLoopGroup();
- EventLoopGroup workerGroup=new NioEventLoopGroup();
- try{
- ServerBootstrap serverBootstrap=new ServerBootstrap();
- serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
- @Override
- protected void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline pipeline = ch.pipeline();
- pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
- pipeline.addLast(new ObjectEncoder());
- pipeline.addLast(new ProcessorHandler(handlerMap));
- }
- });
- serverBootstrap.bind(port).sync();
- }catch (Exception e){
- e.printStackTrace();
- }finally { // 此处不能关闭,此服务应当持续打开
- /* workerGroup.shutdownGracefully();
- bossGroup.shutdownGracefully();*/
- }
- }
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- Map<String,Object> serviceBeanMap=applicationContext.getBeansWithAnnotation(RpcService.class);
- if (!serviceBeanMap.isEmpty()){
- for(Object serviceBean:serviceBeanMap.values()){
- RpcService rpcService=serviceBean.getClass().getAnnotation(RpcService.class);
- String serviceName=rpcService.value().getName();//拿到接口类定义
- String version=rpcService.version(); //拿到版本号
- if (!StringUtils.isEmpty(version)){
- serviceName+="-"+version;
- }
- handlerMap.put(serviceName,serviceBean);
- registryCenter.registry(serviceName,getAddress()+":"+port);
- }
- }
- }
- private static String getAddress(){
- InetAddress inetAddress=null;
- try {
- inetAddress=InetAddress.getLocalHost();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- }
- return inetAddress.getHostAddress();// 获得本机的ip地址
- }
- }
|