• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot配置redis过程详解

springboot 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

在springboot中,默认继承好了一套完好的redis包,可以直接使用,但是如果使用中出了错不容易找到错误的原因,因此这里使用自己配置的redis;

需要使用的三个主要jar包:

<dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
    </dependency>
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>

使用spring-boot-configuration-processor包主要是用来配置加载文件

package com.zs.springboot.config.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
 * @Company
 * @Author Zs
 * 将这个类作为spring的一个组件,添加@ConfigurationProperties(prefix = "spring.redis")注解,就会默认从application.properties
 * 文件中加载前缀为spring.redis的配置信息,配置文件中的配置字段与该类中的属性一致,通过setter方法来设值
 * @Date Create in 2019/8/30
 **/
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
  private String ip;
  private Integer[] ports;
  private Integer maxActive;
  private Integer maxWait;
  private Integer expire;
  public String getIp() {
    return ip;
  }
  public void setIp(String ip) {
    this.ip = ip;
  }
  public Integer[] getPorts() {
    return ports;
  }
  public void setPorts(Integer[] ports) {
    this.ports = ports;
  }
  public Integer getMaxActive() {
    return maxActive;
  }
  public void setMaxActive(Integer maxActive) {
    this.maxActive = maxActive;
  }
  public Integer getMaxWait() {
    return maxWait;
  }
  public void setMaxWait(Integer maxWait) {
    this.maxWait = maxWait;
  }
  public Integer getExpire() {
    return expire;
  }
  public void setExpire(Integer expire) {
    this.expire = expire;
  }
}

在application中配置redis:

然后配置redis的配置类,使用jdisCluster来操作redis:

package com.zs.springboot.config.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * @Company
 * @Author Zs
 * @Date Create in 2019/8/30
 **/
@Configuration
public class RedisConfigur<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码网^</strong>ation {
  private RedisProperties redisProperties;

  public RedisConfiguration(RedisProperties redisProperties) {
    this.redisProperties = redisProperties;
  }
  @Bean
  public JedisCluster jedisCluster() {
    Integer[] ports = redisProperties.getPorts();
    String host = redisProperties.getIp();
    Set<HostAndPort> hostAndPortSet = new HashSet<>();
    for (Integer port : ports) {
      hostAndPortSet.add(new HostAndPort(host, port));
    }
    return new JedisCluster(hostAndPortSet, redisProperties.getMaxActive(), redisProperties.getMaxWait());
  }
}

编辑redis的增删该方法:

/**
 * @Company
 * @Author Zs
 * @Date Create in 2019/8/28
 **/
@Service
public class RedisService {
  @Autowired
  private JedisCluster jedisCluster;
  private static final String SET_SUCCESS = "OK";
  /**
   * 添加string数据,成功返回code:200,失败code:404
   * @param key
   * @param value
   * @return
   */
  public Map<String, Object> set(String key, Object value) {
    Map<String, Object> map = new HashMap<>();
    String result = jedisCluster.set(key, JsonUtil.toJsonString(value));
    if (SET_SUCCESS.equals(result)) {
      map.put(Status.SUCCESS.getCodeName(), Status.SUCCESS.getCode());
    } else {
      map.put(Status.FILED.getCodeName(), Status.FILED.getCode());
    }
    return map;
  }
  /**
   * 从redis根据key获取string数据
   * @param key
   * @return
   */
  public String get(String key) {
    String jsonString = jedisCluster.get(key);
    if (jsonString==null || jsonString.equals("")) {
      return null;
    }
    return jsonString;
  }
  /**
   * 删除string数据
   * @param key
   * @return
   */
  public Map<String, Object> del(String key) {
    Map<String, Object> map = new HashMap<>();
    Long del = jedisCluster.del(key);
    if (del>0) {
      map.put("code", 200);
    } else {
      map.put("code", 404);
    }
    return map;
  }
  /**
   * 设置失效时间
   * @param key
   * @param seconds
   * @return
   */
  public Long expire(String key,Integer seconds) {
    return jedisCluster.expire(key, seconds);
  }
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot配置redis过程详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址