在使用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配置类,注入配置文件中的值:


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进行操作。


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”)中自行更改。如图所示:

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