sppringBoot整合redis

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();
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值