修改redis序列化
This commit is contained in:
parent
db84d796c6
commit
52fa8239a7
|
|
@ -4,15 +4,13 @@ import org.springframework.data.redis.connection.RedisConnection;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
import org.springframework.data.redis.connection.jedis.JedisConnection;
|
import org.springframework.data.redis.connection.jedis.JedisConnection;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义RedisTemplate,支持选库
|
* 自定义RedisTemplate,支持选库
|
||||||
*
|
*
|
||||||
* @author gxx
|
* @author gxx
|
||||||
*/
|
*/
|
||||||
@Component
|
public class GridntRedisTemplate<K, V> extends StringRedisTemplate {
|
||||||
public class GridntRedisTemplate extends StringRedisTemplate {
|
|
||||||
|
|
||||||
public GridntRedisTemplate(RedisConnectionFactory connectionFactory) {
|
public GridntRedisTemplate(RedisConnectionFactory connectionFactory) {
|
||||||
super.setConnectionFactory(connectionFactory);
|
super.setConnectionFactory(connectionFactory);
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,10 @@ package com.ruoyi.common.redis.configure;
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
|
||||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
|
|
@ -20,32 +18,16 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableCaching
|
@EnableCaching
|
||||||
public class RedisConfig extends CachingConfigurerSupport {
|
public class RedisConfig extends CachingConfigurerSupport {
|
||||||
@Value("${spring.redis.host}")
|
|
||||||
private String host;
|
|
||||||
|
|
||||||
@Value("${spring.redis.port}")
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LettuceConnectionFactory lettuceConnectionFactory() {
|
|
||||||
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
|
|
||||||
redisStandaloneConfiguration.setHostName(host);
|
|
||||||
redisStandaloneConfiguration.setPort(port);
|
|
||||||
|
|
||||||
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration);
|
|
||||||
// 关闭共享连接
|
|
||||||
factory.setShareNativeConnection(false);
|
|
||||||
return factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public GridntRedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory)
|
public GridntRedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory)
|
||||||
{
|
{
|
||||||
GridntRedisTemplate template = new GridntRedisTemplate(connectionFactory);
|
// 关闭共享连接
|
||||||
|
connectionFactory.setShareNativeConnection(false);
|
||||||
|
GridntRedisTemplate<Object, Object> template = new GridntRedisTemplate(connectionFactory);
|
||||||
template.setConnectionFactory(connectionFactory);
|
template.setConnectionFactory(connectionFactory);
|
||||||
|
|
||||||
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
|
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||||
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package com.ruoyi.common.redis.service;
|
package com.ruoyi.common.redis.service;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.data.redis.core.*;
|
import org.springframework.data.redis.core.*;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
|
@ -18,7 +17,6 @@ import java.util.concurrent.TimeUnit;
|
||||||
public class RedisService
|
public class RedisService
|
||||||
{
|
{
|
||||||
@Autowired
|
@Autowired
|
||||||
@Qualifier("gridntRedisTemplate")
|
|
||||||
public RedisTemplate redisTemplate;
|
public RedisTemplate redisTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue