SpringBoot 2.1.x整合Redis Cluster集羣配置相關示例

資源相關

相關代碼示例

  • pom.xml
   <!--Redis模塊-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
   </dependency>

   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
   </dependency>
  • application.yml
spring:
  application:
    name: boot-redis
  redis:
    #哨兵配置
    sentinel:
      nodes: 192.168.44.121:26379
      master: hs6379
    #Redis Cluster集羣節點配置
    cluster:
      nodes: 192.168.44.121:6380,192.168.44.121:6379,192.168.44.121:6381,192.168.44.121:6382
    #Redis 默認數據庫設置
    database: 0
    #超時時間
    timeout: 6000
    jedis:
      pool:
        #最大連接數
        max-active: 1000
        #連接池最大等待時間
        max-wait: -1ms
        #最大空閒數
        max-idle: 50
        #最小空閒
        min-idle: 0
  • IsRedisTempleConfig.java
package com.hf.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: RedisTempleConfig
 * @Author: hf
 * @Date: 2019/9/29 2:41
 * @Description: RedisTemple 模板配置
 */
@Configuration
public class IsRedisTempleConfig {


    //設置序列化規則
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

        //key使用StringRedisSerializer序列化 value使用jackson2JsonRedisSerializer序列化
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        // Hash結構設置
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        return redisTemplate;
    }
}

  • 測試相關
 /**
     * Redis常見的五大數據類型
     * String(字符串)、List(列表)、Set(集合)、Hash(散列)、ZSet(有序集合)
     * stringRedisTemplate.opsForValue()[String(字符串)]
     * stringRedisTemplate.opsForList()[List(列表)]
     * stringRedisTemplate.opsForSet()[Set(集合)]
     * stringRedisTemplate.opsForHash()[Hash(散列)]
     * stringRedisTemplate.opsForZSet()[ZSet(有序集合)]
     */

    //k-v都是對象
    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    //k-v都是字符串
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Test
    public void test_set_06() {
        //取值
        redisTemplate.opsForSet().add("set01", "111", "222", "333", "444", "555", "666");

        //取值
        Set<Object> set01 = redisTemplate.opsForSet().members("set01");
        System.out.println(set01);
    }

    @Test
    public void test_hash_04() {
        //設值
        redisTemplate.boundHashOps("userList").put("user_k1", new User(1, "測試用戶", "nopwd", "noEmail"));

        //取值
        Object obj = redisTemplate.boundHashOps("userList").get("user_k1");
        System.out.println(obj);
    }

    @Test
    public void test_obj_03() {
        //設置保存對象
        User user = new User(1, "劉一", "liuyi", "[email protected]");
        redisTemplate.opsForValue().set("user-01", user);

        //獲取對象
        Object obj = redisTemplate.opsForValue().get("user-01");
        System.out.println(obj);
    }

    @Test
    public void test_list_02() {
        //設置列表值
        stringRedisTemplate.opsForList().leftPushAll("list01", Arrays.asList("11111", "22222", "33333", "44444"));

        //獲取
        List<String> list01 = stringRedisTemplate.opsForList().range("list01", 0, -1);
        System.out.println("Redis中獲取list列表的值:" + list01);
    }

    @Test
    public void test_str_01() {

        //設置值
        stringRedisTemplate.opsForValue().set("k2", "我是k2的值");

        //設置帶有過期時間的key
        stringRedisTemplate.opsForValue().set("k1", "Hello", 20, TimeUnit.SECONDS);

        //刪除key
        //stringRedisTemplate.delete("k");

        //讀取值
        String str1 = stringRedisTemplate.opsForValue().get("k1");
        String str2 = stringRedisTemplate.opsForValue().get("k2");
        System.out.println("Redis獲取到k1值:" + str1);
        System.out.println("Redis獲取到k2值:" + str2);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章