spring cloud中redis的多数据源和序列化、反序列化无法跨服务的问题解决
第一步是对于redis多数据源的处理
redis:
host: 192.168.7.162
port: 30379
password:
# 连接超时时间(毫秒)
timeout: 10000
jedis:
pool:
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 10
# 连接池最大连接数(使用负值表示没有限制)
max-active: 100
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
database: 0
#sentinel:
#master: mymaster # 哨兵监听的Redis server的名称
#nodes: 127.0.0.1:26379,127.0.0.1:26479,127.0.0.1:26579 #哨兵的配置列表
domainRedis:
host: 192.168.7.162
port: 30379
password:
# 连接超时时间(毫秒)
timeout: 10000
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 10
# 连接池最大连接数(使用负值表示没有限制)
max-active: 100
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
database: 15
在配置文件中就有了domainRedis的连接参数
package com.bbg.domainManager.common.redis;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
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.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* redis配置类
*redisTemplate 默认jdk序列方式,用来保存对象等
* stringRedisTemplate 默认string的序列化方式,用于存储string格式
* @author zcc ON 2018/3/19
**/
@Configuration
@EnableCaching//开启注解
public class RedisConfig extends CachingConfigurerSupport {
/**
* 数据源2 redis template
*/
@Bean(name = "domainRedis")
public RedisTemplate redisTemplate(
@Value("${spring.domainRedis.database}") int database,
@Value("${spring.domainRedis.timeout}") long timeout,
@Value("${spring.domainRedis.max-active}") int maxActive,
@Value("${spring.domainRedis.max-wait}") int maxWait,
@Value("${spring.domainRedis.max-idle}") int maxIdle,
@Value("${spring.domainRedis.min-idle}") int minIdle,
@Value("${spring.domainRedis.host}") String hostName,
@Value("${spring.domainRedis.port}") int port,
@Value("${spring.domainRedis.password}") String password) {
/* ========= 基本配置 ========= */
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(hostName);
configuration.setPort(port);
configuration.setDatabase(database);
if (!ObjectUtils.isEmpty(password)) {
RedisPassword redisPassword = RedisPassword.of(password);
configuration.setPassword(redisPassword);
}
/* ========= 连接池通用配置 ========= */
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxTotal(maxActive);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMaxWaitMillis(maxWait);
/* ========= jedis pool ========= */
/*
JedisClientConfiguration.DefaultJedisClientConfigurationBuilder builder = (JedisClientConfiguration.DefaultJedisClientConfigurationBuilder) JedisClientConfiguration
.builder();
builder.connectTimeout(Duration.ofSeconds(timeout));
builder.usePooling();
builder.poolConfig(genericObjectPoolConfig);
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration, builder.build());
// 连接池初始化
connectionFactory.afterPropertiesSet();
*/
/* ========= lettuce pool ========= */
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
builder.poolConfig(genericObjectPoolConfig);
builder.commandTimeout(Duration.ofSeconds(timeout));
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build());
connectionFactory.afterPropertiesSet();
/* ========= 创建 template ========= */
return createRedisTemplate(connectionFactory);
}
/**
* json 实现 redisTemplate
* <p>
* 该方法不能加 @Bean 否则不管如何调用,connectionFactory都会是默认配置
*
* @param redisConnectionFactory
* @return
*/
public RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
//调用自己写的StringJackson2JsonSerializer来序列化,value中存储的是json
StringJackson2JsonSerializer<Object> jackson2JsonRedisSerializer = new StringJackson2JsonSerializer<Object>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/*
Redis 连接池: Lettuce、Jedis 比较
Jedis:
Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接
Lettuce:
Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
lettuce主要利用netty实现与redis的同步和异步通信。
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheManager cacheManager = RedisCacheManager.create(factory);
return cacheManager;
}
// 以下两种redisTemplate自由根据场景选择
// @Bean
// public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
// RedisTemplate<Object, Object> template = new RedisTemplate<>();
// template.setConnectionFactory(connectionFactory);
//
// //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
// Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
//
// ObjectMapper mapper = new ObjectMapper();
// mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
// serializer.setObjectMapper(mapper);
//
// template.setValueSerializer(serializer);
// //使用StringRedisSerializer来序列化和反序列化redis的key值
// template.setHashKeySerializer(serializer);
// template.setHashValueSerializer(serializer);
// template.setKeySerializer(new StringRedisSerializer());
// template.afterPropertiesSet();
// return template;
// }
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(factory);
return stringRedisTemplate;
}
}
然后再redis的方法中去引用,这里你如果配置了多个redis数据源,就写多个这样的方法吧,应该还有更加好的方法,只是我没有找到
package com.bbg.domainManager.common.redis; /**
* @Title: ${file_name}
* @Package ${package_name}
* @Description: ${todo}
* @author xwq
* @date 2019/7/15 001516:58
*/
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class RedisUtils {
// @Autowired
// private RedisTemplate redisTemplate;
@Resource(name = "domainRedis")
private RedisTemplate redisTemplate;
public RedisUtils(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 写入缓存
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存设置时效时间
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 批量删除对应的value
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 批量删除key
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern+"*");
if (keys.size() > 0)
redisTemplate.delete(keys);
}
/**
* 删除对应的value
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 读取缓存
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 哈希 添加
* @param key
* @param hashKey
* @param value
*/
public void hmSet(String key, Object hashKey, Object value){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key,hashKey,value);
}
/**
* 哈希获取数据
* @param key
* @param hashKey
* @return
*/
public Object hmGet(String key, Object hashKey){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key,hashKey);
}
/**
* 列表添加
* @param k
* @param v
*/
public void lPush(String k,Object v){
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k,v);
}
/**
* 列表获取
* @param k
* @param l
* @param l1
* @return
*/
public List<Object> lRange(String k, long l, long l1){
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k,l,l1);
}
/**
* 集合添加
* @param key
* @param value
*/
public void add(String key,Object value){
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key,value);
}
/**
* 集合获取
* @param key
* @return
*/
public Set<Object> setMembers(String key){
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}
/**
* 有序集合添加
* @param key
* @param value
* @param scoure
*/
public void zAdd(String key,Object value,double scoure){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key,value,scoure);
}
/**
* 有序集合获取
* @param key
* @param scoure
* @param scoure1
* @return
*/
public Set<Object> rangeByScore(String key,double scoure,double scoure1){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
}
这样下来redis的多数据源已经配置成功了,
然后出现新问题,我在A项目中存储的redis,B项目取出来,会报错
B项目中是没有com.bbg.domainManager.dto.DomainDto这个包的,所以,这个时候存储不能直接序列化,需要存储value为json
package com.bbg.domainManager.common.redis; /**
* @Title: ${file_name}
* @Package ${package_name}
* @Description: ${todo}
* @author xwq
* @date 2019/11/13 001310:28
*/
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
/**
*
* @author xwq
* @create 2019-11-13 10:28
**/
public class StringJackson2JsonSerializer<T> extends Jackson2JsonRedisSerializer<T> {
private ObjectMapper objectMapper = new ObjectMapper();
public StringJackson2JsonSerializer(Class<T> type) {
super(type);
// TODO Auto-generated constructor stub
}
public byte[] serialize(Object t) throws SerializationException {
if (t == null) {
return new byte[0];
}
try {
//将对象转为Json String然后再序列化,方便跨服务 JSON.toJSONString(
return this.objectMapper.writeValueAsBytes(JSON.toJSONString(t));
// return this.objectMapper.writeValueAsBytes(JacksonUtil.objToJson(t));
} catch (Exception ex) {
throw new SerializationException("Could not write JSON: " + ex.getMessage(), ex);
}
}
}
StringJackson2JsonSerializer类中直接就覆写了序列号的方法
这样,redis中存储的格式为
我在B项目中怎么去取出来呢
domainDto = JSONObject.toJavaObject(JSONObject.parseObject(String.valueOf(redisUtils.get("sys_domain:"+key))),DomainDto.class);
这样就可以了。