Springboot2.X集成redis集群(Lettuce)连接的方法
2018-07-13 13:39
1481 查看
前提:搭建好redis集群环境,搭建方式请看:https://www.geek-share.com/detail/2742454439.html
1. 新建工程,pom.xml文件中添加redis支持
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
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<String, Serializable> redisTemplate() { RedisTemplate<String, Serializable> template = new RedisTemplate<>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(myLettuceConnectionFactory); return template; } }
@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. 执行测试
@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,有如下显示,证明成功。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Redis在springboot中的使用教程
- spring boot中内嵌redis的使用方法示例
- SpringBoot 整合Redis 数据库的方法
- spring boot+spring cache实现两级缓存(redis+caffeine)
- SpringBoot整合Redis、ApachSolr和SpringSession的示例
- SpringBoot使用Redis缓存的实现方法
- spring boot整合redis实现shiro的分布式session共享的方法
- Spring Boot 与 Kotlin 使用Redis数据库的配置方法
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
- SpringBoot初步连接redis详解
相关文章推荐
- spring集成 JedisCluster 连接 redis3.0 集群
- springboot集成redis集群(带密码并序列化)
- SpringBoot集成redis集群存储字符串和对象
- spring boot下JedisCluster方式连接Redis集群的配置
- spring集成redis的lettuce连接pom文件配置
- spring集成 JedisCluster 连接 redis3.0 集群
- IDEA springboot集成ehcache单节点和redis集群
- spring集成 JedisCluster 连接 redis3.0 集群
- spring集成 JedisCluster 连接 redis3.0 集群
- spring集成 JedisCluster 连接 redis3.0 集群
- centos7环境下搭建redis及springboot连接集成
- SpringBoot利用redis集成消息队列的方法
- Spring Boot 连接 redis 集群
- Spring Boot中使用Redis做缓存的方法实例
- springboot2.0整合redis-cluster集群
- (16)spring boot中集成Redis实例
- spring集成 JedisCluster 联接 redis3.0 集群
- Spring Boot集成Redis实现缓存
- spring boot中内嵌redis的使用方法示例
- ssm springboot springcloud redis shiro dubbo 分布式集群 支付结算等架构教程视频