通用框架平台,每个分支对应子通用框架平台,禁止Merge不同分支!! 分支版本区别见项目内readme.md
whycxzp
2024-01-10 9a2b1251fc48874b76d3b02dbfc306698325dfeb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.whyc.config;
 
import com.whyc.properties.RedisClusterProperties;
import com.whyc.properties.RedisPoolProperties;
import com.whyc.properties.RedisProperties;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;
 
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@Slf4j
public class RedisConfig {
 
    @Autowired
    RedisProperties redisProperties;
 
 
    /**
     * redis单机
     */
    @Bean("redisClient")
    @ConditionalOnProperty(name = "redis.model", havingValue = "single")
    @Order(1)
    RedissonClient redissonClientSingle() {
        log.error("redis single client初始化了");
        Config config = new Config();
        String node = redisProperties.getSingle().getAddress();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(node)
                .setTimeout(redisProperties.getTimeout())
                .setConnectionPoolSize(redisProperties.getPool().getSize())
                .setConnectionMinimumIdleSize(redisProperties.getPool().getMinIdle());
        if (!StringUtils.isEmpty(redisProperties.getPassword())) {
            serverConfig.setPassword(redisProperties.getPassword());
        }
 
        return Redisson.create(config);
 
    }
 
    /**
     * redis哨兵
     */
    @Bean("redisClient")
    @ConditionalOnProperty(name = "redis.model", havingValue = "sentinel")
    RedissonClient redissonClientSentinel() {
        log.error("redis sentinel client初始化了");
        Config config = new Config();
 
        String[] nodes = redisProperties.getSentinel().getNodes().split(",");
 
        SentinelServersConfig serversConfig = config.useSentinelServers()
                .addSentinelAddress(nodes)
                .setMasterName(redisProperties.getSentinel().getMaster())
                .setReadMode(ReadMode.SLAVE)
                .setFailedAttempts(redisProperties.getSentinel().getFailMax())
                .setTimeout(redisProperties.getTimeout())
                .setMasterConnectionPoolSize(redisProperties.getPool().getSize())
                .setSlaveConnectionPoolSize(redisProperties.getPool().getSize());
        if (!StringUtils.isEmpty(redisProperties.getPassword())) {
            serversConfig.setPassword(redisProperties.getPassword());
        }
        return Redisson.create(config);
 
    }
 
    /**
     * 集群
     */
    @Bean("redisClient")
    @ConditionalOnProperty(name = "redis.model", havingValue = "cluster")
    RedissonClient redissonClientCluster() {
        log.error("redis cluster client初始化了");
        Config config = new Config();
        RedisClusterProperties cluster = redisProperties.getCluster();
        RedisPoolProperties pool = redisProperties.getPool();
        String[] nodes = cluster.getNodes().split(",");
        ClusterServersConfig serversConfig = config.useClusterServers()
                .addNodeAddress(nodes)
                .setScanInterval(cluster.getScanInterval())
                .setIdleConnectionTimeout(pool.getSoTimeout())
                .setConnectTimeout(pool.getConnTimeout())
                .setFailedAttempts(cluster.getFailedAttempts())
                .setRetryAttempts(cluster.getRetryAttempts())
                .setRetryInterval(cluster.getRetryInterval())
                .setMasterConnectionPoolSize(cluster.getMasterConnectionPoolSize())
                .setSlaveConnectionPoolSize(cluster.getSlaveConnectionPoolSize())
                .setTimeout(redisProperties.getTimeout());
 
        if (!StringUtils.isEmpty(redisProperties.getPassword())) {
            serversConfig.setPassword(redisProperties.getPassword());
        }
        return Redisson.create(config);
    }
 
    /**
     *
     */
    @Bean("redisClient")
    @ConditionalOnProperty(name = "redis.model", havingValue = "master-slave")
    RedissonClient redissonClientMasterSlave() {
        log.error("redis master-slave client初始化了");
        Config config = new Config();
        RedisClusterProperties cluster = redisProperties.getCluster();
        RedisPoolProperties pool = redisProperties.getPool();
        String[] nodes = cluster.getNodes().split(",");
        MasterSlaveServersConfig serversConfig = config.useMasterSlaveServers()
                .setMasterAddress(nodes[0])
                .addSlaveAddress(nodes[1]);
 
        if (!StringUtils.isEmpty(redisProperties.getPassword())) {
            serversConfig.setPassword(redisProperties.getPassword());
        }
        return Redisson.create(config);
    }
 
}