redis 預設有0-16 號資料庫,一般我們操作redis時,用的是0號資料庫,但是有時我們的專案想同時操作多個資料庫,又不想每次存取其它庫資料時select 切換資料庫,這樣太繁瑣。
因此我們需要配置多個Jedis Client,但是jedis是容易阻塞,效率不太好,所以我這邊採用了 Lettuce Client,它是 Reactive 的連接方式,效率比較高。但是怎麼使用到Lettuce Client,其實一般我們加入 spring-boot-starter-data-redis 依賴,透過RedisTemplate 去使用Redis的功能時,當版本很高的時候預設RedisTemplate底層用的就是透過Lettuce Client 去建立連線和操作數據。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.0.5.RELEASE</version> </dependency>
下面我的截圖使用了四個資料來源,分別是1,2,3,4號庫。
1)新建一個configuration 設定類別
2)new RedisStandaloneConfiguration(host, port); 初始化一個Redis配置,並接下來選擇好函式庫號。
3)初始化一個 LettuceConnectionFactory 。
4)實例化一個 RedisTemplate ,設定鍵值序列化的方式,這裡key和value都是字串的,所以序列化器選擇 StringRedisSerializer。
5)為 RedisTemplate 設定第三步驟所建立的 LettuceConnectionFactory,並以@Bean註解注入到spring容器中,使用時直接透過方法名稱在spring容器中查找,組裝到引用它的實例中。
import io.lettuce.core.resource.ClientResources; import io.lettuce.core.resource.DefaultClientResources; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.util.ObjectUtils; import java.time.Duration; /** * reactive redis * @Author:wangqipeng * @Date:14:38 2019-07-03 */ @Configuration public class RedisDatasourceConfiguration { @Value("${redis.isCleanRedisCache:false}") private String cleanRedisCache; @Value("${redis.host:127.0.0.1}") public String host; @Value("${redis.port:6379}") public Integer port; private String password; @Value("${redis.timeout:2000}") public Integer timeout; public Integer maxIdle = 16; public Integer minIdle = 5; public Integer maxTotal = 30; @Bean public RedisTemplate<String, String> stringRedisTemplate1() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(1); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } @Bean public RedisTemplate<String, String> stringRedisTemplate2() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(2); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } @Bean public RedisTemplate<String, String> stringRedisTemplate3() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(3); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } @Bean public RedisTemplate<String, String> stringRedisTemplate4() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(4); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } @Bean public RedisTemplate<String, String> stringRedisTemplate5() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port); configuration.setDatabase(5); if (!ObjectUtils.isEmpty(password)) { RedisPassword redisPassword = RedisPassword.of(password); configuration.setPassword(redisPassword); } return createRedisTemplate(creatFactory(configuration)); } private RedisTemplate<String, String> getSerializerRedisTemplate(){ RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); return redisTemplate; } private RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, String> redisTemplate = getSerializerRedisTemplate(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.afterPropertiesSet(); return redisTemplate; } private GenericObjectPoolConfig getGenericObjectPoolConfig(){ GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); genericObjectPoolConfig.setMaxTotal(maxTotal); genericObjectPoolConfig.setMinIdle(minIdle); genericObjectPoolConfig.setMaxIdle(maxIdle); genericObjectPoolConfig.setMaxWaitMillis(timeout); return genericObjectPoolConfig; } private LettuceConnectionFactory creatFactory(RedisStandaloneConfiguration configuration){ LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder(); builder.poolConfig(getGenericObjectPoolConfig()); // LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder(); // builder.clientResources(clientResources()); // builder.commandTimeout(Duration.ofSeconds(3000)); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build()); connectionFactory.afterPropertiesSet(); return connectionFactory; } }
這裡引用是2號函式庫,也就是上面透過@Bean 載入到spring容器中的。
以上是Redis怎麼配置多個資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!