解决spring集成redisson踩过的坑

网友投稿 5068 2022-12-09

解决spring集成redisson踩过的坑

解决spring集成redisson踩过的坑

目录spring集成redisson踩过的坑第一坑就是版本兼容问题第二个坑是设置密码问题spring整合redisson配置配置方式单节点配置standalone哨兵配置sentinel集群配置cluster主从部署方式(master/slave)

spring集成redisson踩过的坑

我用spring的xml集成一直报错,所以只能选择注解方式:

@Configuration

public class RedissionConfig {

Logger log = LoggerFactory.getLogger(RedissionConfig.class);

@Value("${redis_iphttp://}")

String redis_ip;

@Value("${redis_port}")

String redis_port;

@Value("${redis_password}")

String redis_password;

@Bean(name="redissonClient")

public RedissonClient init(){

log.info("redis_ip:"+redis_ip);

log.info("redis_port:" +redis_port);

log.info("redis_password:"+redis_password);

Config config = new Config();

String url = "redis://"+redis_ip+":"+redis_port;

log.info(url);

config.useSingleServer().setAddress(url).setPassword(redis_password);

RedissonClient redissonClient = Redisson.create(config);

log.info("初始化RedissonClient");

return redissonClient;

}

}

第一坑就是版本兼容问题

我用的Spring是4.2.7,第一次集成的是3.12.0,会报以下错误:

严重: Unable to process Jar entry [module-info.class] from Jar [jar:file:/C:/Users/Administrator/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-yaml/2.10.1/jackson-dataformat-yaml-2.10.1.jar!/] for annotations org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19 at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:133) at org.apache.tomcat.util.bcel.classfile.ConstantPool.(ConstantPool.java:60) at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:209) at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:119) at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2134) at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2010) at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1976) at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1961) at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1319) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:748)

所以把版本降低到3.10.0,就不会报错了。

第二个坑是设置密码问题

因为要区分多环境,所以把redis的信息写到配置文件中,如果按照上面的配置,会报以下错误:

Caused by: org.redisson.client.RedisException: ERR Client sent AUTH, but no password is set. channel: [id: 0xe37c85e0, L:/192.168.0.128:54867 - R:192.168.0.128/192.168.0.128:5802] command: (AUTH), params: [] at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:314) at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:130) at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:110) at io-ty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:502) at io-ty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366) at io-ty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:278) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io-ty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io-ty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io-ty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io-ty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io-ty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io-ty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io-ty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io-ty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io-ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io-ty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965) at io-ty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) at io-ty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:656) at io-ty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:591) at io-ty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:508) at io-ty.channel.nio.NioEventLoop.run(NioEventLoop.java:470) ... 3 more

所以要判断密码是否为空,如果是空不做参数即可,下面是改正过的代码:

public RedissonClient init(){

log.info("redis_ip:"+redis_ip);

log.info("redis_port:" +redis_port);

log.info("redis_password:"+redis_password);

Config config = new Config();

String url = "redis://"+redis_ip+":"+redis_port;

log.info(url);

SingleServerConfig singleServerConfig = config.useSingleServer().setAddress(url);

if(!StringUtils.isEmpty(redis_password)){

singleServerConfig.setPassword(redis_password);

}

RedissonClient redissonClient = Redisson.create(config);

log.info("初始化RedissonClient");

return redissonClient;

}

另外,网上的帖子好多直接放的ip+端口号,但是我本地会报错,一定在加redis://192.168.2.128:6379这种格式的,可能是因为版本问题,也有可能就是误导人的。

看到上面的报错,感觉知道xml的错误了:

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.2.xsd

http://redisson.org/schema/redisson

http://redisson.org/schema/redisson/redisson.xsd">

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.2.xsd

http://redisson.org/schema/redisson

http://redisson.org/schema/redisson/redisson.xsd">

没有设置密码,一定不要把password属性放上去,要不会报错的~

spring整合redisson配置

配置方式

redis的部署方式有单节点部署、哨兵方式部署、集群方式部署3种方式

各种配置方式可以去看xsd文件:redisson-1.1.xsd

// 单节点

// 哨兵

// 集群

// 主从

等其他方式

单节点配置standalone

单纯的java代码,基于单节点部署为了保证数据的备份,一般会添加一个节点作为slave来备份master节点上的数据

//创建配置

Config config = new Config();

//指定使用单节点部署方式

config.useSingleServer().setAddress("redis://127.0.0.1:6379");

//创建客户端(发现这一非常耗时,基本在2秒-4秒左右)

RedissonClient redisson = Redisson.create(config);

//首先获取redis中的key-value对象,key不存在没关系

RBucket keyObject = redisson.getBucket("key");

//如果key存在,就设置key的值为新值value

//如果key不存在,就设置key的值为value

keyObject.set("value");

//最后关闭RedissonClient

redisson.shutdown();

spring整合pom

org.redisson

redisson

2.10.4

redis.clients

jedis

