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

springboot接入cachecloud redis示例實(shí)踐

 更新時(shí)間:2019年10月28日 10:28:28   作者:xiabin111222  
這篇文章主要介紹了springboot接入cachecloud redis示例實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

最近項(xiàng)目中需要接入  Redis CacheCloud,   CacheCloud是一個(gè)開源的 Redis 運(yùn)維監(jiān)控云平臺,功能十分強(qiáng)大,支持Redis 實(shí)例自動部署、擴(kuò)容、碎片管理、統(tǒng)計(jì)、監(jiān)控等功能, 特別是支持單機(jī)、sentinel 、cluster三種模式的自動部署,搭建redis集群一步到位輕松搞定。

java項(xiàng)目中 接入 CacheCloud redis的方式主要有兩種。

第一種就是在 CacheCloud 上創(chuàng)建好redis實(shí)例后將對應(yīng)的IP,端口直接配置以配置形式應(yīng)用到項(xiàng)目中,優(yōu)點(diǎn)是通用性好,原有項(xiàng)目改造成本低,不過萬一后期CacheCloud上對redis進(jìn)行管理擴(kuò)容,那只能手動把每個(gè)項(xiàng)目的redis配置都改一遍了。

第二種CacheCloud 上創(chuàng)建好實(shí)例后有一個(gè)對應(yīng)的appId,程序調(diào)用CacheCloud 平臺的rest接口通過 appId獲取redis相關(guān)配置,將程序中的redis配置  統(tǒng)一交給CacheCloud平臺去管理維護(hù),后期管理和擴(kuò)容及其方便,不過程序改造成本比較高。

現(xiàn)在采用第二種方式接入,工程采用springboot,redis采用哨兵模式,redis客戶端主要用spring-data-redis和redisson,    接入流程如下:

添加配置到pom.xml文件

  <!--cachecloud 相關(guān)jar包-->
    <dependency>
      <groupId>com.sohu.tv</groupId>
      <artifactId>cachecloud-open-client-redis</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
 
    <dependency>
      <groupId>com.sohu.tv</groupId>
      <artifactId>cachecloud-open-client-basic</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
 
    <dependency>
      <groupId>com.sohu.tv</groupId>
      <artifactId>cachecloud-open-common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  
  <!--spring redis 和 redisson-->
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <exclusions>
        <exclusion>
          <artifactId>jedis</artifactId>
          <groupId>redis.clients</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.redisson</groupId>
      <artifactId>redisson</artifactId>
      <version>3.9.0</version>
    </dependency>

準(zhǔn)備配置文件  cacheCloudClient.properties,啟動項(xiàng)目時(shí)  VM參數(shù)追加 -Dcachecloud.config= 配置文件路徑

http_conn_timeout = 3000
http_socket_timeout = 5000
client_version = 1.0-SNAPSHOT
domain_url = http://192.168.33.221:8585  #cachecloud實(shí)際路徑
redis_cluster_suffix = /cache/client/redis/cluster/%s.json?clientVersion=
redis_sentinel_suffix = /cache/client/redis/sentinel/%s.json?clientVersion=
redis_standalone_suffix = /cache/client/redis/standalone/%s.json?clientVersion=
cachecloud_report_url = /cachecloud/client/reportData.json

基本思路是先通過cachecloud的restapi接口獲取并解析redis節(jié)點(diǎn)的配置信息,然后就可以按照傳統(tǒng)的訪問redis的方式進(jìn)行初始化,獲取RedisTemplate對象。

java代碼如下:

import com.alibaba.fastjson.JSONObject;
import com.sohu.tv.cachecloud.client.basic.heartbeat.ClientStatusEnum;
import com.sohu.tv.cachecloud.client.basic.util.ConstUtils;
import com.sohu.tv.cachecloud.client.basic.util.HttpUtils;
import com.sohu.tv.cachecloud.client.jedis.stat.ClientDataCollectReportExecutor;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
@Component
public class RedisProperties {
 
  public static Logger logger = LoggerFactory.getLogger(RedisProperties.class);
 
  /**
   * 構(gòu)建鎖
   */
  private static final Lock LOCK = new ReentrantLock();
 
  @Value("${cacheCloud.appId}") //cahcecloud 開通redis實(shí)例 應(yīng)用id
  private Integer appId;
 
  @Getter
  @Setter
  private String masterName;
 
