[由零开始]Spring boot 整合redis集群

Spring boot 整合redis集群

一、环境搭建

Redis集群环境搭建:https://blog.csdn.net/qq497811258/article/details/108124697

二、创建Spring boot项目

1.创建boot项目

[由零开始]Spring boot 整合redis集群_第1张图片

[由零开始]Spring boot 整合redis集群_第2张图片

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

}

5.验证

[由零开始]Spring boot 整合redis集群_第3张图片
在这里插入图片描述

你可能感兴趣的:(redis,redis)