Netty TCP 服务端探测工具

  在基于netty的应用中部分场景需要自动识别远端服务,通常是在业务通信前的握手阶段进行识别, 识别指定的服务端并执行相应的逻辑

直接上依赖:

确保使用最新版本nettyx,此处为了演示使用了如下版本
<dependency>
    <groupId>io.github.fbbzl</groupId>
    <artifactId>nettyx</artifactId>
    <version>2.3.3-RELEASE</version>
</dependency>

RemoteDetector

  Netty对此部分进行了简单的封装, 提供了ServerDetector来对远端server进行探测, 它的本质是一个单通道的tcpclient实例, 在完成探测之后, 用户可以根据自己需求选择是否close此实例
  直接上代码


 @Slf4j
public class TestServerDetector extends RemoteDetector<String> {
    protected TestServerDetector(InetSocketAddress address) {
        super(address);
    }

    @Override
    public boolean checkResponse(String response) {
        return StrUtil.equals(response, "ack");
    }

    @Override
    public void initDetectChannel(NioSocketChannel ch) {
        ch.pipeline().addLast();
    }

    @Override
    public String getDetectMessage() {
        return "this is detect string type msg";
    }

    public static void main(String[] args) throws Exception {
        TestServerDetector testDetector = new TestServerDetector(new InetSocketAddress(9888));
        Console.log(testDetector.doDetect());
    }

    // 简简单单写个main测试下
    public static void main(String[] args) throws Exception {
        TestServerDetector testDetector = new TestServerDetector(new InetSocketAddress(9888));
        // 设置重连的次数
        testDetector.setDetectRetryTimes(2);
        // 设置最大等待远端响应的毫秒数
        testDetector.setWaitResponseMillis(5000);
        Console.log(testDetector.doDetect());
    }

}

至此我们完成一个简单的探测器

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot中使用Netty连接多个TCP服务端可以通过以下步骤实现: 1. 首先,确保你已经在Spring Boot项目中引入了Netty的依赖。可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.65.Final</version> </dependency> ``` 2. 创建一个Netty客户端的处理器类,用于处理接收到的消息。可以继承`SimpleChannelInboundHandler`类,并重写`channelRead0`方法来处理接收到的消息。 ```java public class NettyClientHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // 处理接收到的消息 System.out.println("Received message: " + msg); } } ``` 3. 创建一个Netty客户端的启动类,用于配置和启动Netty客户端。 ```java @Configuration public class NettyClientConfig { @Value("${netty.server.host}") private String serverHost; @Value("${netty.server.port}") private int serverPort; @Bean public EventLoopGroup eventLoopGroup() { return new NioEventLoopGroup(); } @Bean public Bootstrap bootstrap(EventLoopGroup eventLoopGroup, NettyClientHandler nettyClientHandler) { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new StringEncoder()); pipeline.addLast(new StringDecoder()); pipeline.addLast(nettyClientHandler); } }); return bootstrap; } @Bean public ChannelFuture channelFuture(Bootstrap bootstrap) throws InterruptedException { return bootstrap.connect(serverHost, serverPort).sync(); } } ``` 4. 在配置文件中配置要连接的多个TCP服务端的主机和端口。 ```properties # application.properties netty.server.host=127.0.0.1 netty.server.port=8080 ``` 5. 在需要使用Netty客户端的地方注入`ChannelFuture`对象,并使用它来发送消息给服务端。 ```java @Service public class NettyClientService { @Autowired private ChannelFuture channelFuture; public void sendMessage(String message) { if (channelFuture.channel().isActive()) { channelFuture.channel().writeAndFlush(message); } else { // 连接未建立或已断开,处理相应逻辑 } } } ``` 这样,你就可以在Spring Boot项目中使用Netty连接多个TCP服务端了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值