netty启动




public abstract class AbstractServerBootstrap implements ApplicationListener<ContextRefreshedEvent>, DisposableBean {
	
	public static EventLoopGroup workerGroup;
	
	private static Logger logger = LoggerFactory.getLogger(AbstractServerBootstrap.class);
	
	private static final String ZK_PREFIX = TopsConfReader.getConfContent("properties/crawler-autoissue.properties", "zk.prefix", TopsConfEnum.ConfScope.R);

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if (isRootApplicationContext(event.getApplicationContext())){
			new Thread(){
				public void run(){
					// Configure the server.
					EventLoopGroup bossGroup = new NioEventLoopGroup(1);
					workerGroup = new NioEventLoopGroup();
					try {
						ServerBootstrap b = new ServerBootstrap();
						b.option(ChannelOption.SO_BACKLOG, 1024);
						b.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE);
						b.option(ChannelOption.TCP_NODELAY, Boolean.TRUE);
						b.group(bossGroup, workerGroup)
						.channel(NioServerSocketChannel.class)
						.handler(new LoggingHandler(LogLevel.INFO))
						.childHandler(channelInitializer());
						Channel ch = b.bind(getBoundIP(), getListeningPort()).sync().channel();
						logger.info("启动netty server完成");
						registerRPC();
						ch.closeFuture().sync();
					} catch(Exception e){
						logger.error("启动netty server异常", e);
						throw new RuntimeException(e);
					}finally {
						bossGroup.shutdownGracefully();
						workerGroup.shutdownGracefully();
					}
				}

			}.start();
		}
	}
	
	/**
	 * 注册netty rpc
	 * @throws Exception
	 */
	private void registerRPC() throws Exception{
		String rpcAddress = getBoundIP() + ":" + getListeningPort();
		String shardId = "0";
		String replicaId = RandomStringUtils.randomAlphanumeric(10);
		TopsZookeeperBalancer.registerRpc(rpcAddress, ZK_PREFIX, getIssueSite().name(), shardId, replicaId);
	}
	
	/**
	 * 获取netty server 绑定的ip
	 * @return
	 */
	private String getBoundIP(){
		return TopsAppRegistry.getLocalIP();
	}
	
	private boolean isRootApplicationContext(ApplicationContext context) {
		return context.getParent() == null;
	}

	@Override
	public void destroy() throws Exception {

	}
	
	protected ChannelInitializer<SocketChannel> channelInitializer(){
		return new ChannelInitializer<SocketChannel>(){
			@Override
			public void initChannel(SocketChannel ch) {
				channelPipeline(ch);
			}
		};
	}
	
	protected ChannelPipeline channelPipeline(Channel ch){
		ChannelPipeline p = ch.pipeline();
		p.addLast("httpCodec", new HttpServerCodec());
		p.addLast("aggegator", new HttpObjectAggregator(64 * 1024 * 1024));
		p.addLast("protocol", new NettyMethodInvokerServerCodec());
		return p;
	}
	
	protected int getListeningPort(){
		return 5566;
	}
	
	/**
	 * 获取比价站点名称
	 * @return
	 */
	protected abstract IssueSite getIssueSite();

}

首先,我们可以知道,启动一个ApplicationListener, 通过监视ContextRefreshedEvent,只要发现Spring有文件内容更新,这启动.其中isRootApplicationContext,通过设置是不是第一次刷新.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值