国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Springboot2.X集成redis集群(Lettuce)連接的方法

2024-07-14 08:41:33
字體:
來源:轉載
供稿:網友

1. 新建工程,pom.xml文件中添加redis支持

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

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(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. 執行測試

@SpringBootTest@RunWith(SpringRunner.class)public class RedisConfigurationTest {  @Autowiredprivate RedisTemplate redisTemplate;@Testpublic void redisTemplate() throws Exception {    redisTemplate.opsForValue().set("author", "Damein_xym");}}

5. 驗證,使用Redis Desktop Manager 連接redis節點,查看里面的數據是否存在author,有如下顯示,證明成功。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 常宁市| 陆良县| 雅江县| 贵州省| 兴化市| 东辽县| 兴宁市| 安达市| 大港区| 贵南县| 浑源县| 从化市| 兴山县| 陆川县| 鞍山市| 珲春市| 广元市| 南漳县| 蛟河市| 五常市| 太仆寺旗| 陵水| 馆陶县| 岑溪市| 容城县| 泰来县| 天全县| 读书| 杭锦后旗| 沂源县| 佛冈县| 栾城县| 高淳县| 即墨市| 咸丰县| 都安| 苏尼特右旗| 齐齐哈尔市| 石河子市| 临颍县| 隆安县|