springboot2.x Integrated Redis cluster (lettuce) connection

Source: Internet
Author: User
Tags redis desktop manager redis cluster

Premise: Set up a good redis cluster environment, set up the way to see: HTTPS://WWW.CNBLOGS.COM/XYMBLOG/P/9300574.HTML1. New project, add Redis support in Pom.xml file

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

2. Configure 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. Create a new class of two below

@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. Perform the test

@SpringBootTest @runwith (springrunner.class) public class Redisconfigurationtest {    @Autowiredprivate Redistemplate redistemplate; @Testpublic void Redistemplate () throws Exception {        redistemplate.opsforvalue (). Set ( "Author", "Damein_xym");}}

5. Verify that using Redis Desktop Manager to connect to the Redis node to see if the data inside is author, as shown below, proves successful.

springboot2.x Integrated Redis cluster (lettuce) connection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.