感动天感动地,终于可以连接上服务器了。。。。
看到篇问题和我一模一样的博客: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 成功");
}
}
成功!太感动了。