전제 조건 : Redis 클러스터 환경을 구축하십시오. 건설 방법을 참조하십시오 : https://www.vevb.com/article/143749.htm
1. 새 프로젝트를 만들고 pom.xml 파일에 Redis 지원을 추가합니다.
<pectionency> <groupId> org.springframework.boot </groupid> <artifactid> 스프링 부트 스타터-데이터-레 디스 </artifactid> </fectionency>
2. Application.Properties 구성
spring.redis.cluster.nodes = 127.0.0.1 : 6380,127.0.0.1 : 6381,127.0.1 : 6382,127.0.1 : 6383,127.0 .0.1 : 6384,127.0.0.1 : 6385Spring.redis.cluster.timeout = 1000spring.redis.cluster.max-redirects = 3
3. 다음 두 클래스를 만듭니다
@ConfigurationPublic 클래스 readisconfiguration {@Resource private lettuceConnectionFactory mylettuceConnectionFactory; @bean public redistemplate <string, serializable> redistemplate () {redistemplate <string, serializable> template = new Redistemplate <> (); template.setKeyserializer (new StringRedisserializer ()); template.setValueserializer (새로운 genericjackson2jsonredisserializer ()); template.setConnectionFactory (mylettuceConnectionFactory); 리턴 템플릿; }} @ConfigurationPublic 클래스 RedisFactoryConfig {@autowired Private Environment Environment; @bean public readisconnectionfactory 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 = 새로운 redisclusterConfiguration (새로운 mapPropertySource ( "redisclusterConfiguration", source)); 새로운 lettuceConnectionFactory를 반환합니다 (RedisClusterConfiguration); }}4. 테스트를 수행하십시오
@springboottest@runwith (springrunner.class) public class readisconfigurationTest {@autowiredprivate redistemplate redistemplate; @testpublic void redistemplate () 예외 {redistemplate.opsforValue (). set ( "author", "damein_xym"); 5. Redis Desktop Manager를 사용하여 Redis 노드에 연결하고 내부의 데이터가 저자로 존재하는지 확인하십시오. 다음은 그것을 보여주고 성공적입니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.