我是靠谱客的博主 如意戒指,最近开发中收集的这篇文章主要介绍redistemplate 多数据源配置,亲测可用,不能用你打我,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

redistemplate 多数据源配置,网上找了很多博客,都不能用,要么代码报错,要么不报错查询不到数据,真操蛋,气死我了,自己摸索写了个示例,亲测可用

 

pom.xml 引入这两个

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
package com.cun.redis;
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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisTemplateConfig {
@Bean(name = "redisTemplate0")
public RedisTemplate<String, Object> redisTemplate(@Value("${spring.redis.host}") String host,
@Value("${spring.redis.password}") String password,
@Value("${spring.redis.port}") int port,
@Value("${spring.redis.database}") int database) {
return buildRedisTemplate(buildConnectionFactory(jedisPoolConfig(), host, password, port, database));
}
@Bean(name = "redisTemplate1")
public RedisTemplate<String, Object> redisTemplate1(@Value("${spring.redis1.host}") String host,
@Value("${spring.redis1.password}") String password,
@Value("${spring.redis1.port}") int port,
@Value("${spring.redis1.database}") int database) {
return buildRedisTemplate(buildConnectionFactory(jedisPoolConfig(), host, password, port, database));
}
protected RedisTemplate<String, Object> buildRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(stringRedisSerializer);
template.afterPropertiesSet();
return template;
}
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//最大连接数
jedisPoolConfig.setMaxTotal(100);
//最小空闲连接数
jedisPoolConfig.setMinIdle(20);
//当池内没有可用的连接时,最大等待时间
jedisPoolConfig.setMaxWaitMillis(10000);
return jedisPoolConfig;
}
public JedisConnectionFactory buildConnectionFactory(JedisPoolConfig jedisPoolConfig,
String host, String password, int port, int database) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setDatabase(database);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(password);
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
jedisBuilder.poolConfig(jedisPoolConfig);
JedisClientConfiguration jedisClientConfiguration = jedisBuilder.build();
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
}

application.yml

server:
port: 80
servlet:
context-path: /
tomcat:
uri-encoding: utf-8
spring:
redis:
database: 0
host: 119.97.185.22
port: 9736
password: *****
redis1:
database: 1
host: 119.97.185.22
port: 9736
password: ******

调用很简单,如下

package com.cun.redis;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Set;
@RestController
public class HomeControlle {
@Resource(name = "redisTemplate0")
private RedisTemplate redisTemplate;
@Resource(name = "redisTemplate1")
private RedisTemplate redisTemplate1;
@RequestMapping("/hello")
public String hello() {
Set<String> keys = redisTemplate.keys("*");
System.out.println(keys.size());
System.out.println(redisTemplate1.keys("*").size());
return "hello";
}
}

 

 

最后

以上就是如意戒指为你收集整理的redistemplate 多数据源配置,亲测可用,不能用你打我的全部内容,希望文章能够帮你解决redistemplate 多数据源配置,亲测可用,不能用你打我所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部