  @Getter
  @Setter
  private Set<Pair<String, String>> sentinelSet = new HashSet<>();
 
  private Boolean clientStatIsOpen=true;
 
  @Getter
  @Setter
  private String password;
 
  private Boolean getConfigSuccess = false;
 
  @PostConstruct
  public void init() {
 
    while (true) {
      try {
        LOCK.tryLock(10, TimeUnit.MILLISECONDS);
        if (!getConfigSuccess) {
          /**
           * http請求返回的結(jié)果是空的;
           */
          String response = HttpUtils.doGet(String.format(ConstUtils.REDIS_SENTINEL_URL, appId));
          if (response == null || response.isEmpty()) {
            logger.warn("get response from remote server error, appId: {}, continue...", appId);
            continue;
          }
 
          /**
           * http請求返回的結(jié)果是無效的;
           */
          JSONObject jsonObject = null;
          try {
            jsonObject = JSONObject.parseObject(response);
          } catch (Exception e) {
            logger.error("heartbeat error, appId: {}. continue...", appId, e);
          }
          if (jsonObject == null) {
            logger.error("get sentinel info for appId: {} error. continue...", appId);
            continue;
          }
          int status = jsonObject.getIntValue("status");
          String message = jsonObject.getString("message");
 
          /** 檢查客戶端版本 **/
          if (status == ClientStatusEnum.ERROR.getStatus()) {
            throw new IllegalStateException(message);
          } else if (status == ClientStatusEnum.WARN.getStatus()) {
            logger.warn(message);
          } else {
            logger.info(message);
          }
 
          /**
           * 有效的請求:取出masterName和sentinels;
           */
          masterName = jsonObject.getString("masterName");
          String sentinels = jsonObject.getString("sentinels");
          for (String sentinelStr : sentinels.split(" ")) {
            String[] sentinelArr = sentinelStr.split(":");
            if (sentinelArr.length == 2) {
              sentinelSet.add(Pair.of(sentinelArr[0], sentinelArr[1]));
            }
          }
 
          //收集上報(bào)數(shù)據(jù)
          if (clientStatIsOpen) {
            ClientDataCollectReportExecutor.getInstance();
          }
          password = jsonObject.getString("password");
          getConfigSuccess = true;
          return;
        }
      } catch (Throwable e) {//容錯(cuò)
        logger.error("error in build, appId: {}", appId, e);
      } finally {
        LOCK.unlock();
      }
      try {
        TimeUnit.MILLISECONDS.sleep(200 + new Random().nextInt(1000));//活鎖
      } catch (InterruptedException e) {
        logger.error(e.getMessage(), e);
      }
    }
  }
}
import com.shunwang.buss.dispatchPay.provider.config.PropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SentinelServersConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
 
import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
import static java.util.stream.Collectors.toList;
 
 
@Configuration
public class RedisConfig {
 
  /**
   * JedisPoolConfig 連接池
   */
  @Bean
  public JedisPoolConfig jedisPoolConfig(RedisProperties properties) {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    // 最大空閑數(shù)
    jedisPoolConfig.setMaxIdle(20);
    // 連接池的最大數(shù)據(jù)庫連接數(shù)
    jedisPoolConfig.setMaxTotal(20);
    // 最大建立連接等待時(shí)間
    jedisPoolConfig.setMaxWaitMillis(3000);
    return jedisPoolConfig;
  }
 
  /**
   * 配置redis的哨兵
   */
  @Bean
  public RedisSentinelConfiguration sentinelConfiguration(RedisProperties properties) {
    RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
    // 配置redis的哨兵sentinel
    Set<RedisNode> redisNodeSet = properties.getSentinelSet().stream()
        .map(pair -> new RedisNode(pair.getLeft(), Integer.parseInt(pair.getRight())))
        .collect(Collectors.toSet());
    redisSentinelConfiguration.setSentinels(redisNodeSet);
    redisSentinelConfiguration.setMaster(properties.getMasterName());
    return redisSentinelConfiguration;
  }
 
