最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot配置redis過程詳解

 更新時間:2019年09月03日 16:57:55   作者:Zs夏至  
這篇文章主要介紹了springboot配置redis過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

在springboot中,默認(rèn)繼承好了一套完好的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")注解,就會默認(rèn)從application.properties
 * 文件中加載前綴為spring.redis的配置信息,配置文件中的配置字段與該類中的屬性一致,通過setter方法來設(shè)值
 * @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數(shù)據(jù),成功返回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根據(jù)key獲取string數(shù)據(jù)
   * @param key
   * @return
   */
  public String get(String key) {
    String jsonString = jedisCluster.get(key);
    if (jsonString==null || jsonString.equals("")) {
      return null;
    }
    return jsonString;
  }
  /**
   * 刪除string數(shù)據(jù)
   * @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;
  }
  /**
   * 設(shè)置失效時間
   * @param key
   * @param seconds
   * @return
   */
  public Long expire(String key,Integer seconds) {
    return jedisCluster.expire(key, seconds);
  }
}

注意不能在service層中注入service,如果需要可以在controller層將redisService做為參數(shù)傳遞進(jìn)去,如果在service層中注入其他的service對象,可能造成事務(wù)的串聯(lián),讀到臟數(shù)據(jù)。

該方法需要使用到j(luò)sonUtil類,將數(shù)據(jù)轉(zhuǎn)為json字符串存儲

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中object類實例分析

    java中object類實例分析

    這篇文章主要介紹了java中object類實例分析,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • java如何連續(xù)執(zhí)行多條cmd命令

    java如何連續(xù)執(zhí)行多條cmd命令

    這篇文章主要介紹了java如何連續(xù)執(zhí)行多條cmd命令的方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot+MQTT+apollo實現(xiàn)訂閱發(fā)布功能的示例

    SpringBoot+MQTT+apollo實現(xiàn)訂閱發(fā)布功能的示例

    這篇文章主要介紹了SpringBoot+MQTT+apollo實現(xiàn)訂閱發(fā)布功能的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 一文揭曉如何在Java中終止一個線程

    一文揭曉如何在Java中終止一個線程

    工作中我們經(jīng)常會用到線程,一般情況下我們讓線程執(zhí)行就完事了,那么你們有沒有想過如何去終止一個正在運(yùn)行的線程呢?本文就來帶大家一起看看
    2023-03-03
  • java實現(xiàn)插入排序算法

    java實現(xiàn)插入排序算法

    插入排序算法是一個對少量元素進(jìn)行排序的有效算法。插入排序的工作原理與打牌時整理手中的牌的做法類似,開始摸牌時,我們的左手是空的,接著一次從桌上摸起一張牌,并將它插入到左手的正確位置。
    2015-04-04
  • 關(guān)于webLucene 安裝方法

    關(guān)于webLucene 安裝方法

    webLucene是一個基于開源項目lucene實現(xiàn)站內(nèi)搜索的工具,關(guān)于它的安裝,百度得到的大多是一樣的,按照步驟也能正確安裝并運(yùn)行,需要注意的問題是
    2009-06-06
  • Spring中XML schema擴(kuò)展機(jī)制的深入講解

    Spring中XML schema擴(kuò)展機(jī)制的深入講解

    這篇文章主要給大家介紹了關(guān)于Spring中XML schema擴(kuò)展機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Spring中的AOP操作你了解嗎

    Spring中的AOP操作你了解嗎

    這篇文章主要為大家詳細(xì)介紹了Spring中的AOP操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • SpringBoot application.yml和bootstrap.yml的區(qū)別

    SpringBoot application.yml和bootstrap.yml的區(qū)別

    本文主要介紹了SpringBoot application.yml和bootstrap.yml的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 一文帶你搞懂SpringBoot中自動裝配原理

    一文帶你搞懂SpringBoot中自動裝配原理

    這篇文章主要為大家詳細(xì)介紹了SpringBoot中自動裝配原理的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考下
    2025-01-01

最新評論

灵石县| 民县| 云安县| 上虞市| 五峰| 锡林郭勒盟| 札达县| 临漳县| 勃利县| 泰州市| 东台市| 洪洞县| 尼勒克县| 平乡县| 手游| 宽甸| 弥渡县| 江陵县| 乐山市| 无为县| 宜君县| 闽清县| 临潭县| 正安县| 敖汉旗| 商城县| 同江市| 湖北省| 元阳县| 曲沃县| 利津县| 万州区| 都兰县| 白沙| 舞钢市| 曲沃县| 杨浦区| 五峰| 象山县| 无锡市| 云霄县|