1.7.1.RELEASE

org.springframework.data

spring-data-redis

1.7.1.RELEASE

spring整合redisson.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://redisson.org/schema/redisson

http://redisson.org/schema/redisson/redisson.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://redisson.org/schema/redisson

http://redisson.org/schema/redisson/redisson.xsd">

哨兵配置sentinel

纯java代码

//创建配置

Config config = new Config();

//指定使用哨兵部署方式

config.useSentinelServers()

//设置sentinel.conf配置里的sentinel别名

//比如sentinel.conf里配置为sentinel monitor my-sentinel-name 127.0.0.1 6379 2,那么这里就配置my-sentinel-name

.setMasterName("my-sentinel-name")

//这里设置sentinel节点的服务IP和端口,sentinel是采用Paxos拜占庭协议,一般sentinel至少3个节点

//记住这里不是配置redis节点的服务端口和IP,sentinel会自己把请求转发给后面monitor的redis节点

.addSentinelAddress("redis://127.0.0.1:26379")

.addSentinelAddress("redis://127.0.0.1:26389")

.addSentinelAddress("redis://127.0.0.1:26399");

//创建客户端(发现这一非常耗时,基本在2秒-4秒左右)

RedissonClient redisson = Redisson.create(config);

//首先获取redis中的key-value对象,key不存在没关系

RBucket keyObject = redisson.getBucket("key");

//如果key存在,就设置key的值为新值value

//如果key不存在http://,就设置key的值为value

keyObject.set("value");

//最后关闭RedissonClient

redisson.shutdown();

spring整合redis和redisson的配置文件

xmlns="http://springframework.org/schema/beans" xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd">

xmlns="http://springframework.org/schema/beans" xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd">

使用

@Autowired

@Lazy

private RedissonClient redissonClient;

/**

* @param key 具体键值

* @return 锁对象

*/

public RReadWriteLock getLock(String key) {

return redissonClient.getReadWriteLock(key);

}

集群配置cluster

纯java代码

//创建配置

Config config = new Config();

//指定使用集群部署方式

config.useClusterServers()

// 集群状态扫描间隔时间,单位是毫秒

.setScanInterval(2000)

//cluster方式至少6个节点(3主3从,3主做sharding,3从用来保证主宕机后可以高可用)

.addNodeAddress("redis://127.0.0.1:6379" )

.addNodeAddress("redis://127.0.0.1:6380")

.addNodeAddress("redis://127.0.0.1:6381")

.addNodeAddress("redis://127.0.0.1:6382")

.addNodeAddress("redis://127.0.0.1:6383")

.addNodeAddress("redis://127.0.0.1:6384");

//创建客户端(发现这一非常耗时,基本在2秒-4秒左右)

RedissonClient redisson = Redisson.create(config);

//首先获取redis中的key-value对象,key不存在没关系

RBucket keyObject = redisson.getBucket("key");

//如果key存在,就设置key的值为新值value

//如果key不存在,就设置key的值为value

keyObject.set("value");

//最后关闭RedissonClient

redisson.shutdown();

spring整合redisson配置方式

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd">

properties文件

redisColony.host = 172.17.46.11:36379,172.17.46.11:36381,172.17.45.12:36379,172.17.45.12:36381,172.17.45.13:36379,172.17.45.13:36381

redis.timeOut = 100000

redis.redirection = 5

redisColony.host1 = redis://172.17.46.11:36379

redisColony.host2 = redis://172.17.46.11:36381

redisColony.host3 = redis://172.17.45.12:36379

redisColony.host4 = redis://172.17.45.12:36381

redisColony.host5 = redis://172.17.45.13:36379

redisColony.host6 = redis://172.17.45.13:36381

使用方式与哨兵配置方式相同,@Autowired注入即可

主从部署方式(master/slave)

纯java代码

//创建配置

Config config = new Config();

//指定使用主从部署方式

config.useMasterSlaveServers()

//设置redis主节点

.setMasterAddress("redis://127.0.0.1:6379")

//设置redis从节点

.addSlaveAddress("redis://127.0.0.1:6380", "redis://127.0.0.1:6381");

//创建客户端(发现这一非常耗时,基本在2秒-4秒左右)

RedissonClient redisson = Redisson.create(config);

//首先获取redis中的key-value对象,key不存在没关系

RBucket keyObject = redisson.getBucket("key");

//如果key存在,就设置key的值为新值value

//如果key不存在,就设置key的值为value

keyObject.set("value");

//最后关闭RedissonClient

redisson.shutdown();

spring整合redisson.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://redisson.org/schema/redisson

http://redisson.org/schema/redisson/redisson.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:redisson="http://redisson.org/schema/redisson"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://redisson.org/schema/redisson

http://redisson.org/schema/redisson/redisson.xsd">

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:IntelliJ IDEA窗口组件具体操作方法
下一篇:IDEA安装阿里巴巴编码规范插件的两种方式详解(在线安装和离线安装)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~