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", "liuyi@163.com");
        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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值