View Javadoc
1   package top.infra.cloudready.boot;
2   
3   import lombok.extern.slf4j.Slf4j;
4   
5   import org.redisson.Redisson;
6   import org.redisson.api.RedissonClient;
7   import org.redisson.config.Config;
8   import org.redisson.config.SentinelServersConfig;
9   import org.redisson.config.SingleServerConfig;
10  import org.springframework.beans.factory.annotation.Autowired;
11  import org.springframework.beans.factory.annotation.Value;
12  import org.springframework.boot.autoconfigure.AutoConfigureAfter;
13  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
14  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
15  import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
16  import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
17  import org.springframework.context.annotation.Bean;
18  import org.springframework.context.annotation.Configuration;
19  import org.springframework.data.redis.core.RedisTemplate;
20  
21  @AutoConfigureAfter(RedisAutoConfiguration.class)
22  @ConditionalOnClass({Redisson.class, RedisTemplate.class})
23  @ConditionalOnProperty(name = {"spring.redis.host", "spring.redis.port"})
24  @Configuration
25  @Slf4j
26  public class RedissonAutoConfiguration {
27  
28    private RedisProperties redisProperties;
29  
30    @Value("${spring.redis.host:127.0.0.1}:${spring.redis.port:6379}")
31    private String address;
32    @Value("${spring.redis.database:0}")
33    private int database;
34  
35    @Autowired(required = false)
36    public void setRedisProperties(final RedisProperties redisProperties) {
37      this.redisProperties = redisProperties;
38    }
39  
40    public String getRedisAddress() {
41      //should add schema prefix "redis://" or "rediss://" for redisson version >= 3.7.0
42      final String schema = this.redisProperties.isSsl() ? "rediss://" : "redis://";
43      final String redisAddress = schema + this.redisProperties.getHost() + ":" + this.redisProperties.getPort();
44      log.info("redisAddress: {}", redisAddress);
45      return redisAddress;
46    }
47  
48    private SentinelServersConfig sentinelServers(final Config config) {
49      final int redisDatabase = this.redisProperties.getDatabase();
50      final String redisPassword = this.redisProperties.getPassword();
51      final String redisSentinelMaster = this.redisProperties.getSentinel().getMaster();
52      final String[] redisSentinelNodes = this.redisProperties.getSentinel().getNodes().split(",");
53  
54      SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
55      sentinelServersConfig.setMasterName(redisSentinelMaster);
56      sentinelServersConfig.addSentinelAddress(redisSentinelNodes);
57      sentinelServersConfig.setDatabase(redisDatabase);
58      if (redisPassword != null) { //StringUtils.isNotBlank(redisPassword)
59        sentinelServersConfig.setPassword(redisPassword);
60      }
61  
62      return sentinelServersConfig;
63    }
64  
65    private SingleServerConfig singleServer(final Config config) {
66      final SingleServerConfig singleServerConfig = config.useSingleServer();
67  
68      final String redisAddress = this.getRedisAddress();
69      final int redisDatabase = this.redisProperties.getDatabase();
70      final String redisPassword = this.redisProperties.getPassword();
71  
72      singleServerConfig.setAddress(redisAddress).setDatabase(redisDatabase);
73      if (redisPassword != null) { //StringUtils.isNotBlank(redisPassword)
74        singleServerConfig.setPassword(redisPassword);
75      }
76  
77      return singleServerConfig;
78    }
79  
80    @Bean
81    public RedissonClient redissonClient() {
82      final Config config = new Config();
83  
84      final boolean sentinel = this.redisProperties.getSentinel() != null;
85      if (sentinel) {
86        this.sentinelServers(config);
87      } else {
88        this.singleServer(config);
89      }
90  
91      return Redisson.create(config);
92    }
93  }