SpringBoot 整合单机 Redis 和 Redis 集群

目录

一 pom 依赖

二 整合单机 Redis 

2.1 使用 jedis 连接池

2.2 使用 lettuce 连接池

二 整合 Redis 集群

2.1 使用 jedis 连接池

3.2 使用 lettuce 连接池


一 pom 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

二 整合单机 Redis 

2.1 使用 jedis 连接池

application.yml 配置

spring:
  redis:
    host: 127.0.0.1
    database: 0
    port: 6379
    timeout: 1000
    jedis:
      pool:
        max-active: 256
        max-idle: 8
        min-idle: 1
        max-wait: 100

RedisConfig 配置 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

}

使用

扫描二维码关注公众号,回复: 9074976 查看本文章
@Autowired
private RedisTemplate<String, Object> redisTemplate;

2.2 使用 lettuce 连接池

application.yml 配置

spring:
  redis:
    host: 127.0.0.1
    database: 0
    port: 6379
    timeout: 1000
    lettuce:
      pool:
        max-active: 256
        max-idle: 8
        min-idle: 1
        max-wait: 100

RedisConfig 配置

import org.springframework.beans.factory.annotation.Autowired;
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.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Autowired
    private LettuceConnectionFactory lettuceConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

}

使用

@Autowired
private RedisTemplate<String, Object> redisTemplate;

二 整合 Redis 集群

2.1 使用 jedis 连接池

application.yml 配置

spring:
  redis:
    cluster:
      nodes:
      - 127.0.0.1:7001
      - 127.0.0.1:7002
      - 127.0.0.1:7003
    jedis:
      pool:
        min-idle: 8
        max-wait: 100
        max-idle: 1
        max-active: 256

RedisConfig 配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

}

使用

@Autowired
private RedisTemplate<String, Object> redisTemplate;

3.2 使用 lettuce 连接池

application.yml 配置

spring:
  redis:
    cluster:
      nodes:
      - 127.0.0.1:7001
      - 127.0.0.1:7002
      - 127.0.0.1:7003
    lettuce:
      pool:
        min-idle: 8
        max-wait: 100
        max-idle: 1
        max-active: 256

RedisConfig 配置

import org.springframework.beans.factory.annotation.Autowired;
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.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Autowired
    private LettuceConnectionFactory lettuceConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

}

使用

@Autowired
private RedisTemplate<String, Object> redisTemplate;
发布了67 篇原创文章 · 获赞 64 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/jack1liu/article/details/102732919
今日推荐