Netty FixedChannelPool
如今越来越多的应用采用Netty作为服务端高性能异步通讯框架,对于客户端而言,大部分需求只需和服务端建立一条链接收发消息。但如果客户端需要和服务端建立多条链接的例子就比较少了。
最简单的实现就是一个for循环,建立多个NioEventLoopGroup与服务端交互。另外还有如果要和多个服务端进行交互又该如何解决。
其实Netty从4.0版本就提供了连接池ChannelPool,可以解决与多个服务端交互以及与单个服务端建立连接池的问题。
服务端代码
首先我们完成服务端的代码,用户测试客户端的连接池。服务端不需要做任何特殊处理,代码如下。
package tk.yuqibit.nio.pool;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
public class NettyServer {
public void run(final int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter))
.addLast(new StringDecoder()).addLast(new StringEncoder())
.addLast(new DefaultEventExecutorGroup(8),
new NettyServerHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture future = b.bind(port).sync();
future.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 8080;
if (args.length > 0) {
try {
port = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
new NettyServer().run(port);
}
}
服务端Handler,打印客户端发送的字符串,并回复另一个字符串
package tk.yuqibit.nio.pool;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by YuQi on 2017/7/31.
*/
public class NettyServerHandler extends SimpleChannelInboundHandler<Object> {
static AtomicInteger count = new AtomicInteger(1);
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActived");
super.channelActive(ctx);
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String) msg;
System.out.println(count.getAndIncrement() + ":" + body);
ctx.writeAndFlush("Welcome to Netty.$_");
}
}
客户端代码
重点是以下客户端代码,首先要实现自己的ChannelPoolHandler
,主要是channelCreated
,当链接创建的时候添加channelhandler。
package tk.yuqibit.nio.pool;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.pool.ChannelPoolHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
/**
* Created by YuQi on 2017/7/31.
*/
public class NettyChannelPoolHandler implements ChannelPoolHandler {
@Override
public void channelReleased(Channel ch) throws Exception {
System.out.println("channelReleased. Channel ID: " + ch.id());
}
@Override
public void channelAcquired(Channel ch) throws Exception {
System.out.println("channelAcquired. Channel ID: " + ch.id());
}
@Override
public void channelCreated(Channel ch) throws Exception {
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
System.out.println("channelCreated. Channel ID: " + ch.id());
SocketChannel channel = (SocketChannel) ch;
channel.config().setKeepAlive(true);
channel.config().setTcpNoDelay(true);
channel.pipeline()
.addLast(new DelimiterBasedFrameDecoder(1024, delimiter))
.addLast(new StringDecoder()).addLast(new StringEncoder()).addLast(new NettyClientHander());
}
}
客户端Handler,打印服务端的response。
package tk.yuqibit.nio.pool;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.concurrent.atomic.AtomicInteger;
public class NettyClientHander extends ChannelInboundHandlerAdapter {
static AtomicInteger count = new AtomicInteger(1);
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(count.getAndIncrement() + ":" + msg);
}
}
客户端实现连接池,其中ChannelPoolMap可用于与多个服务端建立链接,本例中采用FixedChannelPool建立与单个服务端最大连接数为2的连接池。在main函数里通过向连接池获取channel发送了十条消息。
package tk.yuqibit.nio.pool;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.pool.AbstractChannelPoolMap;
import io.netty.channel.pool.ChannelPoolMap;
import io.netty.channel.pool.FixedChannelPool;
import io.netty.channel.pool.SimpleChannelPool;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import java.net.InetSocketAddress;
/**
* Created by YuQi on 2017/7/31.
*/
public class NettyPoolClient {
final EventLoopGroup group = new NioEventLoopGroup();
final Bootstrap strap = new Bootstrap();
InetSocketAddress addr1 = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress addr2 = new InetSocketAddress("10.0.0.11", 8888);
ChannelPoolMap<InetSocketAddress, SimpleChannelPool> poolMap;
public void build() throws Exception {
strap.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_KEEPALIVE, true);
poolMap = new AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool>() {
@Override
protected SimpleChannelPool newPool(InetSocketAddress key) {
return new FixedChannelPool(strap.remoteAddress(key), new NettyChannelPoolHandler(), 2);
}
};
}
public static void main(String[] args) throws Exception {
NettyPoolClient client = new NettyPoolClient();
client.build();
final String ECHO_REQ = "Hello Netty.$_";
for (int i = 0; i < 10; i++) {
// depending on when you use addr1 or addr2 you will get different pools.
final SimpleChannelPool pool = client.poolMap.get(client.addr1);
Future<Channel> f = pool.acquire();
f.addListener((FutureListener<Channel>) f1 -> {
if (f1.isSuccess()) {
Channel ch = f1.getNow();
ch.writeAndFlush(ECHO_REQ);
// Release back to pool
pool.release(ch);
}
});
}
}
}
输出结果
首先启动服务端,然后启动客户端,for循环里向服务端发送了10条消息。
服务端的输出如下,可以看到总共与服务端建立了两个channel,收到10条消息。
channelActived
channelActived
1:Hello Netty.
2:Hello Netty.
3:Hello Netty.
4:Hello Netty.
5:Hello Netty.
6:Hello Netty.
7:Hello Netty.
8:Hello Netty.
9:Hello Netty.
10:Hello Netty.
客户端输入如下,可以看到channelCreated了两次,剩下都是从连接池里请求连接和释放连接
channelCreated. Channel ID: ea8504a8
channelCreated. Channel ID: 77c8857b
channelReleased. Channel ID: ea8504a8
channelReleased. Channel ID: 77c8857b
channelAcquired. Channel ID: ea8504a8
channelAcquired. Channel ID: 77c8857b
channelReleased. Channel ID: ea8504a8
channelReleased. Channel ID: 77c8857b
channelAcquired. Channel ID: 77c8857b
channelAcquired. Channel ID: ea8504a8
channelReleased. Channel ID: ea8504a8
channelAcquired. Channel ID: ea8504a8
channelReleased. Channel ID: 77c8857b
channelReleased. Channel ID: ea8504a8
channelAcquired. Channel ID: 77c8857b
channelAcquired. Channel ID: ea8504a8
channelReleased. Channel ID: 77c8857b
channelAcquired. Channel ID: 77c8857b
channelReleased. Channel ID: ea8504a8
channelReleased. Channel ID: 77c8857b
1:Welcome to Netty.
2:Welcome to Netty.
3:Welcome to Netty.
4:Welcome to Netty.
5:Welcome to Netty.
6:Welcome to Netty.
7:Welcome to Netty.
8:Welcome to Netty.
9:Welcome to Netty.
10:Welcome to Netty.
Netty FixedChannelPool的更多相关文章
- 教你正确地利用Netty建立连接池
一.问题描述 Netty是最近非常流行的高性能异步通讯框架,相对于Java原生的NIO接口,Netty封装后的异步通讯机制要简单很多. 但是小K最近发现并不是所有开发人员在使用的过程中都了解其内部实现 ...
- Netty自带连接池的使用
一.类介绍1.ChannelPool——连接池接口 2.SimpleChannelPool——实现ChannelPool接口,简单的连接池实现 3.FixedChannelPool——继承Simple ...
- 谈谈如何使用Netty开发实现高性能的RPC服务器
RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络,从远程计算机程序上请求服务,而不必了解底层网络技术的协议.说的再直白一点,就是客户端在不必知道 ...
- 基于netty http协议栈的轻量级流程控制组件的实现
今儿个是冬至,所谓“冬大过年”,公司也应景五点钟就放大伙儿回家吃饺子喝羊肉汤了,而我本着极高的职业素养依然坚持留在公司(实则因为没饺子吃没羊肉汤喝,只能呆公司吃食堂……).趁着这一个多小时的时间,想跟 ...
- 从netty-example分析Netty组件续
上文我们从netty-example的Discard服务器端示例分析了netty的组件,今天我们从另一个简单的示例Echo客户端分析一下上个示例中没有出现的netty组件. 1. 服务端的连接处理,读 ...
- 源码分析netty服务器创建过程vs java nio服务器创建
1.Java NIO服务端创建 首先,我们通过一个时序图来看下如何创建一个NIO服务端并启动监听,接收多个客户端的连接,进行消息的异步读写. 示例代码(参考文献[2]): import java.io ...
- 从netty-example分析Netty组件
分析netty从源码开始 准备工作: 1.下载源代码:https://github.com/netty/netty.git 我下载的版本为4.1 2. eclipse导入maven工程. netty提 ...
- Netty实现高性能RPC服务器优化篇之消息序列化
在本人写的前一篇文章中,谈及有关如何利用Netty开发实现,高性能RPC服务器的一些设计思路.设计原理,以及具体的实现方案(具体参见:谈谈如何使用Netty开发实现高性能的RPC服务器).在文章的最后 ...
- Netty构建分布式消息队列(AvatarMQ)设计指南之架构篇
目前业界流行的分布式消息队列系统(或者可以叫做消息中间件)种类繁多,比如,基于Erlang的RabbitMQ.基于Java的ActiveMQ/Apache Kafka.基于C/C++的ZeroMQ等等 ...
随机推荐
- 成都,我们来啦 | Dubbo 社区开发者日
[关注 阿里巴巴云原生 公众号,回复关键词"报名",即可参与抽奖!] 活动时间:10 月 26 日 13:00 - 18:00 活动地点:成都市高新区交子大道中海国际中心 233 ...
- Budgie Desktop in Linux 无法使用触摸板右键功能
问题描述 Budgie桌面在Linux环境下很好用,不过最近发现触摸板的右键功能是不可用的 解决方法 经查阅Gnome官方文档(Budgie有很多功能基于Gnome)发现是Gnome官方把这个功能去掉 ...
- Kubernetes 静态PV使用
Kubernetes 静态PV使用 Kubernetes支持持久卷的存储插件:https://kubernetes.io/docs/concepts/storage/persistent-volum ...
- 解决Git 克隆代码 The remote end hung up unexpectedly错误
从GitHub上克隆一个项目一直不成功!猜想可能是文件太大超时或者网络太慢超时! 解决方案: 配置 git config -- git config -- 增加最低速时间,but,还是不行! 公司网络 ...
- 【UOJ#388】【UNR#3】配对树(线段树,dsu on tree)
[UOJ#388][UNR#3]配对树(线段树,dsu on tree) 题面 UOJ 题解 考虑一个固定区间怎么计算答案,把这些点搞下来建树,然后\(dp\),不难发现一个点如果子树内能够匹配的话就 ...
- kali渗透综合靶机(十一)--BSides-Vancouver靶机
kali渗透综合靶机(十一)--BSides-Vancouver靶机 靶机下载地址:https://pan.baidu.com/s/1s2ajnWHNVS_NZfnAjGpEvw 一.主机发现 1.n ...
- kali渗透综合靶机(十)--Raven靶机
kali渗透综合靶机(十)--Raven靶机 一.主机发现 1.netdiscover -i eth0 -r 192.168.10.0/24 二.端口扫描 1. masscan --rate=1000 ...
- Maven配置教程详解
Maven的安装与配置 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载以下maven安装包 解压缩即可 根据你的安装路径配置maven环境变量 ...
- ASP.NET Core appsettings.json 文件
ASP.NET Core appsettings.json 文件 在本节中,我们将讨论 ASP.NET Core 项目中appsettings.json文件的重要性. 在以前的 ASP.NET 版本中 ...
- SQL server已经设置为单用户模式,Sql server还原失败数据库正在使用,无法获得对数据库的独占访问权
如果已经设置为单用户模式,还是报这个错误的话,就按照一下SQL执行就