Netty 处理Tcp Http Udp

2 篇文章 0 订阅
1 篇文章 0 订阅

说明

netty5 监听 tcp端口,http端口,udp端口 分别处理消息

1.启动类方法

启动netty 监听端口事件

 public void init(int httpport,int tcpport,int udpport) throws Exception {
 EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
        try{
        //http和tcp设置
            ServerBootstrap bootstrap=new ServerBootstrap();
            bootstrap.group(bossGroup,workerGroup);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.option(ChannelOption.SO_BACKLOG,1024);
        //UDP 设置
            Bootstrap Bootstrap2 = new Bootstrap();//udp不能使用ServerBootstrap
            Bootstrap2.group(workerGroup);
            Bootstrap2 .channel(NioDatagramChannel.class);//设置UDP通道
            Bootstrap2 .option(ChannelOption.SO_BROADCAST, true);// 支持广播
            Bootstrap2 .option(ChannelOption.SO_BACKLOG, 128);
            Bootstrap2 .option(ChannelOption.SO_RCVBUF, 1024 * 1024);// 设置UDP读缓冲区为1M
            Bootstrap2 .option(ChannelOption.SO_SNDBUF, 1024 * 1024);// 设置UDP写缓冲区为1M

            List
   
   
    
     ports = Arrays.asList(httpport,tcpport,udpport);
            Collection
    
    
     
      channels = new ArrayList
     
     
      
      (ports.size());
            for (int pot : ports) {
                Channel serverChannel=null;
                if (pot==udpport){
                    Bootstrap2 .handler(udpServerInitializer);//初始化处理器
                    serverChannel = Bootstrap2.bind(pot).sync().channel();
                }else if (pot==httpport){
                    bootstrap.childHandler(httpServerInitializer);
                    serverChannel = bootstrap.bind(pot).sync().channel();
                }else if (pot==tcpport){
                    bootstrap.childHandler(tcpServerInitializer);
                   serverChannel = bootstrap.bind(pot).sync().channel();
                }
                channels.add(serverChannel);
            }
            for (Channel ch : channels) {
                ch.closeFuture().sync();
            }
      //  future.channel().closeFuture().sync();
    }catch(Exception e){
        System.out.println("Server服务端报异常---------"+e.getMessage());
    }finally {
           bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
}
     
     
    
    
   
   

2.http处理方法格式化化协议处理

 
   @Autowired
    private HttpServerHandler httpServerHandler;
  @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        /* 处理http处理 */
        if (socketChannel.localAddress().getPort()== ConfigProperties.getInt("http.port")){
            socketChannel.pipeline().addLast(new HttpServerCodec());/*HTTP 服务的解码器*/
            socketChannel.pipeline().addLast(new HttpObjectAggregator(2048));/*HTTP 消息的合并处理*/
            socketChannel.pipeline().addLast(httpServerHandler);
            //处理tcp请求
        }else{
            log.info("请求端口出错:"+socketChannel.localAddress().getPort());
           
        }
    }

3.tcp处理方法格式化协议处理

  @Autowired
    private TcpServerHandler tcpServerHandler;
    @Autowired
    private TcpHeartBeatServerHandler tcpHeartBeatServerHandler;
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        /* 处理tcp处理 */
         if (socketChannel.localAddress().getPort()== ConfigProperties.getInt("tcp.port")){
            socketChannel.pipeline().addLast(new ByteArrayDecoder());
            socketChannel.pipeline().addLast(new ByteArrayEncoder());
            socketChannel.pipeline().addLast(tcpServerHandler);
            //心跳包
            socketChannel.pipeline().addLast(new IdleStateHandler(300,0,0, TimeUnit.SECONDS));
            socketChannel.pipeline().addLast("hearbeata",tcpHeartBeatServerHandler); //心跳事件
        }else{
        }
       
    }

4.udp 处理方法 格式化协议处理

    @Autowired
    private  UdpChatServerHandler udpChatServerHandler;
    @Autowired
    private  UdpHeartBeatServerHandler udpHeartBeatServerHandler;

    @Override
    protected void initChannel(NioDatagramChannel nioDatagramChannel) throws Exception {
        ChannelPipeline pipeline = nioDatagramChannel.pipeline();
        pipeline.addLast("handler",udpChatServerHandler);//消息处理器
       // pipeline.addLast("ackHandler", new UdpAckServerHandler());//ack处理器

        pipeline.addLast("timeout", new IdleStateHandler(180, 0, 0, TimeUnit.SECONDS));// //此两项为添加心跳机制,60秒查看一次在线的客户端channel是否空闲
        pipeline.addLast("hearbeat",udpHeartBeatServerHandler);// 心跳处理handler
    }

5.注意

框架采用netty5集成spring,mybatis

在协议处理中需要写自己处理消息的方法处理等业务

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值