Redis 的數據庫的整合在 java 裏面提供的官方工具包:jedis,因此即使你如今使用的是 SpringBoot,那麼也繼續使用此開發包。java
在 Spring 支持的 Redis 操做之中提供有一個 RedisTemplate 處理程序類,利用這個類能夠很是方便的實現 Redis 的各類基本數 據操做。redis
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
spring: redis: host: 47.52.199.52 port: 6379 password: 123456 timeout: 1000 database: 0 jedis: pool: max-active: 4 max-idle: 8 min-idle: 2 max-wait: 100
@RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration public class DemoApplicationTests {
@Resource private RedisTemplate<String, String> redisTemplate; @Test public void testRedis(){ redisTemplate.opsForValue().set("xing","12345678"); System.out.println(redisTemplate.opsForValue().get("xing")); } }
因爲在項目的實際開發過程之中 Redis 的使用會很是的頻繁, 那麼就有可能出現這樣一種問題:如今的項目裏面要求鏈接兩 個 Redis 數據庫。SpringBoot 裏面針對於 Redis 的鏈接配置本質上只提供有一個鏈接配置項,那麼若是你真的須要進行更多的 Redis 的鏈接配置,那麼就須要本身來進行 Redis 的建立管理了。至關於直接使用spring-data-redis。spring
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
spring:
redis-two:
host: 47.52.199.52
port: 6379
password: 123456
timeout: 1000
database: 0
pool:
max-active: 10
max-idle: 8
min-idle: 2
max-wait: 100
RedisTwoConfig.java數據庫
package com.example.demo.config.redisConfig; 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.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration public class RedisTwoConfig { @Bean("redisTwo") public RedisTemplate<String, Object> getRedisTemplate( @Value("${spring.redis-two.host}") String hostName, @Value("${spring.redis-two.password}") String password, @Value("${spring.redis-two.port}") int port, @Value("${spring.redis-two.database}") int database, @Value("${spring.redis-two.pool.max-active}") int maxActive, @Value("${spring.redis-two.pool.max-idle}") int maxIdle, @Value("${spring.redis-two.pool.min-idle}") int minIdle, @Value("${spring.redis-two.pool.max-wait}") long maxWait) { System.out.println(hostName+port+password); RedisConnectionFactory factory = this.getRedisConnectionFactory( hostName, password, port, maxActive, maxIdle, minIdle, maxWait, database); // 創建Redis的鏈接 RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(factory); redisTemplate.setKeySerializer(new StringRedisSerializer()); // key的序列化類型 redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // value的序列化類型 return redisTemplate; } public RedisConnectionFactory getRedisConnectionFactory(String hostName, String password, int port, int maxActive, int maxIdle, int minIdle, long maxWait, int database) { // 是負責創建Factory的鏈接工廠類 JedisConnectionFactory jedisFactory = new JedisConnectionFactory(); jedisFactory.setHostName(hostName); jedisFactory.setPort(port); jedisFactory.setPassword(password); jedisFactory.setDatabase(database); JedisPoolConfig poolConfig = new JedisPoolConfig(); // 進行鏈接池配置 poolConfig.setMaxTotal(maxActive); poolConfig.setMaxIdle(maxIdle); poolConfig.setMinIdle(minIdle); poolConfig.setMaxWaitMillis(maxWait); jedisFactory.setPoolConfig(poolConfig); jedisFactory.afterPropertiesSet(); // 初始化鏈接池配置 return jedisFactory; } }
@RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration public class RedisTest { @Resource private RedisTemplate<String, Object> redisTemplate; @Resource private StringRedisTemplate stringRedisTemplate; @Resource(name = "redisTwo") private RedisTemplate<String, Object> redisTemplate2; @Test public void testRedis2(){ UserPO userPO = new UserPO(); userPO.setAge(25); userPO.setName("小明"); userPO.setUid(111L); redisTemplate2.opsForValue().set("user25",userPO); UserPO result = (UserPO) redisTemplate2.opsForValue().get("user25"); System.out.println(result); } }