亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法

 更新時間:2023年10月17日 10:51:28   作者:帥氣Dee海綿寶寶  
Netty提供異步的、基于事件驅動的網(wǎng)絡應用程序框架,用以快速開發(fā)高性能、高可靠性的網(wǎng)絡 IO 程序,是目前最流行的 NIO 框架,這篇文章主要介紹了SpringBoot 整和 Netty 并監(jiān)聽多端口,需要的朋友可以參考下

SpringBoot 整合 Netty 并監(jiān)聽多端口

Netty 是由 JBOSS 提供的一個 Java 開源框架。Netty 提供異步的、基于事件驅動的網(wǎng)絡應用程序框架,用以快速開發(fā)高性能、高可靠性的網(wǎng)絡 IO 程序,是目前最流行的 NIO 框架,Netty 在互聯(lián)網(wǎng)領域、大數(shù)據(jù)分布式計算領域、游戲行業(yè)、通信行業(yè)等獲得了廣泛的應用,知名的 Elasticsearch 、Dubbo 框架內部都采用了 Netty。

1.依賴

<dependency>
     <groupId>io.netty</groupId>
     <artifactId>netty-all</artifactId>
     <version>4.1.28.Final</version>
</dependency>

2.PortDefinition

讀取yml 配置中的多端口配置

//netty:
//  port: {8300: A, 8500: B}
@Data
@Configuration
@ConfigurationProperties(prefix = "netty")
public class PortDefinition {

    Map<Integer, String> port;
}

3.GatewayType

多端口判斷使用的常量類

public class GatewayType {
    //個人設備
    public final static String GERNESHEBEI_SHOUHUA="A";
}

4.NettyServer

實現(xiàn)Netty服務端

@Slf4j
@RefreshScope
@Component
public class NettyServer {
    @Autowired
    private PortDefinition portDefinition;
    public void start() throws InterruptedException {
        /**
         * 創(chuàng)建兩個線程組 bossGroup 和 workerGroup
         * bossGroup 只是處理連接請求,真正的和客戶端業(yè)務處理,會交給 workerGroup 完成
         *  兩個都是無線循環(huán)
         */
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            //創(chuàng)建服務器端的啟動對象,配置參數(shù)
            ServerBootstrap bootstrap = new ServerBootstrap();
            //設置兩個線程組
            bootstrap.group(bossGroup, workerGroup)
                    //使用NioServerSocketChannel 作為服務器的通道實現(xiàn)
                    .channel(NioServerSocketChannel.class)
                    //設置線程隊列得到連接個數(shù)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    //設置保持活動連接狀態(tài)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    //通過NoDelay禁用Nagle,使消息立即發(fā)出去,不用等待到一定的數(shù)據(jù)量才發(fā)出去
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    //可以給 bossGroup 加個日志處理器
                    .handler(new LoggingHandler(LogLevel.INFO))
					//監(jiān)聽多個端口
                    .childHandler(new SocketChannelInitHandler(portDefinition.getPort()))
            ;
//            監(jiān)聽多個端口
            Map<Integer, String> ports = portDefinition.getPort();
            log.info("netty服務器在{}端口啟動監(jiān)聽", JSONObject.toJSONString(ports));
            for (Map.Entry<Integer, String> p : ports.entrySet()) {
                final int port = p.getKey();
                // 綁定端口
                ChannelFuture cf = bootstrap.bind(new InetSocketAddress(port)).sync();
                if (cf.isSuccess()) {
                    log.info("netty 啟動成功,端口:{}", port);
                } else {
                    log.info("netty 啟動失敗,端口:{}", port);
                }
                //對關閉通道進行監(jiān)聽
                cf.channel().closeFuture().sync();
            }
        } finally {
            //發(fā)送異常關閉
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

5.SocketChannelInitHandler

根據(jù)多端口去判斷去執(zhí)行那個業(yè)務的 Handler 方法

@Slf4j
public class SocketChannelInitHandler extends ChannelInitializer<SocketChannel> {
    /**
     * 用來存儲每個連接上來的設備
     */
    public static final Map<ChannelId, ChannelPipeline> CHANNEL_MAP = new ConcurrentHashMap<>();
    /**
     * 端口信息,用來區(qū)分這個端口屬于哪種類型的連接 如:8300 屬于 A
     */
    Map<Integer, String> ports;
    public SocketChannelInitHandler(Map<Integer, String> ports) {
        this.ports = ports;
    }
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //每次連接上來 對通道進行保存
        CHANNEL_MAP.put(socketChannel.id(), socketChannel.pipeline());
        ChannelPipeline pipeline = socketChannel.pipeline();
        int port = socketChannel.localAddress().getPort();
        String type = ports.get(port);
        pipeline.addLast(new StringEncoder(StandardCharsets.UTF_8));
        pipeline.addLast(new StringDecoder(StandardCharsets.UTF_8));
        pipeline.addLast(new LengthFieldBasedFrameDecoder(24*1024,0,2));
        log.info("【initChannel】端口號: "+port+"  類型: "+type);
        //不同類型連接,處理鏈中加入不同處理協(xié)議
        switch (type) {
            case GatewayType.GERNESHEBEI_SHOUHUA:
                //手環(huán)
                pipeline.addLast(new NettyServerHandler());
                break;
            default:
                log.error("當前網(wǎng)關類型并不存在于配置文件中,無法初始化通道");
                break;
        }
    }
}

6.NettyServerHandler

業(yè)務員的Handler 方法

@Slf4j
public class NettyServerHandler extends SimpleChannelInboundHandler<Object> {
    //private static final Logger log = LoggerFactory.getLogger(NettyServerHandler.class);
    protected void channelRead0(ChannelHandlerContext context, Object obj) throws Exception {
        log.info(">>>>>>>>>>>服務端接收到客戶端的消息:{}",obj);
        String message = (String)obj;
		//之后寫自己的業(yè)務邏輯即可
        String b=message.replace("[", "")
                .replace("]","");
        String[] split1 = b.split("\\*");
        String key = split1[1];
        SocketChannel socketChannel = (SocketChannel) context.channel();
		//調用業(yè)務代碼類并執(zhí)行
        WristWatchSocket.runSocket(key,message,socketChannel,true);
        ReferenceCountUtil.release(obj);
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}

7.WristWatchSocket 業(yè)務代碼調用,根據(jù)自己的業(yè)務需要

@Slf4j
//@Data
public class WristWatchSocket{
    //業(yè)務代碼
    public static void runSocket(String key, String message, SocketChannel socketChannel, Boolean isNotFirst){
        try {
            if(message==null||message.trim().equals("")){
                return;
            }
            String restr="測試發(fā)送";
            if(!"".equals(restr)){
                socketChannel.writeAndFlush(restr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

8.配置文件配置多端口

netty:
  port: {8300: A, 8500: B}

9.配置Netty 啟動

在啟動類中配置

@EnableAsync
@EnableSwagger2
@EnableFeignClients
@EnableTransactionManagement
@Slf4j
@MapperScan({"com.yuandian.platform.mapper"})
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class YunYiplatformApplication {
    public static void main(String[] args) {
        ApplicationContext run = SpringApplication.run(YunYiplatformApplication.class, args);
        log.info("\n\n【【【【平臺成功啟動!】】】】\n");
        //netty 啟動配置
        try {
            run.getBean(NettyServer.class).start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

截圖

到此這篇關于SpringBoot 整合 Netty 并監(jiān)聽多端口的文章就介紹到這了,更多相關SpringBoot 整合 Netty 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Springboot如何根據(jù)實體類生成數(shù)據(jù)庫表

    Springboot如何根據(jù)實體類生成數(shù)據(jù)庫表

    這篇文章主要介紹了Springboot如何根據(jù)實體類生成數(shù)據(jù)庫表的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 淺談一段java代碼是如何執(zhí)行的

    淺談一段java代碼是如何執(zhí)行的

    這篇文章主要介紹了淺談一段java代碼是如何執(zhí)行的,小編覺得不錯,下面就一起來了解一下
    2021-04-04
  • java去除已排序數(shù)組中的重復元素

    java去除已排序數(shù)組中的重復元素

    這篇文章主要為大家詳細介紹了java去除已排序數(shù)組中重復元素的方法,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Java超詳細透徹講解接口

    Java超詳細透徹講解接口

    接口是Java中最重要的概念之一,它可以被理解為一種特殊的類,不同的是接口的成員沒有執(zhí)行體,是由全局常量和公共的抽象方法所組成,本文給大家介紹Java接口,感興趣的朋友一起看看吧
    2022-05-05
  • 微信js sdk invalid signature簽名錯誤問題的解決方法分析

    微信js sdk invalid signature簽名錯誤問題的解決方法分析

    這篇文章主要介紹了微信js sdk invalid signature簽名錯誤問題的解決方法,結合實例形式分析了微信簽名錯誤問題相關解決方法,需要的朋友可以參考下
    2019-04-04
  • Java中IO流 字節(jié)流實例詳解

    Java中IO流 字節(jié)流實例詳解

    這篇文章主要介紹了Java中IO流 字節(jié)流實例詳解的相關資料,需要的朋友可以參考下
    2017-05-05
  • mybatisplus中的xml對象參數(shù)傳遞問題

    mybatisplus中的xml對象參數(shù)傳遞問題

    這篇文章主要介紹了mybatisplus中的xml對象參數(shù)傳遞問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java實現(xiàn)考試系統(tǒng)

    Java實現(xiàn)考試系統(tǒng)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)考試系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Java Class 加密工具 ClassFinal詳解

    Java Class 加密工具 ClassFinal詳解

    ClassFinal 是一款 java class 文件安全加密工具,支持直接加密jar包或war包,無需修改任何項目代碼,兼容spring-framework;可避免源碼泄漏或字節(jié)碼被反編譯,這篇文章主要介紹了Java Class 加密工具 ClassFinal,需要的朋友可以參考下
    2023-03-03
  • 使用mybatis進行數(shù)據(jù)插入時返回自增id的方法及注意點

    使用mybatis進行數(shù)據(jù)插入時返回自增id的方法及注意點

    這篇文章主要給大家介紹了關于使用mybatis進行數(shù)據(jù)插入時返回自增id的方法及注意點,在插入一條數(shù)據(jù)之后需要返回它的自增主鍵id,因為插入的實體類數(shù)據(jù)id為空,后面的邏輯還需要這個id,需要的朋友可以參考下
    2023-09-09

最新評論