|
1 | 1 | package com.snowalker.lock.redisson; |
2 | 2 |
|
| 3 | +import com.google.common.base.Preconditions; |
3 | 4 | import com.snowalker.lock.redisson.constant.RedisConnectionType; |
4 | 5 | import org.redisson.Redisson; |
5 | 6 | import org.redisson.config.Config; |
6 | 7 | import org.slf4j.Logger; |
7 | 8 | import org.slf4j.LoggerFactory; |
8 | 9 |
|
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
9 | 13 | /** |
10 | 14 | * @author snowalker |
11 | 15 | * @date 2018/7/10 |
@@ -64,28 +68,110 @@ public static RedissonConfigFactory getInstance() { |
64 | 68 | * @return Config |
65 | 69 | */ |
66 | 70 | Config createConfig(String connectionType, String address) { |
| 71 | + Preconditions.checkNotNull(connectionType); |
| 72 | + Preconditions.checkNotNull(address); |
67 | 73 | if (connectionType.equals(RedisConnectionType.STANDALONE.getConnection_type())) { |
68 | | - try { |
69 | | - String redisAddr = REDIS_CONNECTION_PREFIX + address; |
70 | | - config.useSingleServer().setAddress(redisAddr); |
71 | | - LOGGER.info("初始化standalone方式Config,redisAddress:" + redisAddr); |
72 | | - } catch (Exception e) { |
73 | | - LOGGER.error("standalone Redisson init error", e); |
74 | | - e.printStackTrace(); |
75 | | - } |
76 | | - return config; |
| 74 | + return createStandaloneConfig(address); |
77 | 75 | } else if (connectionType.equals(RedisConnectionType.SENTINEL.getConnection_type())) { |
78 | | - return null; |
| 76 | + return createSentinelConfig(address); |
| 77 | + } else if (connectionType.equals(RedisConnectionType.CLUSTER.getConnection_type())) { |
| 78 | + return createClusterConfig(address); |
| 79 | + } else if (connectionType.equals(RedisConnectionType.MASTERSLAVE.getConnection_type())) { |
| 80 | + return createMasterSlaveConfig(address); |
79 | 81 | } |
80 | 82 | throw new RuntimeException("创建Redisson连接Config失败!当前连接方式:" + connectionType); |
81 | 83 | } |
82 | | - } |
83 | 84 |
|
84 | | - public static void main(String[] args) { |
85 | | - Config config = new Config(); |
86 | | - config.useSingleServer().setAddress("redis://127.0.0.1:6379"); |
87 | | - Redisson redisson = (Redisson) Redisson.create(config); |
| 85 | + /** |
| 86 | + * 主从方式配置 |
| 87 | + * @param address |
| 88 | + * @return |
| 89 | + */ |
| 90 | + private Config createMasterSlaveConfig(String address) { |
| 91 | + try { |
| 92 | + String[] addrTokens = address.split(","); |
| 93 | + String masterNodeAddr = addrTokens[0]; |
| 94 | + /**设置主节点ip*/ |
| 95 | + config.useMasterSlaveServers().setMasterAddress(masterNodeAddr); |
| 96 | + /**设置从节点,移除第一个节点,默认第一个为主节点*/ |
| 97 | + List<String> slaveList = new ArrayList<>(); |
| 98 | + for (String addrToken : addrTokens) { |
| 99 | + slaveList.add(REDIS_CONNECTION_PREFIX + addrToken); |
| 100 | + } |
| 101 | + slaveList.remove(0); |
| 102 | + |
| 103 | + config.useMasterSlaveServers().addSlaveAddress((String[]) slaveList.toArray()); |
| 104 | + LOGGER.info("初始化[MASTERSLAVE]方式Config,redisAddress:" + address); |
| 105 | + } catch (Exception e) { |
| 106 | + LOGGER.error("MASTERSLAVE Redisson init error", e); |
| 107 | + e.printStackTrace(); |
| 108 | + } |
| 109 | + return config; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * 集群方式配置 |
| 114 | + * @param address |
| 115 | + * @return |
| 116 | + */ |
| 117 | + private Config createClusterConfig(String address) { |
| 118 | + try { |
| 119 | + String[] addrTokens = address.split(","); |
| 120 | + /**设置cluster节点的服务IP和端口*/ |
| 121 | + for (int i = 0; i < addrTokens.length; i++) { |
| 122 | + config.useClusterServers().addNodeAddress(REDIS_CONNECTION_PREFIX + addrTokens[i]); |
| 123 | + } |
| 124 | + LOGGER.info("初始化[cluster]方式Config,redisAddress:" + address); |
| 125 | + } catch (Exception e) { |
| 126 | + LOGGER.error("cluster Redisson init error", e); |
| 127 | + e.printStackTrace(); |
| 128 | + } |
| 129 | + return config; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * 哨兵方式配置 |
| 134 | + * @param address |
| 135 | + * @return |
| 136 | + */ |
| 137 | + private Config createSentinelConfig(String address) { |
| 138 | + try { |
| 139 | + String[] addrTokens = address.split(","); |
| 140 | + String sentinelAliasName = addrTokens[0]; |
| 141 | + /**设置redis配置文件sentinel.conf配置的sentinel别名*/ |
| 142 | + config.useSentinelServers() |
| 143 | + .setMasterName(sentinelAliasName); |
| 144 | + /**设置sentinel节点的服务IP和端口*/ |
| 145 | + for (int i = 1; i < addrTokens.length; i++) { |
| 146 | + config.useSentinelServers().addSentinelAddress(REDIS_CONNECTION_PREFIX + addrTokens[i]); |
| 147 | + } |
| 148 | + LOGGER.info("初始化[sentinel]方式Config,redisAddress:" + address); |
| 149 | + } catch (Exception e) { |
| 150 | + LOGGER.error("sentinel Redisson init error", e); |
| 151 | + e.printStackTrace(); |
| 152 | + } |
| 153 | + return config; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * 单机方式配置 |
| 158 | + * @param address |
| 159 | + * @return |
| 160 | + */ |
| 161 | + private Config createStandaloneConfig(String address) { |
| 162 | + try { |
| 163 | + String redisAddr = REDIS_CONNECTION_PREFIX + address; |
| 164 | + config.useSingleServer().setAddress(redisAddr); |
| 165 | + LOGGER.info("初始化[standalone]方式Config,redisAddress:" + address); |
| 166 | + } catch (Exception e) { |
| 167 | + LOGGER.error("standalone Redisson init error", e); |
| 168 | + e.printStackTrace(); |
| 169 | + } |
| 170 | + return config; |
| 171 | + } |
| 172 | + |
88 | 173 | } |
| 174 | + |
89 | 175 | } |
90 | 176 |
|
91 | 177 |
|
0 commit comments