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