springboot配置redis过程详解

 更新时间:2019年09月03日 16:57:55   作者:Zs夏至  
这篇文章主要介绍了springboot配置redis过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在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 RedisConfiguration {
  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);
  }
}

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

该方法需要使用到jsonUtil类,将数据转为json字符串存储

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 老生常谈java中的数组初始化

    老生常谈java中的数组初始化

    下面小编就为大家带来一篇老生常谈java中的数组初始化。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Java基于控制台界面实现ATM系统

    Java基于控制台界面实现ATM系统

    这篇文章主要为大家详细介绍了Java基于控制台界面实现ATM系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • java数组及arrays类对数组的操作实例

    java数组及arrays类对数组的操作实例

    下面小编就为大家带来一篇java数组及arrays类对数组的操作实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • 基于SpringBoot集成测试远程连接Redis服务的教程详解

    基于SpringBoot集成测试远程连接Redis服务的教程详解

    这篇文章主要介绍了基于SpringBoot集成测试远程连接的Redis服务的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • Spring Boot整合Zookeeper实现分布式锁的场景分析

    Spring Boot整合Zookeeper实现分布式锁的场景分析

    这篇文章主要介绍了Spring Boot整合Zookeeper实现分布式锁,zk实现分布式锁完全是依靠zk节点类型当中的临时序号节点来实现的,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • SpringMVC实现注解式权限验证的实例

    SpringMVC实现注解式权限验证的实例

    本篇文章主要介绍了SpringMVC实现注解式权限验证的实例,可以使用Spring MVC中的action拦截器来实现,具有一定的参考价值,有兴趣的可以了解下。
    2017-02-02
  • 解决springboot服务启动报错:Unable to start embedded contain

    解决springboot服务启动报错:Unable to start embedded contain

    这篇文章主要介绍了解决springboot服务启动报错:Unable to start embedded contain的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • java 删除文件夹中的所有内容而不删除文件夹本身的实例

    java 删除文件夹中的所有内容而不删除文件夹本身的实例

    下面小编就为大家带来一篇java 删除文件夹中的所有内容而不删除文件夹本身的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • java项目中classpath的理解

    java项目中classpath的理解

    这篇文章介绍了java项目中classpath的理解,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • SpringBoot + MyBatis-Plus构建树形结构的几种方式

    SpringBoot + MyBatis-Plus构建树形结构的几种方式

    在实际开发中,很多数据都是树形结构,本文主要介绍了SpringBoot + MyBatis-Plus构建树形结构的几种方式,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08

最新评论