package com.djhu.followup.config;
/**
- Created by zw on 2017/12/13.
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
/**
- code is far away from bug with the animal protecting
- ┏┓ ┏┓
- ┏┛┻━━━┛┻┓
- ┃ ┃
- ┃ ━ ┃
- ┃ ┳┛ ┗┳ ┃
- ┃ ┃
- ┃ ┻ ┃
- ┃ ┃
- ┗━┓ ┏━┛
- ┃ ┃神兽保佑
- ┃ ┃代码无BUG!
- ┃ ┗━━━┓
- ┃ ┣┓
- ┃ ┏┛
- ┗┓┓┏━┳┓┏┛
- ┃┫┫ ┃┫┫
- ┗┻┛ ┗┻┛
-
-
*/
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(value = {Jedis.class, RedisTemplate.class})
@ConditionalOnProperty(prefix = “spring.boot.djhu.redis”, value=“enabled”, matchIfMissing =false )
public class RedisAutoConfiguration {
@Autowired
private RedisProperties redisProperties;
@Bean
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setTestOnBorrow(redisProperties.getTestOnBorrow());
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(redisProperties.getIp());
jedisConnectionFactory.setPassword(redisProperties.getAuth());
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
jedisConnectionFactory.afterPropertiesSet();
return jedisConnectionFactory;
}
@Bean
public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
属性类
package com.djhu.followup.config;
/**
- Created by zw on 2017/12/13.
*/
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = “spring.boot.djhu.redis”)
public class RedisProperties {
public static final int DEFAULT_MAXIDLE = 30;
public static final int DEFAULT_MAXACTIVE = 30;
public static final boolean DEFAULT_TESTONBORROW = true;
private String ip = “192.168.130.192”;
private Integer port = 6397;
private String auth =“hiup”;
private Integer maxIdle = DEFAULT_MAXIDLE;
private Integer maxActive = DEFAULT_MAXACTIVE;
private Boolean testOnBorrow = DEFAULT_TESTONBORROW;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getAuth() {
return auth;
}
public void setAuth(String auth) {
this.auth = auth;
}
public Integer getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public Boolean getTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(Boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}
使用redisTemplate获取jedis
package com.djhu.followup.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
- @Author:mzq
- @DATE:2019/7/2219:26
- @VERSION:1.0.0
*/
@Slf4j
@Component
public class RedisUtils implements InitializingBean {
@Autowired
private JedisConnectionFactory jedisConnectionFactory;
@Autowired
private RedisTemplate redisTemplate;
private JedisPool jedisPool;
private static Jedis jedis;
public Jedis getJedis() throws Exception {// 记的关闭jedis
// log.info(jedis.zrange(“delayKey”,0,-1).toString());
return this.jedis;
}
@Override
public void afterPropertiesSet() throws Exception {
RedisConnection conn = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
this.jedis = (Jedis) conn.getNativeConnection();
}
}