• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Springboot2.X集成redis集群(Lettuce)连接的方法

springboot 搞代码 4年前 (2022-01-05) 17次浏览 已收录 0个评论

这篇文章主要介绍了Springboot2.X集成redis集群(Lettuce)连接的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前提:搭建好redis集群环境,搭建方式请看:

1. 新建工程,pom.xml文件中添加redis支持

  org.springframework.bootspring-boot-starter-data-redis

2.配置application.properties

 spring.redis.cluster.nodes=127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384,127.0.0.1:6385 spring.redis.cluster.timeout=1000 spring.redis.cluster.max-redirects=3

3.      新建下面的两个类

 @Configuration public class RedisConfiguration { @Resource private LettuceConnectionFactory myLettuceConnectionFactory; @Bean public RedisTemplate redisTemplate() { RedisTemplate template = new RedisTemplate(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(myLettuceConnectionFactory); return template; } } 
 @Configuration public class RedisFactoryConfig { @Autowired private Environment environmen<p style="color:transparent">来源gao!daima.com搞$代!码网</p>t; @Bean public RedisConnectionFactory myLettuceConnectionFactory() { Map source = new HashMap(); source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes")); source.put("spring.redis.cluster.timeout", environment.getProperty("spring.redis.cluster.timeout")); source.put("spring.redis.cluster.max-redirects", environment.getProperty("spring.redis.cluster.max-redirects")); RedisClusterConfiguration redisClusterConfiguration; redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source)); return new LettuceConnectionFactory(redisClusterConfiguration); } } 

4. 执行测试

 @SpringBootTest @RunWith(SpringRunner.class) public class RedisConfigurationTest { @Autowired private RedisTemplate redisTemplate; @Test public void redisTemplate() throws Exception { redisTemplate.opsForValue().set("author", "Damein_xym"); } } 

5. 验证,使用Redis Desktop Manager 连接redis节点,查看里面的数据是否存在author,有如下显示,证明成功。

以上就是Springboot2.X集成redis集群(Lettuce)连接的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Springboot2.X集成redis集群(Lettuce)连接的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址