springboot整合redis,用spring-data-redis2.0的同时用jedis

最新版本的spring-data-redis很烦,很多以前用起来很方便的方法过时了。下面是2.0这个版本的搭建过程:

首先要了解这个版本

        (1)spring-data-redis是默认是用lettuce的,要用jedis需要单独出来配置

 

        (2)JedisConnectionFactory的所有set方法全部过时。

        (3)基本配置(host,port...)用redisstandaloneconfiguration,poo连接池这样的配置用jedispoolconfig

        (4)有自带的序列化类的,不需要自定义了

下面就贴出代码,注意这仅适用于jedis,如果不用jedis,用lettuce基本上啥也不用配置

pom.xml

 


 

 

 

 

    redis.clients

 

 

    jedis

 

 

 

 

 

 

 

 

 

 

 

    org.springframework.boot

 

 

 

    spring-boot-starter-data-redis

 

 

 

 

 

 

这个编辑器是真的难用。。。

application.yml

spring:
  redis:
    database: 5
    host: 127.0.0.1
    port: 6379
    timeout: 3000ms
    jedis:
      pool:
        max-idle: 500
        min-idle: 50
        max-active: 2000
        max-wait: 1000ms

jedisConfig.java

package cn.cnm.springboot;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties
public class JedisConfig {
    @Value("${spring.redis.host}")
    public String host;
    @Value("${spring.redis.port}")
    public int port;
    @Value("${spring.redis.database}")
    public int database;
    @Value("${spring.redis.jedis.pool.max-idle}")
    public int maxIdle;
    @Value("${spring.redis.jedis.pool.min-idle}")
    public int minIdle;
    @Value("${spring.redis.jedis.pool.max-active}")
    public int maxActive;
    @Value("${spring.redis.jedis.pool.max-wait}")
    public String maxWait;
    @Value("${spring.redis.timeout}")
    public String timeout;

}

RedisConfig.java

package cn.cnm.springboot;

import cn.cnm.springboot.util.MyRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.JedisClientConfigurationBuilderCustomizer;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Autowired
    JedisConfig jedisConfig;
    @Autowired
    JedisConnectionFactory jedisConnectionFactory;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory (){
        RedisStandaloneConfiguration rf=new RedisStandaloneConfiguration();
        rf.setDatabase(jedisConfig.database);
        rf.setHostName(jedisConfig.host);
        rf.setPort(jedisConfig.port);
        int to=Integer.parseInt(jedisConfig.timeout.substring(0,jedisConfig.timeout.length()-2));
        //JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
        //jedisClientConfiguration.connectTimeout(Duration.ofMillis(to));
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpb=
                (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(jedisConfig.maxIdle);
        jedisPoolConfig.setMinIdle(jedisConfig.minIdle);
        jedisPoolConfig.setMaxTotal(jedisConfig.maxActive);
        int l=Integer.parseInt(jedisConfig.maxWait.substring(0,jedisConfig.maxWait.length()-2));
        jedisPoolConfig.setMaxWaitMillis(l);
        jpb.poolConfig(jedisPoolConfig);
        JedisConnectionFactory jedisConnectionFactory=new JedisConnectionFactory(rf,jpb.build());
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate redisTemplate(){
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate rt=new RedisTemplate();
        rt.setConnectionFactory(jedisConnectionFactory);
        System.out.println(jedisConnectionFactory.getPoolConfig().getMaxIdle());
        System.out.println(jedisConnectionFactory.getPoolConfig().getMinIdle());
        System.out.println(jedisConnectionFactory.getPoolConfig().getMaxTotal());
        RedisSerializer rs=new StringRedisSerializer();
        rt.setKeySerializer(rs);
        rt.setValueSerializer(jackson2JsonRedisSerializer);
        rt.setHashKeySerializer(rs);
        rt.setHashValueSerializer(jackson2JsonRedisSerializer);
        rt.afterPropertiesSet();
        return rt;

    }
}

1.要注意用pool后,设置timeout就没有意义了

2.JedisPoolingClientConfigurationBuilder的poolConfig方法接收的参数是GenericObjectPoolConfig,用GenericObjectPoolConfig也可以设置pool相关的参数,之所以用JedisPoolConfig (继承了GenericObjectPoolConfig)是因为官方文档建议用。

3.序列化key用StringRedisSerializer value用Jackson2JsonRedisSerializer 

4.如果要用RedisStringTemplate的话,可以把 RedisTemplate 的注册代码抽出来单独放到一个方法里

后面就是测试,封装RedisTemplate的过程了,这些我就不说了。

需要注意的是以上只是需要自己定制配置时才需要写的代码,如果不需要做任何自我配置 ,则只需要在yml文件中写好配置即可,redistemplate会自动注册,只是有时候可能需要多个redistemplate这个时候就需要自己额外配置了

 

 

 

 

 

你可能感兴趣的:(SpingBoot)