Обязательное условие: построить окружающую среду Redis Cluster. Пожалуйста, смотрите метод строительства: https://www.vevb.com/article/143749.htm
1. Создайте новый проект и добавьте поддержку Redis в файл pom.xml
<Depective> <groupid> org.springframework.boot </GroupId> <artifactid> Spring-boot-starter-data-redis </artifactid> </deperency>
2. Настройте Application.properties
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: 6385spring.redis.cluster.timeout = 1000spring.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 (новый stringRedisserializer ()); Template.SetValueSerializer (новый genericjackson2jsonredisserializer ()); Template.SetConnectionFactory (MyletTuceConnectionFactory); возвратный шаблон; }} @ConfigurationPublic Class RedIsfactoryConfig {@Autowired частная среда; @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 reSiSclusterConfiguration; rediSclusterConfiguration = new RedisclusterConfiguration (New MapPropertySource ("rediSclusterConfiguration", Source)); вернуть новый LettuceConnectionFactory (rediSclusterConfiguration); }}4. Выполните тесты
@Springboottest@runwith (springrunner.class) открытый класс RedisconfigurationTest {@autowiredPrivate Redistemplate Redistemplate; @testpublic void redistemplate () бросает исключение {redistemplate.opforvalue (). Set («Автор», «damein_xym»); 5. Убедитесь, что используйте Redis Desktop Manager для подключения к узлу Redis и проверьте, существуют ли данные внутри в качестве автора. Следующее показывает это, и это оказывается успешным.
Выше всего содержание этой статьи. Я надеюсь, что это будет полезно для каждого обучения, и я надеюсь, что все будут поддерживать Wulin.com больше.