如何在springboot中对redis进行配置

如何在springboot中对redis进行配置?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了柯坪免费建站欢迎大家使用!

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


      redis.clients
      jedis
      2.9.0
    

      org.springframework.data
      spring-data-redis
    

      org.springframework.boot
      spring-boot-configuration-processor
      true
    

使用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:

如何在springboot中对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 RedisConfiguration {
  private RedisProperties redisProperties;

  public RedisConfiguration(RedisProperties redisProperties) {
    this.redisProperties = redisProperties;
  }
  @Bean
  public JedisCluster jedisCluster() {
    Integer[] ports = redisProperties.getPorts();
    String host = redisProperties.getIp();
    Set 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 set(String key, Object value) {
    Map 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 del(String key) {
    Map 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);
  }
}

注意不能在service层中注入service,如果需要可以在controller层将redisService做为参数传递进去,如果在service层中注入其他的service对象,可能造成事务的串联,读到脏数据。

关于如何在springboot中对redis进行配置问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网站题目:如何在springboot中对redis进行配置
文章来源:http://bzwzjz.com/article/gdcdos.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 重庆网站建设 四川成都网站制作 手机网站制作 手机网站制作 成都网站建设 企业网站设计 重庆电商网站建设 成都网站建设 品牌网站建设 成都网站设计 专业网站设计 成都网站建设推广 企业网站制作 网站建设公司 营销型网站建设 成都营销网站制作 成都网站制作 宜宾网站设计 定制网站设计 网站建设费用 响应式网站建设 成都网站设计