RpcServer.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package cn.hhj.server;
  2. import cn.hhj.annotation.RpcService;
  3. import cn.hhj.handler.ProcessorHandler;
  4. import cn.hhj.regiest.IRegistryCenter;
  5. import cn.hhj.regiest.RegistryCenterWithZk;
  6. import io.netty.bootstrap.ServerBootstrap;
  7. import io.netty.channel.ChannelInitializer;
  8. import io.netty.channel.ChannelPipeline;
  9. import io.netty.channel.EventLoopGroup;
  10. import io.netty.channel.nio.NioEventLoopGroup;
  11. import io.netty.channel.socket.SocketChannel;
  12. import io.netty.channel.socket.nio.NioServerSocketChannel;
  13. import io.netty.handler.codec.serialization.ClassResolvers;
  14. import io.netty.handler.codec.serialization.ObjectDecoder;
  15. import io.netty.handler.codec.serialization.ObjectEncoder;
  16. import org.springframework.beans.BeansException;
  17. import org.springframework.beans.factory.InitializingBean;
  18. import org.springframework.context.ApplicationContext;
  19. import org.springframework.context.ApplicationContextAware;
  20. import org.springframework.util.StringUtils;
  21. import java.net.InetAddress;
  22. import java.net.UnknownHostException;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.concurrent.ExecutorService;
  26. import java.util.concurrent.Executors;
  27. public class RpcServer implements ApplicationContextAware, InitializingBean {
  28. private int port;
  29. private Map<String,Object> handlerMap=new HashMap();
  30. private IRegistryCenter registryCenter=new RegistryCenterWithZk();
  31. public RpcServer(int port) {
  32. this.port = port;
  33. }
  34. @Override
  35. public void afterPropertiesSet() throws Exception {
  36. EventLoopGroup bossGroup=new NioEventLoopGroup();
  37. EventLoopGroup workerGroup=new NioEventLoopGroup();
  38. try{
  39. ServerBootstrap serverBootstrap=new ServerBootstrap();
  40. serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
  41. @Override
  42. protected void initChannel(SocketChannel ch) throws Exception {
  43. ChannelPipeline pipeline = ch.pipeline();
  44. pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
  45. pipeline.addLast(new ObjectEncoder());
  46. pipeline.addLast(new ProcessorHandler(handlerMap));
  47. }
  48. });
  49. serverBootstrap.bind(port).sync();
  50. }catch (Exception e){
  51. e.printStackTrace();
  52. }finally { // 此处不能关闭,此服务应当持续打开
  53. /* workerGroup.shutdownGracefully();
  54. bossGroup.shutdownGracefully();*/
  55. }
  56. }
  57. @Override
  58. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  59. Map<String,Object> serviceBeanMap=applicationContext.getBeansWithAnnotation(RpcService.class);
  60. if (!serviceBeanMap.isEmpty()){
  61. for(Object serviceBean:serviceBeanMap.values()){
  62. RpcService rpcService=serviceBean.getClass().getAnnotation(RpcService.class);
  63. String serviceName=rpcService.value().getName();//拿到接口类定义
  64. String version=rpcService.version(); //拿到版本号
  65. if (!StringUtils.isEmpty(version)){
  66. serviceName+="-"+version;
  67. }
  68. handlerMap.put(serviceName,serviceBean);
  69. registryCenter.registry(serviceName,getAddress()+":"+port);
  70. }
  71. }
  72. }
  73. private static String getAddress(){
  74. InetAddress inetAddress=null;
  75. try {
  76. inetAddress=InetAddress.getLocalHost();
  77. } catch (UnknownHostException e) {
  78. e.printStackTrace();
  79. }
  80. return inetAddress.getHostAddress();// 获得本机的ip地址
  81. }
  82. }