本文的示例代码参考NettyLB
目录
Netty
Server1
mkdir NettyLB && cd NettyLB
mkdir NettyServer1 && cd NettyServer1
gradle init --type java-application
gradle run
# Hello world.
记得添加.gitignore => "gi gradle >> .gitignore"
vim build.gradle
# compile 'io.netty:netty-all:4.1.25.Final'
mkdir -p src/main/java/server
vim src/main/java/server/ServerHandler.java
package server;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.nio.charset.Charset;
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf = (ByteBuf) msg;
System.out.println("Server1: " + byteBuf.toString(Charset.forName("utf-8")));
}
}
mv src/main/java/App.java src/main/java/server/Server.java
vim src/main/java/server/Server.java
package server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class Server {
private static final int PORT = 8881;
public static void main(String[] args) {
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerHandler());
}
});
serverBootstrap.bind(PORT).addListener(future -> {
if (future.isSuccess()) {
System.out.println("端口[" + PORT + "] 绑定成功!");
} else {
System.err.println("端口[" + PORT + "] 绑定失败!!!");
}
});
}
}
sed -i "" "s/App/server.Server/g" build.gradle && gradle run
# 端口[8881] 绑定成功!
# Server1: hello
telnet 127.0.0.1 8881
# Connected to localhost.
# hello
Server2
cd .. && cp -R NettyServer1 NettyServer2 && cd NettyServer2
sed -i "" "s/8881/8882/g" src/main/java/server/Server.java
sed -i "" "s/Server1/Server2/g" src/main/java/server/ServerHandler.java
sed -i "" "s/App/server.Server/g" build.gradle && gradle run
# 端口[8882] 绑定成功!
# Server2: hello
telnet 127.0.0.1 8882
# Connected to localhost.
# hello
Nginx
nginx -V
# nginx version: nginx/1.13.11
# configure arguments: --with-stream
vim nginx.conf
The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter
stream {
upstream netty {
server 127.0.0.1:8881;
server 127.0.0.1:8882;
}
server {
listen 7000;
proxy_connect_timeout 1s;
proxy_pass netty;
}
}
sudo nginx -t
sudo nginx -s reload
# cd NettyServer1
sed -i "" "s/App/server.Server/g" build.gradle && gradle run
# 端口[8881] 绑定成功!
# Server1: yes
# cd NettyServer2
sed -i "" "s/App/server.Server/g" build.gradle && gradle run
# 端口[8882] 绑定成功!
# Server2: no
telnet 127.0.0.1 7000
# Connected to localhost.
# yes
telnet 127.0.0.1 7000
# Connected to localhost.
# no
网友评论