Spring boot 整合redis集群

Spring boot 整合redis集群

一、环境搭建

Redis集群环境搭建:https://editor.csdn.net/md/?articleId=108124697

二、创建Spring boot项目
1.创建boot项目

在这里插入图片描述

在这里插入图片描述

2.创建配置文件
spring:
  redis:
    database: 0 # Redis数据库索引(默认为0)
    timeout: 100000 # 连接超时时间(毫秒)
    #    jedis:
    #      pool:
    #        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
    #        max-idle: 8 # 连接池中的最大空闲连接
    #        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
    #        min-idle: 0 # 连接池中的最小空闲连接
    cluster:
      nodes:
        - 10.1.194.193:6379
        - 10.1.194.193:6380
        - 10.1.194.193:6381
        - 10.1.194.193:6382
        - 10.1.194.193:6383
        - 10.1.194.193:6384
      connectionTimeout: 6000
      soTimeout: 6000
      maxAttempts: 5
   # password:

3.初始化RedisTemplate
package com.lagou.demo.config;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory factory){
        RedisTemplate<String,Object> template=new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        //value采用jackson序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //hash的key采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        //hash的value采用String的序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

4.编写测试代码

我这边使用了2种方式调用

package com.lagou.demo;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;


@SpringBootTest
@RunWith(SpringRunner.class)
class DemoApplicationTests {



    @Autowired
    private RedisTemplate redisTemplate;


    @Test
    void get() {
        redisTemplate.opsForValue().set("sex","nan");
        System.out.println(redisTemplate.opsForValue().get("name"));
        System.out.println("------------------------------------------------------");
        JedisCluster jcd = null;
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            Set<HostAndPort> jedisClusterNode = new HashSet<>();
            jedisClusterNode.add(new HostAndPort("10.1.194.193", 6379));
            jedisClusterNode.add(new HostAndPort("10.1.194.193", 6380));
            jedisClusterNode.add(new HostAndPort("10.1.194.193", 6381));
            jedisClusterNode.add(new HostAndPort("10.1.194.193", 6382));
            jedisClusterNode.add(new HostAndPort("10.1.194.193", 6383));
            jedisClusterNode.add(new HostAndPort("10.1.194.193", 6384));
            jedisClusterNode.add(new HostAndPort("10.1.194.193", 6385));
            jedisClusterNode.add(new HostAndPort("10.1.194.193", 6386));
            jcd = new JedisCluster(jedisClusterNode, config);
            //jcd.set("name", "zhang3");
            String value = jcd.get("sex");
            System.out.println("get val from redis cluster: " + value);
        } finally {
            if (jcd != null){
                jcd.close();
            }
        }
    }



}

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值