In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to configure multiple databases in Redis". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to configure multiple databases in Redis" can help you solve the problem.
Preface
Redis has database 0-16 by default. Generally, we use database 0 when we operate redis, but sometimes our project wants to operate multiple databases at the same time, and does not want select to switch databases every time we access other database data, which is too cumbersome.
Therefore, we need to configure multiple Jedis Client, but jedis is easy to block and the efficiency is not very good, so I use Lettuce Client here, which is the connection mode of Reactive, which is more efficient. But how to use Lettuce Client, in fact, we generally add spring-boot-starter-data-redis dependencies. When we use Redis functions through RedisTemplate, when the version is very high, the default RedisTemplate underlying layer is to establish connections and manipulate data through Lettuce Client.
1. Add pom dependent org.springframework.boot spring-boot-starter-data-redis 2.0.5.RELEASE 2. Configure and add multiple data sources to the spring container
Four data sources are used in my screenshot below, which are Library 1, 2, and 3, respectively.
1) create a new configuration configuration class
2) new RedisStandaloneConfiguration (host, port); initialize a Redis configuration, and then select the library number.
3) initialize a LettuceConnectionFactory.
4) instantiate a RedisTemplate and set the way in which keys are serialized, where both key and value are strings, so the serializer chooses StringRedisSerializer.
5) set the LettuceConnectionFactory created in step 3 to RedisTemplate, and inject the @ Bean annotation into the spring container. When using it, look it up in the spring container directly through the method name, and assemble it into the instance that references it.
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 / @ Configurationpublic 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 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 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 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 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 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 getSerializerRedisTemplate () {RedisTemplate redisTemplate = new RedisTemplate (); redisTemplate.setKeySerializer (new StringRedisSerializer ()); redisTemplate.setHashKeySerializer (new StringRedisSerializer ()); redisTemplate.setHashValueSerializer (new StringRedisSerializer ()) Return redisTemplate;} private RedisTemplate createRedisTemplate (RedisConnectionFactory redisConnectionFactory) {RedisTemplate 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;}} III. Usage
The reference here is library 2, which is loaded into the spring container via @ Bean above.
This is the end of the introduction to "how to configure multiple databases in Redis". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.