Commit 9d3ca743740c1daa5cb269116dd5c5cfd0d239d3
1 parent
fa7b66f6
lts==增加redis密码配置,前缀.configs.redis.password=
Showing
3 changed files
with
26 additions
and
2 deletions
| @@ -141,5 +141,11 @@ | @@ -141,5 +141,11 @@ | ||
| 141 | <artifactId>logback-classic</artifactId> | 141 | <artifactId>logback-classic</artifactId> |
| 142 | <scope>provided</scope> | 142 | <scope>provided</scope> |
| 143 | </dependency> | 143 | </dependency> |
| 144 | + <dependency> | ||
| 145 | + <groupId>org.projectlombok</groupId> | ||
| 146 | + <artifactId>lombok</artifactId> | ||
| 147 | + <version>RELEASE</version> | ||
| 148 | + <scope>provided</scope> | ||
| 149 | + </dependency> | ||
| 144 | </dependencies> | 150 | </dependencies> |
| 145 | </project> | 151 | </project> |
| @@ -75,6 +75,10 @@ public interface ExtConfig { | @@ -75,6 +75,10 @@ public interface ExtConfig { | ||
| 75 | String REGISTRY_RETRY_PERIOD_KEY = "retry.period"; | 75 | String REGISTRY_RETRY_PERIOD_KEY = "retry.period"; |
| 76 | String REDIS_SESSION_TIMEOUT = "redis.session.timeout"; | 76 | String REDIS_SESSION_TIMEOUT = "redis.session.timeout"; |
| 77 | /** | 77 | /** |
| 78 | + * Redis 注册中心密码(开启 requirepass 时使用,多个 redis 节点共用同一密码) | ||
| 79 | + */ | ||
| 80 | + String REDIS_PASSWORD = "redis.password"; | ||
| 81 | + /** | ||
| 78 | * JDBC链接相关配置 | 82 | * JDBC链接相关配置 |
| 79 | */ | 83 | */ |
| 80 | String JDBC_URL = "jdbc.url"; | 84 | String JDBC_URL = "jdbc.url"; |
| @@ -16,6 +16,8 @@ import com.github.ltsopensource.core.registry.NodeRegistryUtils; | @@ -16,6 +16,8 @@ import com.github.ltsopensource.core.registry.NodeRegistryUtils; | ||
| 16 | import com.github.ltsopensource.core.registry.NotifyEvent; | 16 | import com.github.ltsopensource.core.registry.NotifyEvent; |
| 17 | import com.github.ltsopensource.core.registry.NotifyListener; | 17 | import com.github.ltsopensource.core.registry.NotifyListener; |
| 18 | import com.github.ltsopensource.core.support.SystemClock; | 18 | import com.github.ltsopensource.core.support.SystemClock; |
| 19 | + | ||
| 20 | +import lombok.extern.slf4j.Slf4j; | ||
| 19 | import redis.clients.jedis.Jedis; | 21 | import redis.clients.jedis.Jedis; |
| 20 | import redis.clients.jedis.JedisPool; | 22 | import redis.clients.jedis.JedisPool; |
| 21 | import redis.clients.jedis.JedisPoolConfig; | 23 | import redis.clients.jedis.JedisPoolConfig; |
| @@ -27,6 +29,7 @@ import java.util.concurrent.*; | @@ -27,6 +29,7 @@ import java.util.concurrent.*; | ||
| 27 | /** | 29 | /** |
| 28 | * @author Robert HG (254963746@qq.com) on 5/17/15. | 30 | * @author Robert HG (254963746@qq.com) on 5/17/15. |
| 29 | */ | 31 | */ |
| 32 | +@Slf4j | ||
| 30 | public class RedisRegistry extends FailbackRegistry { | 33 | public class RedisRegistry extends FailbackRegistry { |
| 31 | 34 | ||
| 32 | private static final Logger LOGGER = LoggerFactory.getLogger(RedisRegistry.class); | 35 | private static final Logger LOGGER = LoggerFactory.getLogger(RedisRegistry.class); |
| @@ -61,13 +64,24 @@ public class RedisRegistry extends FailbackRegistry { | @@ -61,13 +64,24 @@ public class RedisRegistry extends FailbackRegistry { | ||
| 61 | 64 | ||
| 62 | this.reconnectPeriod = config.getParameter(ExtConfig.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD); | 65 | this.reconnectPeriod = config.getParameter(ExtConfig.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD); |
| 63 | 66 | ||
| 67 | + String password = config.getParameter(ExtConfig.REDIS_PASSWORD); | ||
| 68 | + log.info("==============redis=====[{}]", password); | ||
| 69 | + boolean hasPassword = password != null && password.length() > 0; | ||
| 70 | + | ||
| 64 | String[] addrs = address.split(","); | 71 | String[] addrs = address.split(","); |
| 65 | for (String addr : addrs) { | 72 | for (String addr : addrs) { |
| 66 | int i = addr.indexOf(':'); | 73 | int i = addr.indexOf(':'); |
| 67 | String host = addr.substring(0, i); | 74 | String host = addr.substring(0, i); |
| 68 | int port = Integer.parseInt(addr.substring(i + 1)); | 75 | int port = Integer.parseInt(addr.substring(i + 1)); |
| 69 | - this.jedisPools.put(addr, new JedisPool(redisConfig, host, port, | ||
| 70 | - Constants.DEFAULT_TIMEOUT)); | 76 | + JedisPool pool; |
| 77 | + if (hasPassword) { | ||
| 78 | + pool = new JedisPool(redisConfig, host, port, | ||
| 79 | + Constants.DEFAULT_TIMEOUT, password); | ||
| 80 | + } else { | ||
| 81 | + pool = new JedisPool(redisConfig, host, port, | ||
| 82 | + Constants.DEFAULT_TIMEOUT); | ||
| 83 | + } | ||
| 84 | + this.jedisPools.put(addr, pool); | ||
| 71 | } | 85 | } |
| 72 | 86 | ||
| 73 | this.expirePeriod = config.getParameter(ExtConfig.REDIS_SESSION_TIMEOUT, Constants.DEFAULT_SESSION_TIMEOUT); | 87 | this.expirePeriod = config.getParameter(ExtConfig.REDIS_SESSION_TIMEOUT, Constants.DEFAULT_SESSION_TIMEOUT); |