欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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中NIO的三大核心組件詳細(xì)解析

    Java中NIO的三大核心組件詳細(xì)解析

    這篇文章主要介紹了Java中NIO的三大核心組件詳細(xì)解析,NIO的Buffer類是一個抽象類,位于java.nio包中,提供了一組更加有效的方法,用來進(jìn)行寫入和讀取的交替訪問,本質(zhì)上是一個內(nèi)存塊,既可以寫入數(shù)據(jù),也可以從中讀取數(shù)據(jù),需要的朋友可以參考下
    2023-12-12
  • springcloud整合到項目中無法啟動報錯Failed to start bean 'eurekaAutoServiceRegistration'

    springcloud整合到項目中無法啟動報錯Failed to start bean&n

    這篇文章主要介紹了springcloud整合到項目中無法啟動報錯Failed to start bean 'eurekaAutoServiceRegistration'問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • JPA @Basic單表查詢?nèi)绾螌崿F(xiàn)大字段懶加載

    JPA @Basic單表查詢?nèi)绾螌崿F(xiàn)大字段懶加載

    這篇文章主要介紹了JPA @Basic單表查詢?nèi)绾螌崿F(xiàn)大字段懶加載的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 如何使用RequestHeaders添加自定義參數(shù)

    如何使用RequestHeaders添加自定義參數(shù)

    這篇文章主要介紹了使用RequestHeaders添加自定義參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。
    2022-02-02
  • 圖解Spring框架的設(shè)計理念與設(shè)計模式

    圖解Spring框架的設(shè)計理念與設(shè)計模式

    這篇文章主要通過多圖詳細(xì)解釋Spring框架的設(shè)計理念與設(shè)計模式,需要的朋友可以參考下
    2015-08-08
  • 關(guān)于MyBatis plus條件構(gòu)造器的逐條詳解

    關(guān)于MyBatis plus條件構(gòu)造器的逐條詳解

    什么是條件構(gòu)造器呢?簡單來說,條件構(gòu)造器就是用來生成我們查數(shù)據(jù)庫的sql。它可以簡化sql代碼的編寫,靈活、方便且易于維護(hù)
    2021-09-09
  • java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法

    java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法

    這篇文章主要介紹了java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Java的增強(qiáng)for循環(huán)修改數(shù)組元素的問題小結(jié)

    Java的增強(qiáng)for循環(huán)修改數(shù)組元素的問題小結(jié)

    增強(qiáng)for循環(huán)的元素變量x,就是一個局部變量,它是引用數(shù)組當(dāng)前元素引用的副本(就相當(dāng)于上文所說的你復(fù)刻朋友的鑰匙),或者是基本數(shù)據(jù)類型的值的副本,這篇文章主要介紹了Java的增強(qiáng)for循環(huán)修改數(shù)組元素的問題小結(jié),需要的朋友可以參考下
    2024-02-02
  • 詳解spring多線程與定時任務(wù)

    詳解spring多線程與定時任務(wù)

    本篇文章主要介紹了spring多線程與定時任務(wù),詳細(xì)的介紹了spring多線程任務(wù)和spring定時任務(wù),有興趣的可以了解一下。
    2017-04-04
  • Java中的SPI機(jī)制案例分享

    Java中的SPI機(jī)制案例分享

    這篇文章主要介紹了Java中的SPI機(jī)制案例分享,文章基于Java的相關(guān)資料展開SPI的詳細(xì)介紹,SPI的全稱是Service?Provider?Interface,服務(wù)提供者接口,下文更多相關(guān)內(nèi)容介紹需要的小伙伴可以參考一下
    2022-04-04

最新評論