The main application scenario of redis multiple data sources is to use multiple redis servers or multiple redis libraries. In this paper, the fastdep dependency integration framework is used to quickly integrate redis multiple data sources and integrate lettuce connection pool. Only after introducing dependency, configure the connection information of multiple data sources in yaml file.
Source address
I hope you can support it with star. In the future, we will add simple integration of other dependencies.
https://github.com/louislivi/fastdep
Introduce dependency
-
Maven
<dependency> <groupId>com.louislivi.fastdep</groupId> <artifactId>fastdep-redis</artifactId> <version>1.0.1</version> </dependency>
-
Gradle
compile group: 'com.louislivi.fastdep', name: 'fastdep-redis', version: '1.0.1'
configuration file
fastdep: redis: redis1: #Connection name database: 0 host: 192.168.12.88 port: 6379 lettuce: #The following are the supplementary settings for the connection pool shutdown-timeout: 100 # Shutdown timeout pool: max-active: 18 # Maximum number of connections in the connection pool (use a negative value to indicate no limit) max-idle: 8 # Maximum free connections in connection pool max-wait: 30 # Connection pool maximum block wait time (use a negative value to indicate no limit) min-idle: 0 # Minimum free connections in connection pool redis2: #Connection name database: 1 host: 192.168.12.88 port: 6379 lettuce: #The following are the supplementary settings for the connection pool shutdown-timeout: 100 # Shutdown timeout pool: max-active: 18 # Maximum number of connections in the connection pool (use a negative value to indicate no limit) max-idle: 8 # Maximum free connections in connection pool max-wait: 30 # Connection pool maximum block wait time (use a negative value to indicate no limit) min-idle: 0 # Minimum free connections in connection pool
application
@Autowired
private StringRedisTemplate redis1StringRedisTemplate;
// During injection, redis1 represents that the connection name StringRedisTemplate in the configuration file is a fixed injection redis object type,
// Will automatically match based on the injected variable name
@Autowired
private StringRedisTemplate redis2StringRedisTemplate;
@GetMapping("redis")
public void redis() {
System.out.println(redis1StringRedisTemplate.opsForValue().get("test"));
System.out.println(redis2StringRedisTemplate.opsForValue().get("test"));
}
extend
Sometimes we need to customize the redisTemplate serialization and add some additional configuration. At this time, we can encapsulate a redis tool class to implement it
package com.louislivi.fastdep.test.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
/**
* RedisUtil
*
* @author : louislivi
*/
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate redis1StringRedisTemplate;
@Autowired
private StringRedisTemplate redis2StringRedisTemplate;
@Autowired
private RedisTemplate redis2RedisTemplate;
@Autowired
private RedisTemplate redis1RedisTemplate;
public RedisTemplate redisTemplate(String name) {
RedisTemplate redisTemplate;
switch (name) {
case "redis2":
redisTemplate = redis2RedisTemplate;
break;
default:
redisTemplate = redis1RedisTemplate;
break;
}
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(stringRedisSerializer);
return redisTemplate;
}
public StringRedisTemplate stringRedisTemplate(String name) {
StringRedisTemplate stringRedisTemplate;
switch (name) {
case "redis2":
stringRedisTemplate = redis2StringRedisTemplate;
break;
default:
stringRedisTemplate = redis1StringRedisTemplate;
break;
}
stringRedisTemplate.setEnableTransactionSupport(true);
return stringRedisTemplate;
}
}
@Autowired
private RedisUtil redisUtil;
@GetMapping("redis")
public void redis() {
System.out.println(redisUtil.redisTemplate("redis1").opsForValue().get("test"));
System.out.println(redisUtil.stringRedisTemplate("redis2").opsForValue().get("test"));
}
principle
Using importbeandefinitionregister BeanDefinitionBuilder.genericBeanDefinition to dynamically inject a Bean is actually very simple. If you are interested in looking at the source code, is such dependency integration much easier?
I hope you can support open source, give a little star, and continue to develop other dependent integrations in the future, even compatible with other frameworks. fastdep makes it easier for java to integrate dependencies. Here, we also recruit coder with the same aspiration to jointly improve the project.
Finally note: the theory of light is not enough. By the way, I'd like to present you ten sets of practical tutorials and interview question bank of the latest JAVA architecture project in 2020, which can be found under the transformation of seven bar umbrella bar and Zero clothing umbrella (Digital homophony), and also can communicate with the old architect
The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling