redisson用来做分布式锁
lettuce用来做多线程操作
1、gradle配置文件
[group: 'io.lettuce', name: 'lettuce-core', version: '6.1.9.RELEASE'],
[group: 'org.redisson', name: 'redisson-spring-boot-starter', version: '3.26.1'],
dependencies {
api ("org.springframework.boot:spring-boot-starter-data-redis")
api "org.springframework.boot:spring-boot-starter-cache"
api 'io.lettuce:lettuce-core'
api 'org.redisson:redisson-spring-boot-starter'
}
2、redisson配置
@Configuration
public class RedissonConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
@ConditionalOnProperty(prefix = "spring.redis", name = "cluster")
public Redisson redisson() {
//redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加
List<String> clusterNodes = new ArrayList<>();
List<String> list = new ArrayList<>();
if(redisProperties.getCluster() != null) {
list.addAll(redisProperties.getCluster().getNodes());
} else {
list.add(redisProperties.getHost()+":"+redisProperties.getPort());
}
for (int i = 0; i < list.size(); i++) {
clusterNodes.add("redis://" + list.get(i));
}
Config config = new Config();
ClusterServersConfig clusterServersConfig = config.useClusterServers()
.addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
if(StringUtils.isNotBlank(redisProperties.getPassword())) {
clusterServersConfig.setPassword(redisProperties.getPassword());//设置密码
}
return (Redisson) Redisson.create(config);
}
@Bean
@ConditionalOnProperty(prefix = "spring.redis", name = "host")
public Redisson redissonSingleServer() {
Config config = new Config();
config.useSingleServer().setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort());
if(StringUtils.isNotBlank(redisProperties.getPassword())) {
config.useSingleServer().setPassword(redisProperties.getPassword());
}
return (Redisson) Redisson.create(config);
}
}
3、lettuce配置
@Slf4j
@Configuration
public class RedisLettuceConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
//通用配置
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
genericObjectPoolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
genericObjectPoolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
//支持自适应集群拓扑刷新和静态刷新源
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
.enablePeriodicRefresh(Duration.ofSeconds(5))
.enableAllAdaptiveRefreshTriggers()
.adaptiveRefreshTriggersTimeout(Duration.ofSeconds(10))
//.enablePeriodicRefresh(Duration.ofSeconds(10))
.build();
ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder().timeoutOptions(TimeoutOptions.enabled(Duration.ofSeconds(30)))
//.autoReconnect(false) 是否自动重连
//.pingBeforeActivateConnection(Boolean.TRUE)
//.cancelCommandsOnReconnectFailure(Boolean.TRUE)
//.disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
.topologyRefreshOptions(clusterTopologyRefreshOptions).build();
LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder()
.poolConfig(genericObjectPoolConfig)
//.readFrom(ReadFrom.NEAREST)
.clientOptions(clusterClientOptions).build();
return newConnectionFactory(lettuceClientConfiguration);
}
public LettuceConnectionFactory newConnectionFactory(LettuceClientConfiguration lettuceClientConfiguration) {
if(redisProperties.getCluster() == null) {
RedisStandaloneConfiguration redisClusterConfiguration = new RedisStandaloneConfiguration();
//单机redis
redisClusterConfiguration.setHostName(redisProperties.getHost());
redisClusterConfiguration.setDatabase(redisProperties.getDatabase());
redisClusterConfiguration.setPort(redisProperties.getPort());
redisClusterConfiguration.setPassword(redisProperties.getPassword());
return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
} else {
// 集群模式
Set<RedisNode> nodes = new HashSet<>();
for (String split : redisProperties.getCluster().getNodes()) {
String[] split1 = split.split(":");
nodes.add(new RedisNode(split1[0].trim(), Integer.parseInt(split1[1])));
}
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
redisClusterConfiguration.setClusterNodes(nodes);
redisClusterConfiguration.setPassword(redisProperties.getPassword());
return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
}
}
}