Skip to content

Commit

Permalink
优化redisson配置
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGaoRobot committed Feb 11, 2022
1 parent 96b5b50 commit acb88a6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 110 deletions.
21 changes: 0 additions & 21 deletions buyer-api/src/main/resources/redisson.yaml

This file was deleted.

21 changes: 0 additions & 21 deletions common-api/src/main/resources/redisson.yaml

This file was deleted.

21 changes: 0 additions & 21 deletions consumer/src/main/resources/redisson.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package cn.lili.cache.config.redis;

import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -20,7 +25,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
Expand All @@ -31,9 +35,10 @@
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -54,6 +59,9 @@ public class RedisConfig extends CachingConfigurerSupport {
@Value("${lili.cache.timeout:7200}")
private Integer timeout;

@Autowired
private RedisProperties redisProperties;


/**
* 当有多个管理器的时候,必须使用该注解在一个管理器上注释:表示该管理器为默认的管理器
Expand Down Expand Up @@ -105,9 +113,41 @@ public RedisTemplate<Object, Object> redisTemplate(LettuceConnectionFactory lett
}

@Bean(destroyMethod = "shutdown")
public RedissonClient redisson() throws IOException {
return Redisson.create(
Config.fromYAML(new ClassPathResource("redisson.yaml").getInputStream()));
public RedissonClient redisson() {
Config config = new Config();

if (redisProperties.getSentinel() != null && !redisProperties.getSentinel().getNodes().isEmpty()) {
// 哨兵模式
SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster());
List<String> sentinelAddress = new ArrayList<>();
for (String node : redisProperties.getCluster().getNodes()) {
sentinelAddress.add("redis://" + node);
}
sentinelServersConfig.setSentinelAddresses(sentinelAddress);
if (CharSequenceUtil.isNotEmpty(redisProperties.getSentinel().getPassword())) {
sentinelServersConfig.setSentinelPassword(redisProperties.getSentinel().getPassword());
}
} else if (redisProperties.getCluster() != null && !redisProperties.getCluster().getNodes().isEmpty()) {
// 集群模式
ClusterServersConfig clusterServersConfig = config.useClusterServers();
List<String> clusterNodes = new ArrayList<>();
for (String node : redisProperties.getCluster().getNodes()) {
clusterNodes.add("redis://" + node);
}
clusterServersConfig.setNodeAddresses(clusterNodes);
if (CharSequenceUtil.isNotEmpty(redisProperties.getPassword())) {
clusterServersConfig.setPassword(redisProperties.getPassword());
}
} else {
SingleServerConfig singleServerConfig = config.useSingleServer();
singleServerConfig.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort());
if (CharSequenceUtil.isNotEmpty(redisProperties.getPassword())) {
singleServerConfig.setPassword(redisProperties.getPassword());
}
}

return Redisson.create(config);
}

/**
Expand Down
21 changes: 0 additions & 21 deletions manager-api/src/main/resources/redisson.yaml

This file was deleted.

21 changes: 0 additions & 21 deletions seller-api/src/main/resources/redisson.yaml

This file was deleted.

0 comments on commit acb88a6

Please sign in to comment.