Hystix熔斷解決雪崩問題

1.線程隔離,服務降級(服務的消費方做降級處理)

當服務繁忙時,如果服務出現異常,不是粗暴的直接報錯,而是返回一個友好的提示,雖然拒絕了用戶的訪問,但是會返回一個結果。

這就好比去買魚,平常超市買魚會額外贈送殺魚的服務。等到逢年過節,超時繁忙時,可能就不提供殺魚服務了,這就是服務的降級。

系統特別繁忙時,一些次要服務暫時中斷,優先保證主要服務的暢通,一切資源優先讓給主要服務來使用,在雙十一、618時,京東天貓都會采用這樣的策略。

pom.xml

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

UserConsumerApplication.java

 
package cn.itcast.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

//@EnableDiscoveryClient
//@SpringBootApplication
//@EnableCircuitBreaker //服務的熔斷
@SpringCloudApplication //SpringCloudApplication可以替代上面三個
public class UserConsumerDemoApplication {

@Bean
@LoadBalanced //負載均衡
public RestTemplate restTemplate() {
// 這次我們使用了OkHttp客戶端,只需要注入工廠即可
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}

public static void main(String[] args) {
SpringApplication.run(UserConsumerDemoApplication.class, args);
}
}
?

?3.UserHController.java

package cn.itcast.user.controller;import cn.itcast.user.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RequestMapping("consumerH")
public class UserHController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("{id}")@HystrixCommand(fallbackMethod = "queryUserByIdFallback")   //單個方法的超時返回public String queryUserById(@PathVariable("id") Long id){String url = "http://user-service/user/" + id;//User user = restTemplate.getForObject(url, User.class);//return  user;String user = restTemplate.getForObject(url, String.class);return user;}public String queryUserByIdFallback(@PathVariable("id") Long id){return "用戶信息查詢出現異常!";
//        User user = new User();
//        user.setId(id);
//        user.setNickName("用戶信息查詢出現異常!");
//        return user;
    }
}

==========================
配置統一超時信息
package cn.itcast.user.controller;

import cn.itcast.user.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("consumerH")
@DefaultProperties(defaultFallback = "queryUserByIdFallback")
public class UserHController {
@Autowired
private RestTemplate restTemplate;

@GetMapping("{id}")
//@HystrixCommand(fallbackMethod = "queryUserByIdFallback")
@HystrixCommand
public String queryUserById(@PathVariable("id") Long id){
String url = "http://user-service/user/" + id;
//User user = restTemplate.getForObject(url, User.class);
//return user;
String user = restTemplate.getForObject(url, String.class);
return user;
}

//public String queryUserByIdFallback(@PathVariable("id") Long id){
//return "用戶信息查詢出現異常!";
// User user = new User();
// user.setId(id);
// user.setNickName("用戶信息查詢出現異常!");
// return user;
//}

public String queryUserByIdFallback(){
return "用戶信息查詢出現異常!";
}
}

=============================================================

package cn.itcast.user.controller;

import cn.itcast.user.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("consumerH")
@DefaultProperties(defaultFallback = "queryUserByIdFallback")
public class UserHController {
@Autowired
private RestTemplate restTemplate;

@GetMapping("{id}")
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"), //響應超時時間
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
})
public String queryUserById(@PathVariable("id") Long id){
if(id % 2 ==0){
throw new RuntimeException("");
}
String url = "http://user-service/user/" + id;
String user = restTemplate.getForObject(url, String.class);
return user;
}

public String queryUserByIdFallback(){
return "用戶信息查詢出現異常!";
}
}
?

?

轉載于:https://www.cnblogs.com/wuxiang12580/p/10785643.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/449095.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/449095.shtml
英文地址,請注明出處:http://en.pswp.cn/news/449095.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Docker 環境下如何 安裝 Zookeeper

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 第一步&#xff1a;首先下載Zookeeper的鏡像文件&#xff1a; 從倉庫中pull 這個zookeeper鏡像&#xff1a;docker pull jplock/zookeep…

office教程:教你Excel 怎么樣使用信息函數

Excel如何使用信息函數信息函數專門用來返回某些指定單元格或區域的信息&#xff0c;例如獲取文件路徑、單元格格式信息或操作環境信息等。一&#xff0c;使用CELL函數返回引用單元格信息工作表中的每一個單元格都有對應的單元格格式、位置和內容等信息&#xff0c;在Excel中可…

【C基礎】指針/指針運算/二級指針/函數指針

指針定義&#xff1a; 指針是一種數據類型&#xff0c;使用它可以用來定義指針變量&#xff0c;指針變量中存儲的其實是整數&#xff0c;這種整數代表了內存的編號。指針的使用&#xff1a; 1、函數之間相獨立&#xff0c;但有些時候需要共享變量。傳參是值傳遞全局變量容易命…

中醫養生 選對方法就成功一半

在醫院門診室&#xff0c;因為腸胃不適前來看病的林先生。問及他平時的養生之道&#xff0c;他笑談&#xff0c;現在也正困惑著呢。 原來&#xff0c;最近他有兩個朋友&#xff0c;在單位體檢時分別被查出患有腎結石和膽囊炎&#xff0c;他本人最近也犯胃病。 最令人奇怪的一…

二叉查找樹,紅黑樹

漫畫算法&#xff1a;什么是紅黑樹&#xff1f;&#xff08;適合初學紅黑樹小白簡單易懂&#xff09; 2018年09月14日 09:55:54 蘇杭-Java工程師 閱讀數&#xff1a;494———————————— 二叉查找樹&#xff08;BST&#xff09;具備什么特性呢&#xff1f; 1.左子樹上所…

如何在 CentOS 7上安裝和使用 Docker Compose

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 介紹 Docker是一個很好的工具&#xff0c;但要真正充分利用它的潛力&#xff0c;最好是應用程序的每個組件都在它自己的容器中運行。對于…

WebSSH2安裝過程可實現WEB可視化管理SSH工具

目錄 Chrome web Secure Shell Extension gotty GateOne noVNCvncserver XtermjsSSH2nodejs nodejstty.js CheungSSH TriAquae https://github.com/Scirh/Python/tree/master/django https://www.smarthomebeginner.com/install-shellinabox-on-ubuntu/#64-bit https://gist.gi…

原碼反碼補碼位運算,

進制轉換&#xff1a; 十進制轉二進制&#xff1a; 求余法&#xff1a;用2對數據求余&#xff0c;然后再對商繼續求余&#xff0c;直到商為0結束&#xff0c;過程中產生的余數就是該數據的二進制(逆序)。 求權法&#xff1a;數據 - 2^(n-1) 如果可以減 第n位就是1&#xff0c;否…

一個人幸運的前提,是他有能力改變自己

很多時候&#xff0c;我們羨慕那些幸運的人&#xff0c;卻看不到他們為此做出的努力和改變。 其實&#xff0c;一個人的幸運并不是偶然的&#xff0c;美國成功哲學家金洛恩說過這么一句話&#xff1a;“成功不是追求得來的&#xff0c;而是被改變后的自己主動吸引來的。” …

劍指Offer-正則表達式匹配(Python)

1 題干內容 請實現一個函數用來匹配包括.和*的正則表達式。模式中的字符.表示任意一個字符&#xff0c;而*表示它前面的字符可以出現任意次&#xff08;包含0次&#xff09;。 在本題中&#xff0c;匹配是指字符串的所有字符匹配整個模式。 例如&#xff0c;字符串aaa與模式a.a…

Docker 制作鏡像的方式

其它制作鏡像的方式 前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 除了標準的使用 Dockerfile 生成鏡像的方法外&#xff0c;由于各種特殊需求和歷史原因&#xff0c;還提供了一些其它…

【算法】快排

快速排序 其利用的思想就是分治思想&#xff0c;最開始先從數組中隨機選擇一個元素p&#xff08;為什么隨機下面解釋&#xff09;&#xff0c;然后以這個元素對數組中的元素進行分類&#xff0c;數組左側都是小于p的元素&#xff0c; 右側都是大于等于p的元素。這樣就讓數組分成…

【C基礎】堆內存創建/釋放和內存清理函數/內存泄漏

本期涉及到了較多的指針&#xff0c;沒有徹底領悟的同學請翻閱之前的博文~ 一閃一閃亮晶晶&#xff0c;滿天都是小星星*** 什么是堆內存&#xff1a; 是進程的一個內存段(text、data、bss、heap、stack)之一&#xff0c;由程序員手動管理&#xff0c; 特點就是足夠大&#x…

19_05_01校內訓練[polygon]

題意 把一個邊長為1的正n邊形放到一個正m邊形中&#xff0c;要求m邊形完全覆蓋n邊形&#xff0c;可以有交點&#xff0c;并且中心重合。求正m邊形的最小邊長&#xff0c;至少精確到6位。要求logn計算。 思考 先考慮m|n的情況。 我們知道&#xff0c;正m邊形的邊長與可行區域&am…

六度人脈 全球最高效的人脈法則(圖)

六度人脈這一概念&#xff0c;在20世紀60年代由美國心理學家Stanley Milgram提出并驗證。 所謂六度人脈&#xff0c;即地球上所有的人都可以通過六層以內的熟人關系鏈和其他人聯系起來。 通俗地說&#xff1a;“最多通過六個人你就可以認識地球上任何一個陌生人。” SNS(社會…

[轉]numpy中的np.max 與 np.maximum區別

轉自&#xff1a;https://blog.csdn.net/lanchunhui/article/details/52700895 轉載于:https://www.cnblogs.com/xianhan/p/10609319.html

JVM 的 Finalization Delay 引起的 OOM(java.lang.OutOfMemoryError:null at sun.misc.Unsafe.allocateMemory.)

今天在壓力測試環境某一個服務出現crash了&#xff0c;經過一番檢查&#xff0c;終于發現是由于JVM的Finalization Delay引起的&#xff0c;這個問題比較特殊&#xff0c;這里記錄一下。 這個服務是用Java寫的&#xff0c;主要完成的功能是根據特定的指令文件生成mp4文件&#…

win10 php7+apache2.4的配置以及遇到的問題及解決

首先進入PHP官網下載php7的版本,我下的是PHP7.1.28,在PHP的下載頁面注意劃紅線和綠線的地方(我畫的) 1.畫了紅線的意思是請使用由apache lounge提供的編譯文件,也就是點進藍色Apache lounge這里下載. 2.畫了綠色的線的意思是用Apache的話你必須使用Thread Safe(線程安全)的PHP…

緩存區的輸入輸出,字符串常用操作,實現strlen/strcpy/strcat/strcmp函數)

輸出緩沖區&#xff1a; 程序輸入的數據并不能立即顯示在屏幕上&#xff0c;而是先存儲在輸出緩沖區中&#xff0c;滿足一些條件后才顯示出來。 1、遇到\n后 2、遇到輸入語句 3、當輸出緩沖區滿4K 4、當程序結束 5、手動刷新 fflush(stdout) 緩沖區機制可以提高數據的讀寫速度…

理性分散投資 收益袋袋平安

理財錦囊 想要投資理財&#xff0c;不光可以選擇股票和債券這類入門產品&#xff0c; 實際上&#xff0c;還可選擇其他低風險及高回報的投資產品&#xff0c;例如外匯、期貨和商品。 針對此&#xff0c;幾位分析師預測了2014年各國經濟走勢的重點&#xff0c;協助散戶們分配…