Springboot統合RedisTemplate
37738 ワード
义齿
統合の際、以前は接続プールを構成していたのを思い出しましたが、その後はRedisConnectionFactoryを直接構成し、試してみましたが、jedisのないpom構成を加えると、次のエラーが表示されます.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘taskConnectionFactory’ defined in class path resource [net/rdd/config/TaskServiceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.RedisConnectionFactory]: Factory method ‘taskConnectionFactory’ threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
試してみましたが、jedisの構成が必要であることに気づき、ソースコードを見てみました.factory内部は接続プールを維持していることに気づき、後で整理しました.次に、接続の構成クラスが表示されます.
次に具体的に使用して、対応するクラスを注入すればいいです.
これにより、簡単な配置ができる.ソース参照アドレス:https://github.com/stackXu/SpringBootRedis
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<guava-version>27.0-jre</guava-version>
<jedis-version>2.9.0</jedis-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- springboot redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava-version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
統合の際、以前は接続プールを構成していたのを思い出しましたが、その後はRedisConnectionFactoryを直接構成し、試してみましたが、jedisのないpom構成を加えると、次のエラーが表示されます.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘taskConnectionFactory’ defined in class path resource [net/rdd/config/TaskServiceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.RedisConnectionFactory]: Factory method ‘taskConnectionFactory’ threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
試してみましたが、jedisの構成が必要であることに気づき、ソースコードを見てみました.factory内部は接続プールを維持していることに気づき、後で整理しました.次に、接続の構成クラスが表示されます.
package net.rdd.config;
import net.rdd.listener.RedisMessageListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
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.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.scripting.support.ResourceScriptSource;
@Configuration
public class RedisServiceConfig {
@Value("${redis.task.host}")
private String redisHost;
@Value("${redis.task.port}")
private int redisPort;
@Value("${redis.task.pass}")
private String redisPass;
@Value("${redis.task.db}")
private int redisDb;
@Bean
@Primary
public RedisConnectionFactory taskConnectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setPort(redisPort);
connectionFactory.setHostName(redisHost);
connectionFactory.setDatabase(redisDb);
connectionFactory.setPassword(redisPass);
return connectionFactory;
}
@Bean
public RedisTemplate taskRedisTemplate() {
RedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(taskConnectionFactory());
return template;
}
@Bean
public RedisConnectionFactory rddConnectionFactory() {
//
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setPort(redisPort);
redisStandaloneConfiguration.setHostName(redisHost);
redisStandaloneConfiguration.setDatabase(3);
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisPass));
JedisConnectionFactory redisStandaloneConfigurationFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
// JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
// connectionFactory.setPort(redisPort);
// connectionFactory.setHostName(redisHost);
// connectionFactory.setDatabase(3);
// connectionFactory.setPassword(redisPass);
//
//todo something wrong
/**
* org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
*/
// JedisShardInfo jedisShardInfo = new JedisShardInfo(redisHost,redisPort );
// jedisShardInfo.setConnectionTimeout(10000);
// jedisShardInfo.setSoTimeout(10000);
// jedisShardInfo.setPassword(redisPass);
// JedisConnectionFactory jedisShardInfoFactory = new JedisConnectionFactory(jedisShardInfo);
return redisStandaloneConfigurationFactory;
}
@Bean("rddRedisTemplate")
public StringRedisTemplate rddRedisTemplate() {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(rddConnectionFactory());
// RedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// RedisSerializer stringRedisSerializer = new StringRedisSerializer();
// // key StringRedisSerializer
// template.setKeySerializer(stringRedisSerializer);
// template.setHashKeySerializer(stringRedisSerializer);
// // value GenericJackson2JsonRedisSerializer
// template.setValueSerializer(genericJackson2JsonRedisSerializer);
// template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
return template;
}
@Bean
public RedisScript<Boolean> lockScript() {
DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<Boolean>();
redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/redis-lock.lua")));
redisScript.setResultType(Boolean.class);
return redisScript;
}
@Bean
public RedisScript<Boolean> unlockScript() {
DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<Boolean>();
redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/redis-unlock.lua")));
redisScript.setResultType(Boolean.class);
return redisScript;
}
@Bean
//key ,
RedisMessageListenerContainer keyExpirationListenerContainer(RedisMessageListener listener) {
RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
listenerContainer.setConnectionFactory(taskConnectionFactory());
listenerContainer.addMessageListener(listener, new PatternTopic("__keyevent@" + redisDb + "__:expired"));
return listenerContainer;
}
}
次に具体的に使用して、対応するクラスを注入すればいいです.
@Autowired
@Qualifier(value = "taskRedisTemplate")
private RedisTemplate taskRedisTemplate;
@Autowired
@Qualifier(value = "rddRedisTemplate")
private RedisTemplate rddRedisTemplate;
@Test
public void test02() {
taskRedisTemplate.opsForValue().set("dssd","fdsfds");
rddRedisTemplate.opsForValue().set("322","ewew");
}
これにより、簡単な配置ができる.ソース参照アドレス:https://github.com/stackXu/SpringBootRedis