nanshaws/nettyWeb: 复习一下netty,并打算做一个web项目出来 (github.com)
SET key value:设置指定key的值。
GET key:获取指定key的值。
DEL key:删除指定key。
EXISTS key:检查指定key是否存在。
TTL key:获取指定key的过期时间。
KEYS pattern:查找所有符合指定模式的key。
INCR key:将指定key的值增加1。
DECR key:将指定key的值减少1。
LPUSH key value:将值添加到列表的左侧。
RPUSH key value:将值添加到列表的右侧。
LPOP key:移除并返回列表左侧的值。
RPOP key:移除并返回列表右侧的值。
SADD key member:将成员添加到集合中。
SMEMBERS key:获取集合中的所有成员。
ZADD key score member:将成员添加到有序集合中。
ZRANGE key start stop:按照分数从小到大的顺序获取有序集合中指定范围的成员。
HSET key field value:将哈希表中指定字段的值设置为指定值。
HGET key field:获取哈希表中指定字段的值。
HMGET key field1 [field2]:获取哈希表中指定字段的值列表。
PING:测试Redis服务器是否可用。
引入依赖:
4.0.0
org.tianfan
nettyTestLearn
1.0-SNAPSHOT
17
17
UTF-8
io.netty
netty-transport-native-epoll
4.1.70.Final
io.netty
netty-all
4.1.86.Final
jakarta.activation
jakarta.activation-api
2.1.2
org.eclipse.angus
angus-mail
1.0.0
mysql
mysql-connector-java
8.0.33
org.mybatis
mybatis
3.5.10
org.projectlombok
lombok
1.18.30
com.google.code.gson
gson
2.8.9
完整代码:
package org.tianfan.example;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.GenericFutureListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RedisClient {
String host; // 目标主机
int port; // 目标主机端口
public RedisClient(String host,int port){
this.host = host;
this.port = port;
}
public void start() throws Exception{
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new RedisClientInitializer());
Channel channel = bootstrap.connect(host, port).sync().channel();
System.out.println(" connected to host : " + host + ", port : " + port);
System.out.println(" type redis's command to communicate with redis-server or type 'quit' to shutdown ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
ChannelFuture lastWriteFuture = null;
for (;;) {
String s = in.readLine();
if(s.equalsIgnoreCase("quit")) {
break;
}
System.out.print(">");
lastWriteFuture = channel.writeAndFlush(s);
lastWriteFuture.addListener(new GenericFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
System.err.print("write failed: ");
future.cause().printStackTrace(System.err);
}
}
});
}
if (lastWriteFuture != null) {
lastWriteFuture.sync();
}
System.out.println(" bye ");
}finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
RedisClient client = new RedisClient("192.168.56.10",6379);
client.start();
}
}
package org.tianfan.example;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.CodecException;
import io.netty.handler.codec.redis.*;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import java.util.ArrayList;
import java.util.List;
public class RedisClientHandler extends ChannelDuplexHandler {
// 发送 redis 命令
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
String[] commands = ((String) msg).split("\\s+");
List children = new ArrayList<>(commands.length);
for (String cmdString : commands) {
children.add(new FullBulkStringRedisMessage(ByteBufUtil.writeUtf8(ctx.alloc(), cmdString)));
}
RedisMessage request = new ArrayRedisMessage(children);
ctx.write(request, promise);
}
// 接收 redis 响应数据
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
RedisMessage redisMessage = (RedisMessage) msg;
// 打印响应消息
printAggregatedRedisResponse(redisMessage);
// 是否资源
ReferenceCountUtil.release(redisMessage);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
System.err.print("exceptionCaught: ");
cause.printStackTrace(System.err);
ctx.close();
}
private static void printAggregatedRedisResponse(RedisMessage msg) {
if (msg instanceof SimpleStringRedisMessage) {
System.out.println(((SimpleStringRedisMessage) msg).content());
} else if (msg instanceof ErrorRedisMessage) {
System.out.println(((ErrorRedisMessage) msg).content());
} else if (msg instanceof IntegerRedisMessage) {
System.out.println(((IntegerRedisMessage) msg).value());
} else if (msg instanceof FullBulkStringRedisMessage) {
System.out.println(getString((FullBulkStringRedisMessage) msg));
} else if (msg instanceof ArrayRedisMessage) {
for (RedisMessage child : ((ArrayRedisMessage) msg).children()) {
printAggregatedRedisResponse(child);
}
} else {
throw new CodecException("unknown message type: " + msg);
}
}
private static String getString(FullBulkStringRedisMessage msg) {
if (msg.isNull()) {
return "(null)";
}
return msg.content().toString(CharsetUtil.UTF_8);
}
}
package org.tianfan.example;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.redis.RedisArrayAggregator;
import io.netty.handler.codec.redis.RedisBulkStringAggregator;
import io.netty.handler.codec.redis.RedisDecoder;
import io.netty.handler.codec.redis.RedisEncoder;
public class RedisClientInitializer extends ChannelInitializer {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new RedisDecoder());
pipeline.addLast(new RedisBulkStringAggregator());
pipeline.addLast(new RedisArrayAggregator());
pipeline.addLast(new RedisEncoder());
pipeline.addLast(new RedisClientHandler());
}
}
结果图: