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

詳解spring cloud hystrix緩存功能的使用

 更新時間:2018年08月18日 10:02:37   作者:TS笑天  
這篇文章主要介紹了詳解spring cloudhystrix緩存功能的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

hystrix緩存的作用是

- 1.減少重復(fù)的請求數(shù),降低依賴服務(wù)的返回數(shù)據(jù)始終保持一致。
- 2.==在同一個用戶請求的上下文中,相同依賴服務(wù)的返回數(shù)據(jù)始終保持一致==。
- 3.請求緩存在run()和construct()執(zhí)行之前生效,所以可以有效減少不必要的線程開銷。

1 通過HystrixCommand類實現(xiàn)

1.1 開啟緩存功能

繼承HystrixCommand或HystrixObservableCommand,覆蓋getCacheKey()方法,指定緩存的key,開啟緩存配置。

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixRequestCache;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
import com.szss.demo.orders.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

public class UserCacheCommand extends HystrixCommand<UserVO> {
  private static final Logger LOGGER = LoggerFactory.getLogger(UserCacheCommand.class);

  private static final HystrixCommandKey GETTER_KEY= HystrixCommandKey.Factory.asKey("CommandKey");
  private RestTemplate restTemplate;
  private String username;

  public UserCacheCommand(RestTemplate restTemplate, String username) {
    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userCacheCommand")).andCommandKey(GETTER_KEY));
    this.restTemplate = restTemplate;
    this.username = username;
  }

  @Override
  protected UserVO run() throws Exception {
    LOGGER.info("thread:" + Thread.currentThread().getName());
    return restTemplate.getForObject("http://users-service/user/name/{username}", UserVO.class, username);
  }

  @Override
  protected UserVO getFallback() {
    UserVO user = new UserVO();
    user.setId(-1L);
    user.setUsername("調(diào)用失敗");
    return user;
  }

  @Override
  protected String getCacheKey() {
    return username;
  }

  public static void flushCache(String username){
    HystrixRequestCache.getInstance(GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear(username);
  }
}

1.2 配置HystrixRequestContextServletFilter

通過servlet的Filter配置hystrix的上下文。

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(filterName = "hystrixRequestContextServletFilter",urlPatterns = "/*",asyncSupported = true)
public class HystrixRequestContextServletFilter implements Filter {
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
      chain.doFilter(request, response);
    } finally {
      context.shutdown();
    }
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void destroy() {

  }
}

在不同context中的緩存是不共享的,還有這個request內(nèi)部一個ThreadLocal,所以request只能限于當(dāng)前線程。

1.3 清除失效緩存

繼承HystrixCommand或HystrixObservableCommand,在更新接口調(diào)用完成后,清空緩存。

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.szss.demo.orders.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.web.client.RestTemplate;

public class UserUpdateCacheCommand extends HystrixCommand<UserVO> {
  private static final Logger LOGGER = LoggerFactory.getLogger(UserUpdateCacheCommand.class);

  private static final HystrixCommandKey GETTER_KEY = HystrixCommandKey.Factory.asKey("CommandKey");
  private RestTemplate restTemplate;
  private UserVO user;

  public UserUpdateCacheCommand(RestTemplate restTemplate, UserVO user) {
    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userUpdateCacheCommand")));
    this.restTemplate = restTemplate;
    this.user = user;
  }

  @Override
  protected UserVO run() throws Exception {
    LOGGER.info("thread:" + Thread.currentThread().getName());
    HttpEntity<UserVO> u = new HttpEntity<UserVO>(user);
    UserVO userVO=restTemplate.postForObject("http://users-service/user",u,UserVO.class);
    UserCacheCommand.flushCache(user.getUsername());
    return userVO;
  }

//  @Override
//  protected UserVO getFallback() {
//    UserVO user = new UserVO();
//    user.setId(-1L);
//    user.setUsername("調(diào)用失敗");
//    return user;
//  }

  @Override
  protected String getCacheKey() {
    return user.getUsername();
  }
}

2 使用@CacheResult、@CacheRemove和@CacheKey標(biāo)注來實現(xiàn)緩存

2.1 使用@CacheResult實現(xiàn)緩存功能

  @CacheResult(cacheKeyMethod = "getCacheKey")
  @HystrixCommand(commandKey = "findUserById", groupKey = "UserService", threadPoolKey = "userServiceThreadPool")
  public UserVO findById(Long id) {
    ResponseEntity<UserVO> user = restTemplate.getForEntity("http://users-service/user?id={id}", UserVO.class, id);
    return user.getBody();
  }

  public String getCacheKey(Long id) {
    return String.valueOf(id);
  }

@CacheResult注解中的cacheKeyMethod用來標(biāo)示緩存key(cacheKey)的生成函數(shù)。函數(shù)的名稱可任意取名,入?yún)⒑蜆?biāo)注@CacheResult的方法是一致的,返回類型是String。

2.2 使用@CacheResult和@CacheKey實現(xiàn)緩存功能

  @CacheResult
  @HystrixCommand(commandKey = "findUserById", groupKey = "UserService", threadPoolKey = "userServiceThreadPool")
  public UserVO findById2(@CacheKey("id") Long id) {
    ResponseEntity<UserVO> user = restTemplate.getForEntity("http://users-service/user?id={id}", UserVO.class, id);
    return user.getBody();
  }

標(biāo)注@HystrixCommand注解的方法,使用@CacheKey標(biāo)注需要指定的參數(shù)作為緩存key。

2.3 使用@CacheRemove清空緩存

  @CacheRemove(commandKey = "findUserById")
  @HystrixCommand(commandKey = "updateUser",groupKey = "UserService",threadPoolKey = "userServiceThreadPool")
  public void updateUser(@CacheKey("id")UserVO user){
    restTemplate.postForObject("http://users-service/user",user,UserVO.class);
  }

@CacheRemove必須指定commandKey,否則程序無法找到緩存位置。

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

相關(guān)文章

  • java中字符串替換常用的4種方法

    java中字符串替換常用的4種方法

    在Java中String類提供了許多方便的方法來處理字符串,下面這篇文章主要給大家介紹了關(guān)于java中字符串替換常用的4種方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-03-03
  • Springboot Tomcat APR模式詳解和實踐記錄

    Springboot Tomcat APR模式詳解和實踐記錄

    這篇文章主要介紹了Springboot Tomcat APR模式詳解和實踐記錄,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-04-04
  • Java線程池的工作機制詳解

    Java線程池的工作機制詳解

    本文講述了Java線程池的工作機制,包括線程池的創(chuàng)建、任務(wù)調(diào)度、線程復(fù)用、資源管理和拒絕策略,通過合理配置線程池參數(shù),可以提升系統(tǒng)的并發(fā)性能和穩(wěn)定性
    2024-12-12
  • Java將圖片組合成PDF文件的方法

    Java將圖片組合成PDF文件的方法

    這篇文章主要為大家詳細介紹了Java將圖片組合成PDF文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Java常用流程控制語句實現(xiàn)原理解析

    Java常用流程控制語句實現(xiàn)原理解析

    這篇文章主要介紹了Java常用流程控制語句實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • MySql多表查詢 事務(wù)及DCL

    MySql多表查詢 事務(wù)及DCL

    這篇文章主要介紹了MySql多表查詢 、事務(wù)、DCL的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • log4j2日志異步打印(實例講解)

    log4j2日志異步打印(實例講解)

    下面小編就為大家?guī)硪黄猯og4j2日志異步打印(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 解決Java中由于數(shù)據(jù)太大自動轉(zhuǎn)換成科學(xué)計數(shù)法的問題

    解決Java中由于數(shù)據(jù)太大自動轉(zhuǎn)換成科學(xué)計數(shù)法的問題

    今天小編就為大家分享一篇解決Java中由于數(shù)據(jù)太大自動轉(zhuǎn)換成科學(xué)計數(shù)法的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Spring?MVC實現(xiàn)GET請求接收Date類型參數(shù)

    Spring?MVC實現(xiàn)GET請求接收Date類型參數(shù)

    這篇文章主要介紹了Spring?MVC實現(xiàn)GET請求接收Date類型參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • java文件操作之java寫文件簡單示例

    java文件操作之java寫文件簡單示例

    這篇文章主要介紹了java文件操作中的java寫文件示例,需要的朋友可以參考下
    2014-03-03

最新評論

峨眉山市| 奈曼旗| 德兴市| 阜平县| 闻喜县| 平湖市| 云龙县| 沅陵县| 阿鲁科尔沁旗| 昌宁县| 渝北区| 泗阳县| 中江县| 凤阳县| 淮阳县| 会东县| 阳江市| 高唐县| 卓尼县| 宜兰县| 乐至县| 广宁县| 德昌县| 乳山市| 靖西县| 广安市| 四会市| 宜春市| 吐鲁番市| 通化县| 岗巴县| 吴江市| 兴安县| 郯城县| 班玛县| 四会市| 棋牌| 塔河县| 西青区| 吉林市| 临邑县|