免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

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

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

10年積累的成都網站制作、成都網站設計、外貿營銷網站建設經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先做網站設計后付款的網站建設流程,更有長順免費網站建設讓你可以放心的選擇與我們合作。

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,有如下顯示,證明成功。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。

本文標題:Springboot2.X集成redis集群(Lettuce)連接的方法
當前鏈接:http://m.newbst.com/article36/gdspsg.html

成都網站建設公司_創新互聯,為您提供營銷型網站建設定制網站網站營銷網站建設面包屑導航網站收錄

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站優化排名