Voraussetzung: Erstellen Sie eine Redis -Cluster -Umgebung. Bitte beachten Sie die Konstruktionsmethode: https://www.vevb.com/article/143749.htm
1. Erstellen Sie ein neues Projekt und fügen Sie der Datei pom.xml REDIS -Unterstützung hinzu
<Depopenty> <gruppe> org.springFramework.boot </Groupid> <artifactId> Spring-Boot-Starter-Data-redis </artifactid> </abhängig>
2. Konfigurieren Sie application.Properties
Spring.redis.cluster.nodes = 127.0.0.1: 6380,127.0.0.1: 6381,127.0.1: 6382,127.0.0.1: 6383,127.0 .0.1: 6384,127.0.0.1: 6385spring.redis.cluster.timeout = 1000Spring.redis.cluster.max-Redirects = 3
3. Erstellen Sie die folgenden zwei Klassen
@ConfigurationPublic Class Redisconfiguration {@resource private lettuceConnectionFactory mylettucEconnectionFactory; @Bean Public Redistemplate <String, serialisierbar> redistemplate () {redistemplate <string, serializable> template = new redistemplate <> (); template.setkeyserializer (neuer StringRedisserializer ()); template.setValueSerializer (neuer genericjackson2jsonRedisserializer ()); template.setConnectionFactory (mylettucEconnectionFactory); Rückgabevorlage; }} @ConfigurationPublic Class RedisfactoryConfig {@autowired Private Environment Umgebung; @Bean public redisconnectionFactory mylettucEconnectionFactory () {map <String, Objekt> 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 (neuer MappropertySource ("RedisclusterConfiguration", Quelle)); Neue LettuceConnectionFactory (RedisclusterConfiguration) zurückgeben; }}4. Führen Sie Tests durch
@SpringBoottest@Runwith (Springrunner.Class) öffentliche Klasse RedisconfigurationTest {@autowiredPrivate redistemplate redistemplate; @TestPublic void redistemplate () löst Ausnahme aus {redistemplate.opsforvalue (). 5. Überprüfen Sie, ob der Redis -Desktop -Manager eine Verbindung zum Redis -Knoten herstellt, und überprüfen Sie, ob die Daten im Inneren als Autor vorhanden sind. Das Folgende zeigt es und es erweist sich als erfolgreich.
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.