Spring boot配置多个Redis数据源操作实例
在SpringBoot是项目中整合了两个Redis的操作实例,可以增加多个;
一般在一个微服务生态群中是不会出现多个Redis中间件的,所以这种场景很少见,但也不可避免,但是不建议使用,个人建议,勿喷。
- 基于Maven3.0搭建,spring1.5.9.RELEASE和JDK1.8
1、新建SpringBoot项目,添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2、application.yml配置文件
spring: redis: database: 6
3、新建RedisConfig类
package com.lilian.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; import java.lang.reflect.Method;
4、使用Java类注入多个数据源
- 数据源一
package com.lilian.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate;
- 数据源二
package com.lilian.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate;
- 数据源三同理。。。
5、随便定义一个实体类
package com.lilian.entity; import lombok.AllArgsConstructor; import lombok.Data;
6、测试方法
package com.lilian; import com.lilian.entity.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource;
7、结果
使用redis可视化工具查看是否成功;
redisresult.jpg
原文链接:https://www.cnblogs.com/shihaiming/p/10107919.html
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/32909