1. 程式人生 > >SpringBoot 整合Jedis操作Redis快取

SpringBoot 整合Jedis操作Redis快取

  在使用SpringBoot構建SpringCloud微服務時,需要用到Redis做資料快取,提高業務邏輯的處理。所以就不得不讓SpringBoot整合Redis,但如果使用官方的Redis去操作的話,你叫麻煩,所以就使用Jedis去操作Reids,這樣操作簡便,編碼效率打打提高。這篇就介紹SpringBoot如何整合Jedis去操作Redis。

首先在application.properties檔案中加入redis的基本配置:

#多redis連線配置
spring.redis.shard.1.host = 127.0.0.1
spring.redis.shard.1.password = 
spring.redis.shard.1.port = 6379

#spring.redis.shard.2.host = 127.0.0.1
#spring.redis.shard.2.password = 
#spring.redis.shard.2.port = 6379

spring.redis.pool.maxIdle = 20
spring.redis.pool.maxTotal = 500
spring.redis.pool.numTestsPerEvictionRun = 3
spring.redis.pool.testOnBorrow = true
spring.redis.pool.blockWhenExhausted = false
spring.redis.pool.testOnReturn = false
然後配置JedisConfig配置類,注入配置檔案中的值:
package com.cictec.cloud.bus.minddleware.gps.common.jedis;

import java.io.Serializable;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.redis.pool")
public class JedisConfig implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String host;
	private int port;
	private String password;
	private Integer maxTotal;
	private Integer maxIdle;
	private Integer minIdle;
	private Long maxWaitMillis;
	private boolean testOnBorrow;
	private boolean testOnReturn;
	private boolean testWhileIdle;
	
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getMaxTotal() {
		return maxTotal;
	}
	public void setMaxTotal(Integer maxTotal) {
		this.maxTotal = maxTotal;
	}
	public Integer getMaxIdle() {
		return maxIdle;
	}
	public void setMaxIdle(Integer maxIdle) {
		this.maxIdle = maxIdle;
	}
	public Integer getMinIdle() {
		return minIdle;
	}
	public void setMinIdle(Integer minIdle) {
		this.minIdle = minIdle;
	}
	public Long getMaxWaitMillis() {
		return maxWaitMillis;
	}
	public void setMaxWaitMillis(Long maxWaitMillis) {
		this.maxWaitMillis = maxWaitMillis;
	}
	public boolean isTestOnBorrow() {
		return testOnBorrow;
	}
	public void setTestOnBorrow(boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}
	public boolean isTestOnReturn() {
		return testOnReturn;
	}
	public void setTestOnReturn(boolean testOnReturn) {
		this.testOnReturn = testOnReturn;
	}
	public boolean isTestWhileIdle() {
		return testWhileIdle;
	}
	public void setTestWhileIdle(boolean testWhileIdle) {
		this.testWhileIdle = testWhileIdle;
	}
	
}

最後配置JedisConfiguration類,生成2個工具Bean,redisTemplate和forecastRedisTemplate,在需要使用jedis的地方注入bean,可以對redis進行操作。
package com.cictec.cloud.bus.minddleware.gps.common.jedis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class JedisConfiguration {
	@Autowired
	JedisConfig redisConfig;
	public JedisConnectionFactory convertJedisConnectionFactory() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setHostName(redisConfig.getHost());
		jedisConnectionFactory.setPort(redisConfig.getPort());
		jedisConnectionFactory.setPassword(redisConfig.getPassword());
		
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(redisConfig.getMaxTotal());
		jedisPoolConfig.setMaxIdle(redisConfig.getMaxIdle());
		jedisPoolConfig.setMinIdle(redisConfig.getMinIdle());
		jedisPoolConfig.setMaxWaitMillis(redisConfig.getMaxWaitMillis());
		jedisPoolConfig.setTestOnBorrow(redisConfig.isTestOnBorrow());
		jedisPoolConfig.setTestOnReturn(redisConfig.isTestOnReturn());
		jedisPoolConfig.setTestWhileIdle(redisConfig.isTestWhileIdle());

		jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
		return jedisConnectionFactory;
	}

	@Bean(name = "redisTemplate")
	public StringRedisTemplate convertStringRedisTemplate() {
		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(convertJedisConnectionFactory());
		return stringRedisTemplate;
	}

	@Bean(name = "forecastRedisTemplate")
	public StringRedisTemplate convertStringRedisTemplate1() {
		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(convertJedisConnectionFactory());
		return stringRedisTemplate;
	}
}
使用的地方可以注入bean(redisTemplate,forecastRedisTemplate),名字在JedisConfiguration類的bean(“name”)中自行更改。如圖所示: