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 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() { @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 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地址 } }