SpringBoot 2.1.x整合Redis Cluster集群配置相关示例

资源相关

  • Redis Cluster集群搭建【推荐 】

相关代码示例

  • pom.xml
   
   <dependency>
       <groupId>org.springframework.bootgroupId>
       <artifactId>spring-boot-starter-data-redisartifactId>
   dependency>

   <dependency>
       <groupId>org.projectlombokgroupId>
       <artifactId>lombokartifactId>
   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 redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        Jackson2JsonRedisSerializer 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);
    }

你可能感兴趣的:(SpringBoot)