# Conflicts: # dxhy-core/src/main/java/com/dxhy/core/job/service/impl/InvoiceInterfaceServiceImpl.javarelease
commit
c57dd6e57f
@ -1,33 +1,135 @@ |
|||||||
package com.dxhy.core.config; |
package com.dxhy.core.config; |
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.apache.commons.lang3.StringUtils; |
||||||
import org.redisson.Redisson; |
import org.redisson.Redisson; |
||||||
import org.redisson.api.RedissonClient; |
import org.redisson.api.RedissonClient; |
||||||
|
import org.redisson.config.ClusterServersConfig; |
||||||
import org.redisson.config.Config; |
import org.redisson.config.Config; |
||||||
import org.springframework.beans.factory.annotation.Value; |
import org.redisson.config.SentinelServersConfig; |
||||||
|
import org.redisson.config.SingleServerConfig; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.boot.autoconfigure.data.redis.RedisProperties; |
||||||
import org.springframework.context.annotation.Bean; |
import org.springframework.context.annotation.Bean; |
||||||
import org.springframework.context.annotation.Configuration; |
|
||||||
|
|
||||||
//@Configuration
|
import java.util.List; |
||||||
//public class RedissionConfig {
|
import java.util.Objects; |
||||||
//
|
|
||||||
// @Value("${spring.redis.host}")
|
@Configuration |
||||||
// private String host;
|
public class RedissionConfig { |
||||||
//
|
Logger log = LoggerFactory.getLogger(RedissionConfig.class); |
||||||
// @Value("${spring.redis.port}")
|
|
||||||
// private String port;
|
//直接使用starter注入的redis配置信息
|
||||||
//
|
@Autowired |
||||||
// @Value("${spring.redis.password}")
|
private RedisProperties redisProperties; |
||||||
// private String redisPassword;
|
|
||||||
//
|
private int timeout = 3000; |
||||||
// @Bean
|
private int connectionPoolSize = 64; |
||||||
// public RedissonClient getRedisson(){
|
private int connectionMinimumIdleSize = 10; |
||||||
//
|
private int pingConnectionInterval = 60000; |
||||||
// Config config = new Config();
|
private static String ADDRESS_PREFIX = "redis://"; |
||||||
// //单机模式 依次设置redis地址和密码
|
|
||||||
// config.useSingleServer()
|
/** |
||||||
// .setAddress("redis://" + host + ":" + port)
|
* 单机模式 |
||||||
// .setPassword(redisPassword);
|
*/ |
||||||
// return Redisson.create(config);
|
@Bean |
||||||
// }
|
public RedissonClient initBean() { |
||||||
//}
|
// 哨兵模式
|
||||||
//
|
RedisProperties.Sentinel sentinel = redisProperties.getSentinel(); |
||||||
|
if (Objects.nonNull(sentinel)) { |
||||||
|
log.info("redis is sentinel mode"); |
||||||
|
return redissonSentinel(); |
||||||
|
} |
||||||
|
// 集群模式
|
||||||
|
RedisProperties.Cluster cluster = redisProperties.getCluster(); |
||||||
|
if (Objects.nonNull(cluster)) { |
||||||
|
log.info("redis is cluster mode"); |
||||||
|
return redissonCluster(); |
||||||
|
} |
||||||
|
// 单机模式
|
||||||
|
String host = redisProperties.getHost(); |
||||||
|
if (StringUtils.isNotBlank(host)) { |
||||||
|
log.info("redis is single mode"); |
||||||
|
return redissonSingle(); |
||||||
|
} |
||||||
|
|
||||||
|
log.error("redisson config can not support this redis mode"); |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 单机模式 |
||||||
|
*/ |
||||||
|
private RedissonClient redissonSingle() { |
||||||
|
String host = redisProperties.getHost(); |
||||||
|
String password = redisProperties.getPassword(); |
||||||
|
int port = redisProperties.getPort(); |
||||||
|
// 声明一个配置类
|
||||||
|
Config config = new Config(); |
||||||
|
SingleServerConfig serverConfig = config.useSingleServer() |
||||||
|
.setAddress(ADDRESS_PREFIX + host + ":" + port) |
||||||
|
.setTimeout(timeout) |
||||||
|
.setPingConnectionInterval(pingConnectionInterval) |
||||||
|
.setConnectionPoolSize(this.connectionPoolSize) |
||||||
|
.setConnectionMinimumIdleSize(this.connectionMinimumIdleSize); |
||||||
|
// 判断密码
|
||||||
|
if (!StringUtils.isEmpty(password)) { |
||||||
|
serverConfig.setPassword(password); |
||||||
|
} |
||||||
|
return Redisson.create(config); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 哨兵模式 |
||||||
|
*/ |
||||||
|
private RedissonClient redissonSentinel() { |
||||||
|
// mymaster
|
||||||
|
String masterName = redisProperties.getSentinel().getMaster(); |
||||||
|
// 127.0.0.1:26389,127.0.0.1:26379
|
||||||
|
List<String> nodes = redisProperties.getSentinel().getNodes(); |
||||||
|
String password = redisProperties.getPassword(); |
||||||
|
// 声明一个配置类
|
||||||
|
Config config = new Config(); |
||||||
|
SentinelServersConfig sentinelServersConfig = config.useSentinelServers(); |
||||||
|
// 扫描间隔
|
||||||
|
sentinelServersConfig.setScanInterval(2000); |
||||||
|
// 判断密码
|
||||||
|
if (!StringUtils.isEmpty(password)) { |
||||||
|
sentinelServersConfig.setPassword(password); |
||||||
|
} |
||||||
|
sentinelServersConfig.setMasterName(masterName); |
||||||
|
String[] nodeArr = nodes.toArray(new String[nodes.size()]); |
||||||
|
for (int i = 0; i < nodeArr.length; i++) { |
||||||
|
nodeArr[i] = ADDRESS_PREFIX + nodeArr[i]; |
||||||
|
} |
||||||
|
// 添加redis节点
|
||||||
|
sentinelServersConfig.addSentinelAddress(nodeArr); |
||||||
|
return Redisson.create(config); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 集群模式 |
||||||
|
*/ |
||||||
|
private RedissonClient redissonCluster() { |
||||||
|
// 192.168.116.156:1901,192.168.116.156:1902
|
||||||
|
List<String> nodes = redisProperties.getCluster().getNodes(); |
||||||
|
String password = redisProperties.getPassword(); |
||||||
|
// 声明一个配置类
|
||||||
|
Config config = new Config(); |
||||||
|
ClusterServersConfig clusterServersConfig = config.useClusterServers(); |
||||||
|
// 扫描间隔
|
||||||
|
clusterServersConfig.setScanInterval(2000); |
||||||
|
// 判断密码
|
||||||
|
if (!StringUtils.isEmpty(password)) { |
||||||
|
clusterServersConfig.setPassword(password); |
||||||
|
} |
||||||
|
// 添加redis节点
|
||||||
|
for (String node : nodes) { |
||||||
|
clusterServersConfig.addNodeAddress(ADDRESS_PREFIX + node); |
||||||
|
} |
||||||
|
return Redisson.create(config); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue