|
| 1 | +package com.example.redisson; |
| 2 | + |
| 3 | +import cn.hutool.core.util.StrUtil; |
| 4 | +import org.redisson.Redisson; |
| 5 | +import org.redisson.api.RedissonClient; |
| 6 | +import org.redisson.config.Config; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
| 10 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | + |
| 14 | +/** |
| 15 | + * RedissonConfig配置 |
| 16 | + * |
| 17 | + * @author wliduo[i@dolyw.com] |
| 18 | + * @date 2020/8/14 16:16 |
| 19 | + */ |
| 20 | +// @Configuration |
| 21 | +public class RedissonConfigNew { |
| 22 | + |
| 23 | + private static final Logger logger = LoggerFactory.getLogger(RedissonConfig.class); |
| 24 | + |
| 25 | + @Value("${spring.redis.hostName}") |
| 26 | + private String host; |
| 27 | + |
| 28 | + @Value("${spring.redis.port}") |
| 29 | + private int port; |
| 30 | + |
| 31 | + @Value("${spring.redis.password}") |
| 32 | + private String password; |
| 33 | + |
| 34 | + @Value("${spring.redis.database}") |
| 35 | + private int database; |
| 36 | + |
| 37 | + /** |
| 38 | + * RedissonClient单机配置 |
| 39 | + * |
| 40 | + * @param |
| 41 | + * @return org.redisson.api.RedissonClient |
| 42 | + * @throws |
| 43 | + * @author wliduo[i@dolyw.com] |
| 44 | + * @date 2020/8/14 16:25 |
| 45 | + */ |
| 46 | + @Bean |
| 47 | + @ConditionalOnProperty(name = {"spring.redis.cluster"}, havingValue = "false") |
| 48 | + public RedissonClient redissonClientSingle() { |
| 49 | + RedissonClient redissonClient = null; |
| 50 | + Config config = new Config(); |
| 51 | + String url = "redis://" + host + ":" + port; |
| 52 | + // 单节点配置 |
| 53 | + config.useSingleServer().setAddress(url).setDatabase(database); |
| 54 | + if (StrUtil.isNotBlank(password)) { |
| 55 | + config.useSingleServer().setPassword(password); |
| 56 | + } |
| 57 | + try { |
| 58 | + redissonClient = Redisson.create(config); |
| 59 | + return redissonClient; |
| 60 | + } catch (Exception e) { |
| 61 | + logger.error("RedissonClient Init Url:[{}], Exception: {}", url, e); |
| 62 | + return null; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Value("${spring.redis.cluster.nodes}") |
| 67 | + private String cluster; |
| 68 | + |
| 69 | + /** |
| 70 | + * RedissonClient集群配置 |
| 71 | + * |
| 72 | + * @param |
| 73 | + * @return org.redisson.api.RedissonClient |
| 74 | + * @throws |
| 75 | + * @author wliduo[i@dolyw.com] |
| 76 | + * @date 2020/8/14 16:25 |
| 77 | + */ |
| 78 | + @Bean |
| 79 | + @ConditionalOnProperty(name = {"spring.redis.cluster"}, havingValue = "true") |
| 80 | + public RedissonClient redissonClientCluster() { |
| 81 | + RedissonClient redissonClient = null; |
| 82 | + Config config = new Config(); |
| 83 | + // 集群状态扫描间隔时间,单位是毫秒 |
| 84 | + config.useClusterServers().setScanInterval(2000); |
| 85 | + if (StrUtil.isNotBlank(password)) { |
| 86 | + config.useClusterServers().setPassword(password); |
| 87 | + } |
| 88 | + String[] serverArray = cluster.split(","); |
| 89 | + for (String ipPort : serverArray) { |
| 90 | + String[] ipAndPort = ipPort.split(":"); |
| 91 | + String url = "redis://" + ipAndPort[0].trim() + ":" + ipAndPort[1].trim(); |
| 92 | + config.useClusterServers().addNodeAddress(url); |
| 93 | + } |
| 94 | + try { |
| 95 | + redissonClient = Redisson.create(config); |
| 96 | + return redissonClient; |
| 97 | + } catch (Exception e) { |
| 98 | + logger.error("RedissonClient init redis url:[{}], Exception:", cluster, e); |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments