使用Netty进行两台或者多台服务器之间的数据通信,大体有以下三种情况:
- 使用长连接通道不断开的方式进行通信。也就是服务器和客户端的通道不断开,一直处于开启状态,如果服务器的性能足够好,并且我们的客户端数量足够少的情况下,推荐这种方式。
- 一次性批量提交数据,推荐采用短连接方式。即我们可以把数据保存在本地临时缓冲区或者临时表中,当到达一定临界值的时候一次性批量提交,或者是根据定时任务轮询提交,这种方式的弊端是做不到实时性,在对实时性要求不高的应用程序中推荐使用。
- 我们可以使用一种特殊的长连接,在指定的一段时间内,服务器与某台客户端么有进行任何通信,则断开连接。下次客户端向服务器端发送请求时再次建立连接,这种模式需要考虑两种因素:
3.1 如何在服务器端和客户端在一定时间超时后关闭通道?关闭通道后如何再建立连接?
答:可以使用Netty的ReadTimeoutHandler,在一定时间内没读取到数据则断开连接;再次建立连接直接发起请求即可。
3.2 客户端宕机时我们无需考虑,下次客户端重启后就可以与服务器建立连接;但是服务器宕机后,我们客户端如何与服务端建立连接?答案无非是隔一段时间轮询建立连接。
以下是设置连接的超时处理,在规定时间内无数据传输则断开,在断开连接后重新建立连接:
server:
public class Server {
public static void main(String[] args) {
EventLoopGroup pGroup = new NioEventLoopGroup();
EventLoopGroup cGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(pGroup, cGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
// 设置日志
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel sc) throws Exception {
// Marshalling的编解码器
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
// 设置无数据读取的超时处理器
sc.pipeline().addLast(new ReadTimeoutHandler(5));
// 自定义处理器
sc.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture cf = b.bind(8765).sync();
cf.channel().closeFuture().sync();
}finally{
pGroup.shutdownGracefully();
cGroup.shutdownGracefully();
}
}
}
serverhandler
public class ServerHandler extends ChannelHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Request request = (Request)msg;
System.out.println("Server : " + request.getId() + ", " + request.getName() + ", " + request.getRequestMessage());
Response response = new Response();
response.setId(request.getId());
response.setName("response" + request.getId());
response.setResponseMessage("响应内容" + request.getId());
ctx.writeAndFlush(response);//.addListener(ChannelFutureListener.CLOSE);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.close();
}
}
client:
public class Client {
private static class SingletonHolder {
static final Client instance = new Client();
}
public static Client getInstance(){
return SingletonHolder.instance;
}
private EventLoopGroup group;
private Bootstrap b;
private ChannelFuture cf ;
private Client(){
group = new NioEventLoopGroup();
b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
// 超时handler(当服务器端与客户端在指定时间以上没有任何进行通信,则会关闭响应的通道,主要为减小服务端资源占用)
sc.pipeline().addLast(new ReadTimeoutHandler(5));
sc.pipeline().addLast(new ClientHandler());
}
});
}
public void connect(){
try {
this.cf = b.connect("127.0.0.1", 8765).sync();
System.out.println("远程服务器已经连接, 可以进行数据交换..");
} catch (Exception e) {
e.printStackTrace();
}
}
public ChannelFuture getChannelFuture(){
if(this.cf == null){
this.connect();
}
if(!this.cf.channel().isActive()){
this.connect();
}
return this.cf;
}
public static void main(String[] args) throws Exception{
final Client c = Client.getInstance();
ChannelFuture cf = c.getChannelFuture();
for(int i = 1; i <= 3; i++ ){
Request request = new Request();
request.setId("" + i);
request.setName("pro" + i);
request.setRequestMessage("数据信息" + i);
cf.channel().writeAndFlush(request);
TimeUnit.SECONDS.sleep(4);
}
cf.channel().closeFuture().sync();
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("进入子线程...");
ChannelFuture cf = c.getChannelFuture();
System.out.println(cf.channel().isActive());
System.out.println(cf.channel().isOpen());
//再次发送数据
Request request = new Request();
request.setId("" + 4);
request.setName("pro" + 4);
request.setRequestMessage("数据信息" + 4);
cf.channel().writeAndFlush(request);
cf.channel().closeFuture().sync();
System.out.println("子线程结束.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
System.out.println("断开连接,主线程结束..");
}
}
clienthandler
public class ClientHandler extends ChannelHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
Response resp = (Response)msg;
System.out.println("Client : " + resp.getId() + ", " + resp.getName() + ", " + resp.getResponseMessage());
} finally {
ReferenceCountUtil.release(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
流程分析:
客户端每隔4s钟向服务器端发数据:
ChannelFuture cf = c.getChannelFuture();
for(int i = 1; i <= 3; i++ ){
Request request = new Request();
request.setId("" + i);
request.setName("pro" + i);
request.setRequestMessage("数据信息" + i);
cf.channel().writeAndFlush(request);
TimeUnit.SECONDS.sleep(4);
}
cf.channel().closeFuture().sync();
那么当发完三次数据后,将会断开和服务器端的连接,看打印:
Client打印:
远程服务器已经连接, 可以进行数据交换..
Client : 1, response1, 响应内容1
Client : 2, response2, 响应内容2
Client : 3, response3, 响应内容3
断开连接,主线程结束..
Server打印:
[01:07:14] nioEventLoopGroup-0-0 INFO [] [] [io.netty.handler.logging.LoggingHandler] - [id: 0xa1a7160d] REGISTERED
[01:07:14] nioEventLoopGroup-0-0 INFO [] [] [io.netty.handler.logging.LoggingHandler] - [id: 0xa1a7160d] BIND: 0.0.0.0/0.0.0.0:8765
[01:07:14] nioEventLoopGroup-0-0 INFO [] [] [io.netty.handler.logging.LoggingHandler] - [id: 0xa1a7160d, /0:0:0:0:0:0:0:0:8765] ACTIVE
[01:07:21] nioEventLoopGroup-0-1 INFO [] [] [io.netty.handler.logging.LoggingHandler] - [id: 0xa1a7160d, /0:0:0:0:0:0:0:0:8765] RECEIVED: [id: 0x076dc15e, /127.0.0.1:61655 => /127.0.0.1:8765]
Server : 1, pro1, 数据信息1
Server : 2, pro2, 数据信息2
Server : 3, pro3, 数据信息3
为了让客户端断开后可以重新连接上,添加以下代码:
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("进入子线程...");
ChannelFuture cf = c.getChannelFuture();
System.out.println(cf.channel().isActive());
System.out.println(cf.channel().isOpen());
// 再次发送数据
Request request = new Request();
request.setId("" + 4);
request.setName("pro" + 4);
request.setRequestMessage("数据信息" + 4);
cf.channel().writeAndFlush(request);
cf.channel().closeFuture().sync();
System.out.println("子线程结束.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
开启一个线程重新获取channelFuture:
public ChannelFuture getChannelFuture(){
if(this.cf == null){
this.connect();
}
if(!this.cf.channel().isActive()){
this.connect();
}
return this.cf;
}
于是客户端继续向服务器发送第四次数据:
服务端: