您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关springboot redis如何使用lettuce配置多数据源,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
目前项目上需要连接两个redis数据源,一个redis数据源是单机模式,一个redis数据源是分片集群模式,这里将具体配置列一下。
项目用的springboot版本为
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
redis: cluster: nodes: 127.0.0.1:9001 lettuce: #连接池配置 pool: #连接池最大连接数 max-active: 20 #连接池最大等待时间,负数表示不做限制 max-wait: -1 #最大空闲连接 max-idle: 9 #最小空闲连接 min-idle: 0 timeout: 500000 redis2: host: 127.0.0.1 port: 6385 lettuce: pool: max-active: 20 max-idle: 8 max-wait: -1 min-idle: 0 timeout: 500000
(这里的redis都没有配置密码)
package com.cq.config; import cn.hutool.core.convert.Convert; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; 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.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.io.Serializable; import java.util.HashMap; import java.util.Map; /** * @author cccccloud on 2020/11/16 17:16 */ @Configuration public class RedisConfig { @Autowired private Environment environment; @Value("${spring.redis2.host}") private String host; @Value("${spring.redis2.port}") private String port; @Value("${spring.redis2.lettuce.pool.max-active}") private String max_active; @Value("${spring.redis2.lettuce.pool.max-idle}") private String max_idle; @Value("${spring.redis2.lettuce.pool.max-wait}") private String max_wait; @Value("${spring.redis2.lettuce.pool.min-idle}") private String min_idle; /** * 配置lettuce连接池 * * @return */ @Bean @Primary @ConfigurationProperties(prefix = "spring.redis.cluster.lettuce.pool") public GenericObjectPoolConfig redisPool() { return new GenericObjectPoolConfig(); } /** * 配置第一个数据源的 * * @return */ @Bean("redisClusterConfig") @Primary public RedisClusterConfiguration redisClusterConfig() { Map<String, Object> source = new HashMap<>(8); source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes")); RedisClusterConfiguration redisClusterConfiguration; redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source)); redisClusterConfiguration.setPassword(environment.getProperty("spring.redis.password")); return redisClusterConfiguration; } /** * 配置第一个数据源的连接工厂 * 这里注意:需要添加@Primary 指定bean的名称,目的是为了创建两个不同名称的LettuceConnectionFactory * * @param redisPool * @param redisClusterConfig * @return */ @Bean("lettuceConnectionFactory") @Primary public LettuceConnectionFactory lettuceConnectionFactory(GenericObjectPoolConfig redisPool, @Qualifier("redisClusterConfig") RedisClusterConfiguration redisClusterConfig) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).build(); return new LettuceConnectionFactory(redisClusterConfig, clientConfiguration); } /** * 配置第一个数据源的RedisTemplate * 注意:这里指定使用名称=factory 的 RedisConnectionFactory * 并且标识第一个数据源是默认数据源 @Primary * * @param redisConnectionFactory * @return */ @Bean("redisTemplate") @Primary public RedisTemplate redisTemplate(@Qualifier("lettuceConnectionFactory") RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(stringRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(stringRedisSerializer); template.afterPropertiesSet(); return template; } @Bean public GenericObjectPoolConfig redisPool2() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMinIdle(Convert.toInt(min_idle)); config.setMaxIdle(Convert.toInt(max_idle)); config.setMaxTotal(Convert.toInt(max_active)); config.setMaxWaitMillis(Convert.toInt(max_wait)); return config; } @Bean public RedisStandaloneConfiguration redisConfig2() { RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(host,Convert.toInt(port)); return redisConfig; } @Bean("factory2") public LettuceConnectionFactory factory2(@Qualifier("redisPool2") GenericObjectPoolConfig config, @Qualifier("redisConfig2") RedisStandaloneConfiguration redisConfig) {//注意传入的对象名和类型RedisStandaloneConfiguration LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(redisConfig, clientConfiguration); } /** * 单实例redis数据源 * * @param connectionFactory * @return */ @Bean("redisTemplateSingle") public RedisTemplate<String, Object> redisTemplateSingle(@Qualifier("factory2")LettuceConnectionFactory connectionFactory) {//注意传入的对象名 RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(redisSerializer); redisTemplate.setValueSerializer(redisSerializer); redisTemplate.setHashKeySerializer(redisSerializer); redisTemplate.setHashValueSerializer(redisSerializer); return redisTemplate; } }
使用单实例redis
/** * redis 单节点 */ @Resource(name = "redisTemplateSingle") private RedisTemplate redisTemplateSingle;
使用redis集群
/** * redis 集群 */ @Resource(name = "redisTemplate") private RedisTemplate redisTemplate;
关于“springboot redis如何使用lettuce配置多数据源”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。