  /**
   * 配置工廠
   */
  @Bean
  public RedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig, RedisSentinelConfiguration sentinelConfig) {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig, jedisPoolConfig);
    return jedisConnectionFactory;
  }
 
 
  @Bean
  public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
      throws UnknownHostException {
    RedisTemplate template = new RedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
    // 設(shè)置值(value)的序列化采用FastJsonRedisSerializer。
    template.setValueSerializer(fastJsonRedisSerializer);
    template.setHashValueSerializer(fastJsonRedisSerializer);
    // 設(shè)置鍵(key)的序列化采用StringRedisSerializer。
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
  }
 
  /**
   * Redisson 配置
   */
  @Bean
  public RedissonClient redissonClient(RedisProperties properties) {
    Config config = new Config();
    List<String> newNodes = properties.getSentinelSet().stream()
        .map(pa -> "redis://" + pa.getLeft() + ":" + pa.getRight()).collect(toList());
    SentinelServersConfig serverConfig = config.useSentinelServers()
        .addSentinelAddress(newNodes.toArray(new String[newNodes.size()]))
        .setMasterName(properties.getMasterName())
        .setReadMode(ReadMode.SLAVE);
 
    if (StringUtils.isNotBlank(properties.getPassword())){
      serverConfig.setPassword(properties.getPassword());
    }
    return Redisson.create(config);
  }
}

到這里我們已經(jīng)在Spring中 生成了RedisTemplate 和  RedissonClient 對象,無論是基本數(shù)據(jù)結(jié)構(gòu)操作  還是分布式鎖  都已經(jīng)輕松支持了,具體使用就不展開了

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

相關(guān)文章

  • maven-compiler-plugin版本指定方式

    maven-compiler-plugin版本指定方式

    這篇文章主要介紹了maven-compiler-plugin版本指定方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java 線程池詳解

    Java 線程池詳解

    本文給大家總結(jié)了java中的線程池的相關(guān)問題,非常的詳細(xì)也很實(shí)用,有需要的小伙伴可以參考下。
    2016-03-03
  • SpringBoot接收請求參數(shù)的四種方式總結(jié)

    SpringBoot接收請求參數(shù)的四種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于SpringBoot接收請求參數(shù)的四種方式,文中通過代碼以及圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SpringBoot具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • 詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

    詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

    本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • springboot實(shí)現(xiàn)mock平臺的示例代碼

    springboot實(shí)現(xiàn)mock平臺的示例代碼

    本文主要介紹了springboot實(shí)現(xiàn)mock平臺的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • SpringBoot?AnnotationUtils工具類的使用實(shí)例詳解

    SpringBoot?AnnotationUtils工具類的使用實(shí)例詳解

    這篇文章主要介紹了SpringBoot?AnnotationUtils工具類的使用,使用自定義注解標(biāo)記業(yè)務(wù)方法,原生Java獲取注解及AnnotationUtils工具類獲取方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • JavaScript base64 與 File 之間的互轉(zhuǎn)(操作方法)

    JavaScript base64 與 File 之間的互轉(zhuǎn)(操作方法)

    在JavaScript 中,可以使用 Blob 對象將 base64 字符串轉(zhuǎn)換為 File 對象,這篇文章主要介紹了JavaScript base64 與 File之間的互轉(zhuǎn),需要的朋友可以參考下
    2024-05-05
  • 基于jdbc處理Clob的使用介紹

    基于jdbc處理Clob的使用介紹

    本篇文章是對jdbc處理Clob的使用進(jìn)行了分析介紹,需要的朋友參考下
    2013-05-05
  • Java中向文件寫入數(shù)據(jù)的幾種常見方式分享

    Java中向文件寫入數(shù)據(jù)的幾種常見方式分享

    在日常開發(fā)中,肯定離不開要和文件打交道,今天就簡單羅列一下平時(shí)比較常用的創(chuàng)建文件并向文件中寫入數(shù)據(jù)的幾種方式,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-10-10
  • Java的split方法使用詳解

    Java的split方法使用詳解

    這篇文章主要詳細(xì)介紹了Java的split方法使用說明,十分的細(xì)致全面,有需要的小伙伴可以參考下。
    2015-07-07

最新評論

庆阳市| 胶南市| 宣化县| 苗栗县| 凭祥市| 贵德县| 安远县| 阿勒泰市| 长武县| 杨浦区| 东乡县| 沙湾县| 盘山县| 台州市| 江孜县| 永仁县| 三原县| 札达县| 阳信县| 南澳县| 绥德县| 高雄县| 福鼎市| 五莲县| 淄博市| 安徽省| 台州市| 万载县| 汾西县| 玛沁县| 静宁县| 蓝田县| 武隆县| 嘉定区| 吴旗县| 永昌县| 诏安县| 桃园县| 石泉县| 通许县| 东乡|