一、Redisson是什么?
Redisson——具有内存数据网格功能的Redis Java 客户端
基于高性能异步无锁Java Redis客户端和Netty框架。
JDK 兼容性:1.8 - 16,Android
二、使用步骤
1.引入库
- maven
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.16.0</version>
</dependency>
2.Redisson 配置
Config config = new Config();
//设置编解码器(默认FST)
config.setCodec(new JsonJacksonCodec());
//模式设置(单例)
config.useSingleServer()
.setAddress("redis://You redis IP: Port")
.setPassword("password")
.setDatabase(Database);
RedissonClient redisson = Redisson.create(config);
2.增(改)
public static void addData(RedissonClient redissonClient, DataType Type){
switch (Type){
case String:
RBucket<String> rBucket = redissonClient.getBucket("fud:test:meng:String");
rBucket.set("MengTest");
break;
case Hash:
RMap<String, Object> rMap = redissonClient.getMap("fud:test:meng:map");
HashMap<String,String> DataMap = new HashMap<>();
DataMap.put("Test1","1");
DataMap.put("Test2","2");
DataMap.put("Test3","3");
DataMap.put("Test4","4");
rMap.putAll(DataMap);
break;
case List:
RList<String> rList = redissonClient.getList("fud:test:meng:list");
List<String> List = new ArrayList();
List.add("Test1");
List.add("Test2");
List.add("Test3");
List.add("Test4");
List.add("Test5");
rList.addAll(List);
break;
case Set:
RSet<String> rSet = redissonClient.getSet("fud:test:meng:set");
HashSet<String> set = new HashSet<>();
set.add("Test1");
set.add("Test2");
set.add("Test3");
set.add("Test4");
set.add("Test5");
rSet.addAll(set);
break;
default:
break;
}
}
Redis对应命令
SET fud:test:meng:String "MengTest"
HMSET fud:test:meng:map Test1"1" Test12 "Test2"
RPUSH fud:test:meng:list "Test1"
RPUSH fud:test:meng:list "Test2"
SADD fud:test:meng:set "Test1"
SADD fud:test:meng:set "Test2"
3.删
public static void DeleteData(RedissonClient redissonClient, String key, DataType Type){
switch (Type){
case String:
redissonClient.getBucket(key).delete();
break;
case Hash:
redissonClient.getKeys().delete(key);
break;
case Set:
redissonClient.getKeys().delete(key);
break;
case List:
redissonClient.getKeys().delete(key);
default:
break;
}
}
4.查
public static void SelectData(RedissonClient redissonClient, String key, DataType Type){
switch (Type){
case String:
RBucket<String> rBucket = redissonClient.getBucket(key);
System.out.println(rBucket.get());
break;
case Hash:
RMap<String, Object> rMap = redissonClient.getMap(key);
Map<String, Object> Map = rMap.readAllMap();
Map.forEach((s,o)->{
System.out.println(s+" "+o);
});
break;
case List:
RList<String> rList = redissonClient.getList(key);
List<String> list = rList.readAll();
list.forEach(System.out::println);
break;
case Set:
RSet<String> rSet = redissonClient.getSet(key);
Set<String> set = rSet.readAll();
set.forEach((s)->{
System.out.println("s = " + s);
});
break;
default:
break;
}
}
5.测试类
public enum DataType {
String,
Hash,
List,
Set,
}
public static void main(String[] args){
Config config = new Config();
config.setCodec(new JsonJacksonCodec());
config.useSingleServer()// use "rediss://" for SSL connection
.setAddress("redis://10.8.1.211:6379")
.setPassword("211")
.setDatabase(15);
RedissonClient redissonClient = Redisson.create(config);
System.out.println("===============写数据开始=================");
addData(redissonClient,DataType.String);
addData(redissonClient,DataType.Hash);
addData(redissonClient,DataType.List);
addData(redissonClient,DataType.Set);
System.out.println("===============写数据结束=================="+"\n");
//读
System.out.println("===============读数据开始=================");
SelectData(redissonClient,"fud:test:meng:String",DataType.String);
SelectData(redissonClient,"fud:test:meng:map",DataType.Hash);
SelectData(redissonClient,"fud:test:meng:list",DataType.List);
SelectData(redissonClient,"fud:test:meng:set",DataType.Set);
System.out.println("===============读数据结束=================="+"\n");
//删
System.out.println("===============删数据开始=================");
DeleteData(redissonClient,"fud:test:meng:String",DataType.String);
DeleteData(redissonClient,"fud:test:meng:map",DataType.Hash);
DeleteData(redissonClient,"fud:test:meng:list",DataType.List);
DeleteData(redissonClient,"fud:test:meng:set",DataType.Set);
System.out.println("===============删数据结束=================="+"\n");
redissonClient.shutdown();
System.out.println("是否关闭"+redissonClient.isShutdown());
}
三、遇到错误
完整报错信息
16:55:31.272 [redisson-netty-2-6] ERROR org.redisson.client.handler.CommandDecoder - Unable to decode data. channel: [id: 0x57c050b5, L:/10.8.7.3:63833 - R:/10.8.1.211:6379], reply: ReplayingDecoderByteBuf(ridx=17, widx=17), command: (GET), params: [fud:test:meng:String]
java.io.IOException: java.lang.NullPointerException
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:247)
at org.redisson.codec.FstCodec$1.decode(FstCodec.java:250)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:368)
at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:196)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:134)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:104)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:507)
at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: null
at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:357)
at org.nustaq.serialization.FSTObjectInput.readObjectInternal(FSTObjectInput.java:331)
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:311)
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:245)
... 24 common frames omitted
16:55:31.273 [redisson-netty-2-6] DEBUG org.redisson.command.RedisExecutor - connection released for command (GET) and params [fud:test:meng:String] from slot NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=1, freeSubscribeConnectionsCounter=value:50:queue:0, freeConnectionsAmount=24, freeConnectionsCounter=value:64:queue:0, freezed=false, freezeReason=null, client=[addr=redis://10.8.1.211:6379], nodeType=MASTER, firstFail=0]]] using connection RedisConnection@971981736 [redisClient=[addr=redis://10.8.1.211:6379], channel=[id: 0x57c050b5, L:/10.8.7.3:63833 - R:/10.8.1.211:6379], currentCommand=CommandData [promise=RedissonPromise [promise=ImmediateEventExecutor$ImmediatePromise@496257e3(failure: java.io.IOException: java.lang.NullPointerException)], command=(GET), params=[fud:test:meng:String], codec=org.redisson.codec.FstCodec]]
16:55:31.274 [redisson-netty-2-6] WARN io.netty.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.handler.codec.DecoderException: java.io.IOException: java.lang.NullPointerException
at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:421)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: java.lang.NullPointerException
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:247)
at org.redisson.codec.FstCodec$1.decode(FstCodec.java:250)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:368)
at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:196)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:134)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:104)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:507)
at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)
... 17 common frames omitted
Caused by: java.lang.NullPointerException: null
at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:357)
at org.nustaq.serialization.FSTObjectInput.readObjectInternal(FSTObjectInput.java:331)
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:311)
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:245)
... 24 common frames omitted
Exception in thread "main" org.redisson.client.RedisException: Unexpected exception while processing command
at org.redisson.command.CommandAsyncService.convertException(CommandAsyncService.java:350)
at org.redisson.command.CommandAsyncService.get(CommandAsyncService.java:147)
at org.redisson.RedissonObject.get(RedissonObject.java:90)
at org.redisson.RedissonBucket.get(RedissonBucket.java:104)
at com.example.demo.Test.SelectData(Test.java:102)
at com.example.demo.Test.main(Test.java:37)
Caused by: java.io.IOException: java.lang.NullPointerException
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:247)
at org.redisson.codec.FstCodec$1.decode(FstCodec.java:250)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:368)
at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:196)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:134)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:104)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:507)
at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:357)
at org.nustaq.serialization.FSTObjectInput.readObjectInternal(FSTObjectInput.java:331)
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:311)
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:245)
... 24 more
重要报错信息
16:55:31.272 [redisson-netty-2-6] ERROR org.redisson.client.handler.CommandDecoder - Unable to decode data. channel: [id: 0x57c050b5, L:/10.8.7.3:63833 - R:/10.8.1.211:6379], reply: ReplayingDecoderByteBuf(ridx=17, widx=17), command: (GET), params: [fud:test:meng:String]
可得知是数据序列化和反序列化的过程出现的问题,可能是 编/解码器 未设置或设置的不同(请看 2.Redisson 配置)
总结
这是Redisson入门学习记录,只是简单的进行了增删改查。更多内容请参考Redisson官网。