网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

前提:搭建好redis集群环境,搭建方式请看:https://www.jb51.net/article/143749.htm

10年积累的成都网站制作、成都网站设计、外贸营销网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站设计后付款的网站建设流程,更有长顺免费网站建设让你可以放心的选择与我们合作。

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


  org.springframework.boot
  spring-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 environment;
  @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)连接的方法
当前路径:http://bjjierui.cn/article/gdspsg.html

其他资讯