将netty服务端编写简化到一个注解就能完成的程度,客户端编写甚至更简单。码云地址:https://gitee.com/xiaoyudeguang/easy-netty
<dependency>
<groupId>io.github.xiaoyudeguang</groupId>
<artifactId>easy-netty</artifactId>
<version>1.2.5-RELEASE</version>
</dependency>
@Server(port = 9000)
public class NettyServer extends AbstractNettyServer{
@Override
public String handleMsg(String msg) {
Console.log(this.getClass(), msg);
return EasyBuffer.wrapper(this.hashCode(),"=>",System.currentTimeMillis());
}
}
注意,@Server注解和继承AbstractNettyServer类是必须的。
NettyUtils.getInstance("127.0.0.1", 9000).sendMsg(msg);
@Sharable
public class NettyClient extends AbstractNettyClient{
public NettyClient(String key, String host, int port) throws Exception {
super(key, host, port);
}
@Override
public void doOnConnection(ChannelHandlerContext ctx) throws Exception {
Console.log(this.getClass(), "初次建立连接时做点啥");
this.write(hex);
this.writeBytes(bytes);
this.writeMsg(msg);
}
@Override
public Object doService(Channel channel, byte[] msg) throws Exception {
Console.log(this.getClass(), "服务端响应时做点啥");
return null;
}
}
(1)@Sharable和继承AbstractNettyClient类是必须的。构造方法的key,可以随意设置,只是用于保存Netty连接。
(2)怎么向服务端输出点啥呢?你可以用doOnConnection()方法里提到的三个write()方法来输出内容到服务端。
(3) 在doService()方法里,返回null将不会输出东西到服务端;同样,你也可以直接调用三个write()方法来蹂躏服务端。
public String test(String key, String host, int port, String data) throws Exception {
BlockNettyClient nettyClient = CacheUtils.get(key);
//如果从未与TCPServer建立过连接,就建立一个连接并缓存起来;否则,直接从缓存里取出已经建立好的连接发送消息
if (nettyClient == null) {
nettyClient = new BlockNettyClient(key, host, port, data);
ThreadManager.execute(new Thread(nettyClient));
CacheUtils.put(key, nettyClient);
} else {
nettyClient.doSend(data);
}
return nettyClient.getResponse();
}