Springboot2.X整合redis叢集(Lettuce)串連

來源:互聯網
上載者:User

標籤:etc   cti   clust   time   整合   red   public   private   tps   

前提:搭建好redis叢集環境,搭建方式請看:https://www.cnblogs.com/xymBlog/p/9300574.html1. 建立工程,pom.xml檔案中添加redis支援

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 2.      配置application.properties

 

1 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:63852 3 spring.redis.cluster.timeout=10004 5 spring.redis.cluster.max-redirects=3

 

 

3.      建立下面的兩個類

 

@Configurationpublic 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;    }}

  

 

 

@Configurationpublic 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 {    @Autowiredprivate RedisTemplate redisTemplate;@Testpublic void redisTemplate() throws Exception {        redisTemplate.opsForValue().set("author", "Damein_xym");}}

 

5.      驗證,使用Redis Desktop Manager 串連redis節點,查看裡面的資料是否存在author,有如下顯示,證明成功。

 

Springboot2.X整合redis叢集(Lettuce)串連

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.