1項目配置文件:
1.1:pom.xml
<dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.17.7</version></dependency>
1.2 application.yml配置文件
spring:redis:cluster:nodes:- ip:port- ip:port- ip:port- ip:port- ip:port- ip:port# 密碼password: *******# 連接超時時間timeout: 10s# 是否開啟sslssl: false
redisson:
# 線程池數量threads: 8
# Netty線程池數量nettyThreads: 16
# 集群配置clusterServersConfig:# 客戶端名稱clientName: redisClient# master最小空閑連接數masterConnectionMinimumIdleSize: 16# master連接池大小masterConnectionPoolSize: 32# slave最小空閑連接數slaveConnectionMinimumIdleSize: 16# slave連接池大小slaveConnectionPoolSize: 64# 連接空閑超時,單位:毫秒idleConnectionTimeout: 10000# 命令等待超時,單位:毫秒timeout: 3000# 發布和訂閱連接池大小subscriptionConnectionPoolSize: 50# 讀取模式readMode: "SLAVE"# 訂閱模式subscriptionMode: "MASTER"cacheGroup:# 用例: @Cacheable(cacheNames="groupId", key="#XXX") 方可使用緩存組配置- groupId: redissonCacheMap# 組過期時間(腳本監控)ttl: 60000# 組最大空閑時間(腳本監控)maxIdleTime: 60000# 組最大長度maxSize: 0
配置類
2-1配置屬性類:RedissonProperties.java
import lombok.Data;
import lombok.NoArgsConstructor;
import org.redisson.config.ReadMode;
import org.redisson.config.SubscriptionMode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.List;/*** Redisson 配置屬性* @author zengshichang*/
@Data
@Component
@ConfigurationProperties(prefix = "redisson")
public class RedissonProperties {/*** 線程池數量,默認值 = 當前處理核數量 * 2*/private int threads;/*** Netty線程池數量,默認值 = 當前處理核數量 * 2*/private int nettyThreads;/*** 單機服務配置*/private SingleServerConfig singleServerConfig;/*** 集群服務配置*/private ClusterServersConfig clusterServersConfig;/*** 緩存組*/private List<CacheGroup> cacheGroup;@Data@NoArgsConstructorpublic static class SingleServerConfig {/*** 客戶端名稱*/private String clientName;/*** 最小空閑連接數*/private int connectionMinimumIdleSize;/*** 連接池大小*/private int connectionPoolSize;/*** 連接空閑超時,單位:毫秒*/private int idleConnectionTimeout;/*** 命令等待超時,單位:毫秒*/private int timeout;/*** 發布和訂閱連接池大小*/private int subscriptionConnectionPoolSize;}@Data@NoArgsConstructorpublic static class ClusterServersConfig {/*** 客戶端名稱*/private String clientName;/*** master最小空閑連接數*/private int masterConnectionMinimumIdleSize;/*** master連接池大小*/private int masterConnectionPoolSize;/*** slave最小空閑連接數*/private int slaveConnectionMinimumIdleSize;/*** slave連接池大小*/private int slaveConnectionPoolSize;/*** 連接空閑超時,單位:毫秒*/private int idleConnectionTimeout;/*** 命令等待超時,單位:毫秒*/private int timeout;/*** 發布和訂閱連接池大小*/private int subscriptionConnectionPoolSize;/*** 讀取模式*/private ReadMode readMode;/*** 訂閱模式*/private SubscriptionMode subscriptionMode;}@Data@NoArgsConstructorpublic static class CacheGroup {/*** 組id*/private String groupId;/*** 組過期時間*/private long ttl;/*** 組最大空閑時間*/private long maxIdleTime;/*** 組最大長度*/private int maxSize;}
}
2-2redis配置:RedisConfig.java
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import cn.hutool.core.util.ObjectUtil;import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** redis配置* @author zengshichang*/
@Slf4j
@Configuration
@EnableCaching
@EnableConfigurationProperties(RedissonProperties.class)
public class RedisConfig extends CachingConfigurerSupport {@Autowiredprivate RedissonProperties redissonProperties;@Autowiredprivate ObjectMapper objectMapper;@Beanpublic RedissonAutoConfigurationCustomizer redissonCustomizer() {return config -> {config.setThreads(redissonProperties.getThreads()).setNettyThreads(redissonProperties.getNettyThreads()).setCodec(new StringCodec());RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig();if (ObjectUtil.isNotNull(singleServerConfig)) {// 使用單機模式config.useSingleServer().setTimeout(singleServerConfig.getTimeout()).setClientName(singleServerConfig.getClientName()).setIdleConnectionTimeout(singleServerConfig.getIdleConnectionTimeout()).setSubscriptionConnectionPoolSize(singleServerConfig.getSubscriptionConnectionPoolSize()).setConnectionMinimumIdleSize(singleServerConfig.getConnectionMinimumIdleSize()).setConnectionPoolSize(singleServerConfig.getConnectionPoolSize());}// 集群配置方式 參考下方注釋RedissonProperties.ClusterServersConfig clusterServersConfig = redissonProperties.getClusterServersConfig();if (ObjectUtil.isNotNull(clusterServersConfig)) {config.useClusterServers().setTimeout(clusterServersConfig.getTimeout()).setClientName(clusterServersConfig.getClientName()).setIdleConnectionTimeout(clusterServersConfig.getIdleConnectionTimeout()).setSubscriptionConnectionPoolSize(clusterServersConfig.getSubscriptionConnectionPoolSize()).setMasterConnectionMinimumIdleSize(clusterServersConfig.getMasterConnectionMinimumIdleSize()).setMasterConnectionPoolSize(clusterServersConfig.getMasterConnectionPoolSize()).setSlaveConnectionMinimumIdleSize(clusterServersConfig.getSlaveConnectionMinimumIdleSize()).setSlaveConnectionPoolSize(clusterServersConfig.getSlaveConnectionPoolSize()).setReadMode(clusterServersConfig.getReadMode()).setSubscriptionMode(clusterServersConfig.getSubscriptionMode());}log.info("初始化 redis 配置");};}/*** 整合spring-cache*/@Beanpublic CacheManager cacheManager(RedissonClient redissonClient) {List<RedissonProperties.CacheGroup> cacheGroup = redissonProperties.getCacheGroup();Map<String, CacheConfig> config = new HashMap<>(16);for (RedissonProperties.CacheGroup group : cacheGroup) {CacheConfig cacheConfig = new CacheConfig(group.getTtl(), group.getMaxIdleTime());cacheConfig.setMaxSize(group.getMaxSize());config.put(group.getGroupId(), cacheConfig);}return new RedissonSpringCacheManager(redissonClient, config, new JsonJacksonCodec(objectMapper));}
}
注:EnableConfigurationPropertiess用法:
@EnableConfigurationProperties(RedissonProperties.class)是一個注解,用于啟用將指定的配置類作為配置屬性進行自動配置。在Spring Boot應用程序中,通常使用@EnableConfigurationProperties注解來啟用將配置屬性綁定到特定的配置類。在這種情況下,RedissonProperties是用于綁定與Redisson相關的配置屬性的配置類。通過使用@EnableConfigurationProperties(RedissonProperties.class)注解,可以實現以下幾點:將RedissonProperties類作為配置屬性類進行注冊和加載。
自動將應用程序配置文件中與RedissonProperties相關的屬性值與RedissonProperties類中的字段進行綁定。
可以通過在代碼中注入RedissonProperties類的實例來獲得配置的屬性值,從而在應用程序中使用這些屬性值。
這樣,通過@EnableConfigurationProperties(RedissonProperties.class)注解,Redisson的配置屬性就會被正確地加載和綁定,并能夠在應用程序中使用這些配置。
2.3:Redisson使用
@RequiredArgsConstructor
@Service
@Slf4j
@RefreshScope
public class ServiceImpl implements IService {@Autowiredprivate RedisDao redisDao;
}