gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 30 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Springboot2.X集成redis集群(Lettuce)连接的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

前提:搭建好redis集群环境,搭建方式请看:http://www.zzvips.com/article/29914.html 。

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

?
1
2
3
4
<dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>

2.配置application.properties 。

?
1
2
3
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.      新建下面的两个类 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@configuration
public class redisconfiguration {
   @resource
   private lettuceconnectionfactory mylettuceconnectionfactory;
   @bean
   public redistemplate<string, serializable> redistemplate() {
     redistemplate<string, serializable> template = new redistemplate<>();
     template.setkeyserializer( new stringredisserializer());
     template.setvalueserializer( new genericjackson2jsonredisserializer());
     template.setconnectionfactory(mylettuceconnectionfactory);
     return template;
   }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@configuration
public class redisfactoryconfig {
   @autowired
   private environment environment;
   @bean
   public redisconnectionfactory mylettuceconnectionfactory() {
     map<string, object> source = new hashmap<string, object>();
     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. 执行测试 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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,有如下显示,证明成功.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:http://www.cnblogs.com/xymBlog/p/9303032.html 。

最后此篇关于Springboot2.X集成redis集群(Lettuce)连接的方法的文章就讲到这里了,如果你想了解更多关于Springboot2.X集成redis集群(Lettuce)连接的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com