当前位置: 移动技术网 > IT编程>开发语言>Java > springboot配置redis过程详解

springboot配置redis过程详解

2019年09月06日  | 移动技术网IT编程  | 我要评论

在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字符串存储

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网