遠程netty客戶端連接上部署在雲服務器上的netty服務端

感動天感動地,終於可以連接上服務器了。。。。
看到篇問題和我一模一樣的博客:https://segmentfault.com/q/1010000015717059
看了一下下面的解答,大致就是公網IP並不是雲服務器上的IP啥的。

通過vim /etc/hosts,添加:

公網IP 自定義域名

然後修改客戶端程序:

@PostConstruct
    public void start() throws InterruptedException {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        ChannelPipeline pipeline = socketChannel.pipeline();
                        pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
                        pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
                        pipeline.addLast(nettyClientHandler);
                    }
                });
        ChannelFuture future = bootstrap.bind("自定義的域名(一開始我在這裏填了公網IP)",9000).sync();

        if (future.isSuccess()) {
            System.out.println("啓動 Netty 成功");
        }
    }

成功!太感動了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章