Introduction to the
The main application scenario of Redis multi-data source is to use multiple Redis servers or use multiple Redis libraries. This article uses the FastDEP dependent integration framework to quickly integrate Redis multi-data source and integrate the Lettuce connection pool. Simply import the dependencies and configure the multi-source connection information in the YAML file.
The source address
We hope you can support star, and we will add easy integration of other dependencies in the future. Github.com/louislivi/f…
Introduction of depend on
Maven
<dependency>
<groupId>com.louislivi.fastdep</groupId>
<artifactId>fastdep-redis</artifactId>
<version>1.0.1</version>
</dependency>
Copy the code
Gradle
compile group: 'com.louislivi.fastdep'.name: 'fastdep-redis'.version: '1.0.1'
Copy the code
The configuration file
fastdep:
redis:
redis1: # connection name
database: 0
host: 192.16812.88.
port: 6379
lettuce: The following are supplementary Settings for connection pooling
shutdown-timeout: 100 # turn off the timeout
pool:
max-active: 18 # maximum number of connections in the pool (use negative values to indicate no limit)
max-idle: 8 The maximum number of free connections in the connection pool
max-wait: 30 Maximum connection pool blocking wait time (negative value indicates no limit)
min-idle: 0 Minimum free connection in connection pool
redis2: # connection name
database: 1
host: 192.16812.88.
port: 6379
lettuce: The following are supplementary Settings for connection pooling
shutdown-timeout: 100 # turn off the timeout
pool:
max-active: 18 # maximum number of connections in the pool (use negative values to indicate no limit)
max-idle: 8 The maximum number of free connections in the connection pool
max-wait: 30 Maximum connection pool blocking wait time (negative value indicates no limit)
min-idle: 0 Minimum free connection in connection pool
Copy the code
using
@Autowired
private StringRedisTemplate redis1StringRedisTemplate;
StringRedisTemplate is the fixed injected Redis object type.
// Will be automatically matched based on the injected variable name
@Autowired
private StringRedisTemplate redis2StringRedisTemplate;
@GetMapping("redis")
public void redis(a) {
System.out.println(redis1StringRedisTemplate.opsForValue().get("test"));
System.out.println(redis2StringRedisTemplate.opsForValue().get("test"));
}
Copy the code
extension
Sometimes you need to customize the redisTemplate serialization and add some extra configuration. In this case, you can package a Redis utility class to do this
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);
returnstringRedisTemplate; }}Copy the code
@Autowired
private RedisUtil redisUtil;
@GetMapping("redis")
public void redis(a) {
System.out.println(redisUtil.redisTemplate("redis1").opsForValue().get("test"));
System.out.println(redisUtil.stringRedisTemplate("redis2").opsForValue().get("test"));
}
Copy the code
The principle of
Use ImportBeanDefinitionRegistrar BeanDefinitionBuilder. GenericBeanDefinition dynamic injection Bean is very simple interested can look at the source, is dependent on integrated so simple a lot?
Hope everyone can support open source, give a little star, will continue to develop other dependencies integration, even compatible with other frameworks. Fastdep makes Java integration dependencies easier. We also recruit like-minded coders to work together to improve this project.