1. 程式人生 > >Spring Cloud Hystrix 定義服務降級和異常 處理

Spring Cloud Hystrix 定義服務降級和異常 處理

概述

原來的計劃是寫一篇大而全的文章,用來介紹spring cloud Hystrix,但是想了想,這樣的做法其實也並不是特別好,所以
打算把原來的文章拆分,這樣能夠看起來比較清晰。

服務降級

繼承HystrixCommand來實現
fallback是Hystrix命令執行失敗時使用的後備方法,用來實現服務的降級處理邏輯。在HystrixCommand中可以通過過載
getFallback ()方法來實現服務降級邏輯, Hystrix會在run ()執行過程中出現錯誤、超時、執行緒池拒絕、斷路器熔斷等情況時,執
行getFallback ()方法內的邏輯,比如我們可以用如下方式實現服務降級邏輯:
package com.bobo.eurekacomsumer.command;

import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;

/**
 * @author [email protected]
 * @create 2018-10-23 18:47
 **/
public class HelloCommend extends HystrixCommand<String>{

    private RestTemplate restTemplate;
public HelloCommend(Setter setter, RestTemplate restTemplate) { super(setter); this.restTemplate = restTemplate; } @Override protected String run() throws Exception { String forObject = restTemplate.getForObject("http://EUREKA-CLIENT/hello", String.class); return
forObject; } @Override protected String getFallback() { return "error"; } }
在HystrixObservableCommand實現的Hystrix命令中,我們可以通過過載resumeWithFallback方法來實現服務降級邏輯。該方
法會返回一個Observable物件,當命令執行失敗的時候, Hystrix會將Observable中的結果通知給所有的訂閱者。
使用註解實現
package com.bobo.eurekacomsumer.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

/**
 * @author [email protected]
 * @create 2018-10-21 10:59
 **/
@Service
public class HelloService {

    @Autowired
    @Qualifier(value = "restTemplate")
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFeedback")
    public String helloConsumer () {
        return restTemplate.getForEntity("http://EUREKA-CLIENT/hello",
                String.class).getBody();
    }

    public String helloFeedback() {
        return "error";
    }
}

在使用註解來定義服務降級邏輯時,我們需要將具體的Hystrix命令與fallback實現函式定義在同一個類中,並且fallbackMethod
的值必須與實現fallback方法的名字相同。由於必須定義在一個類中,所以對於fallback的訪問修飾符沒有特定的要求,定義為
private. protected. public均可。
在上面的例子中, defaultUser方法將在getUserById執行時發生錯誤的情況下被執行。若defaultUser方法實現的並不是一個穩
定邏輯,它依然可能會發生異常,那麼我們也可以為它新增@HystrixCommand註解以生成Hystrix命令,同時使用fallbackMethod
來指定服務降級 。
服務降級的程式碼:
package com.bobo.eurekacomsumer.service;/**
 * Created by wuxiaobo on 2018/10/21.
 */

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

/**
 * @author [email protected]
 * @create 2018-10-21 10:59
 **/
@Service
public class HelloService2 {

    @Autowired
    @Qualifier(value = "restTemplate")
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFeedback1")
    public String helloConsumer () {
        return restTemplate.getForEntity("http://EUREKA-CLIENT/hello",
                String.class).getBody();
    }

    @HystrixCommand(fallbackMethod = "helloFeedback")
    public String helloFeedback1() {
        return "Hello World";
    }

    
    public String helloFeedback() {
        return "error";
    }
}

異常處理

	當Hystrix命令因為異常(除了HystrixBadRequestException的異常)進入服務降級邏輯之後,往往需要對不同異常做針對性的處
理,那麼我們如何來獲取當前丟擲的異常呢?
	在以傳統繼承方式實現的Hystrix命令中,我們可以用getFallback ()方法通過Throwable getExecutionException ()方法來獲取具
體的異常,通過判斷來進入不同的處理邏輯。
	除了傳統的實現方式之外,註解配置方式也同樣可以實現異常的獲取。它的實現也非,常簡單,只需要在fallback實現方法的參
數中增加Throwable e物件的定義,這樣在方法內部就可以獲取觸發服務降級的具體異常內容了,比如:
@HystrixCommand (fallbackMethod = "fallbacki")
User getUserById (String id) {
	throw new RuntimeException ("getUserById command failed");
}
User fallback1 (String id, Throwable e) {
	assert "getUserById command failed".equals (e.getMessage ());
}

相關推薦

Spring Cloud Hystrix 定義服務降級異常 處理

概述 原來的計劃是寫一篇大而全的文章,用來介紹spring cloud Hystrix,但是想了想,這樣的做法其實也並不是特別好,所以 打算把原來的文章拆分,這樣能夠看起來比較清晰。 服務降級 繼承H

Spring CloudHystrix服務降級異常處理

上篇文章我們看了自定義Hystrix請求命令的問題,使小夥伴們對Hystrix的使用有了進一步的瞭解,之前兩篇文章都有涉及到一個叫做fallbackMethod的東西,我們之前沒有細說這個東西,今天我們就來詳細說說這個fallbackMethod,也就是服務降級

Spring Cloud Hystrix實現服務短路服務降級

Spring Cloud Hystrix實現服務短路和服務降級 個人部落格 之前文章 Spring Cloud 服務註冊和發現 Spring Cloud 服務端高可用 Spring Cloud Ribbon實現負載均衡 背景 ​ 在微服務架構中,我們將系統拆分成了一個個的服務單元

Spring Cloud Netflix》 -- 服務註冊服務發現-Eureka的常用配置

cti project ica 地址 cat 清理 class ict course 一、版本的說明 Angel版本對應Spring Boot 1.2.x,可以使用Spring Boot 1.3.x; Brixton版本對應Spring Boot 1.3.x,可以使用Spr

Spring Cloud Netflix》-- 服務註冊服務發現-Eureka的服務認證集群

cluster word self. app server 同步 not eat wal 一、 Eureka的服務認證 1、 服務端添加依賴 <dependency> <groupId>org.springframework.boot&

幹貨分享微服務spring-cloud(8.服務治理配置中心Spring-cloud-zooke)

not found span zookeepe service get() eight request wire autowired 8.1. 服務治理Spring-cloud-zookeeper提供的模式包括服務發現和配置,配置動態更新不需要手動請求/refresh

SpringCloud 熔斷引數配置說明 Spring Cloud Hystrix服務容錯保護)

原 Spring Cloud Hystrix(服務容錯保護) 2017年06月26日 17:57:42 jack281706 閱讀數:3763

Spring Cloud Eureka 搭建服務客戶端

前言 在Eureka的服務治理體系中,主要分為服務端和客戶端 服務端:註冊中心 構建高可用的註冊中心集群后,每個註冊中心也是體系中的客戶端,只是註冊中心除了作為客戶端之外,還為其他客戶端提供了服務註冊的特殊功能) 客戶端:提供介面的微服務應用 Spri

Spring Cloud Consul 實現服務註冊發現

Spring Cloud 是一個基於 Spring Boot 實現的雲應用開發工具,它為基於 JVM 的雲應用開發中涉及的配置管理、服務發現、斷路器、智慧路由、微代理、控制匯流排、全域性鎖、決策競選、分散式會話和叢集狀態管理等操作提供了一種簡單的開發方式。通過 Spring Boot 風格進行再封裝遮蔽掉了

spring-cloud-hystrix-dasboard服務調用監控

actor gin 可視化 集群 輸出信息 圖形化監控 圖形化 項目 type   除了隔離依賴服務的調用以外,hystrix還提供了準實時的調用監控(hystrix dashboard),hystrxi會持續的記錄所有通過hyxtrix發起的請求的執行信息,並以統計報

spring-cloud-kubernetes的服務發現輪詢實戰(含熔斷)

本文是《spring-cloud-kubernetes實戰系列》的第四篇,主要內容是在kubernetes上部署兩個應用:Web-Service和Account-Service,通過spring-cloud-kubernetes提供的註冊發現能力,實現Web-Service呼叫Account-Service提

Spring Cloud構建微服務架構-Hystrix服務降級

static 原因 架構 一個個 policy 消費者 兩個 comm 以及 在微服務架構中,我們將系統拆分成了一個個的服務單元,各單元應用間通過服務註冊與訂閱的方式互相依賴。由於每個單元都在不同的進程中運行,依賴通過遠程調用的方式執行,這樣就有可能因為網絡原因或是依賴服務

Spring Cloud Feign 中使用Hystrix進行請求降級快速失敗

前言 微服務中經常會用到熔斷器來增強服務依賴的穩定性,他可以在網路連線緩慢,資源繁忙,暫時不可用,服務離線等情況中進行服務的快速失敗,並可自我恢復,以避免請求執行緒的堆積造成大量資源的浪費。 相信讀者看這篇文章的目的都是解決實際問題,並不是來看我分析原始碼的,如果對原始碼感興趣的我推薦《重新

服務spring cloudHystrix簡介通過方式整合H

Hystrix簡介 Hystrix是由Netflix開源的延遲和容錯庫,用於隔離訪問遠端系統、服務或者第三方庫,防止級聯失敗,從而提升系統的可用性與容錯性。Hystrix主要通過以下幾點實現延遲和容錯。 包裹請求:使用HystrixCommand(或HystrixObservable

Spring Cloud構建微服務架構:服務容錯保護(Hystrix服務降級

tro sco load 服務架構 延遲 正常 map ati href 動手試一試 在開始使用Spring Cloud Hystrix實現斷路器之前,我們先拿之前實現的一些內容作為基礎,其中包括: eureka-server工程:服務註冊中心,端口:1001 eurek

Spring Cloud構建微服務架構 服務容錯保護(Hystrix服務降級)【Dalston版】

前言 在微服務架構中,我們將系統拆分成了一個個的服務單元,各單元應用間通過服務註冊與訂閱的方式互相依賴。由於每個單元都在不同的程序中執行,依賴通過遠端呼叫的方式執行,這樣就有可能因為網路原因或是依賴服務自身問題出現呼叫故障或延遲,而這些問題會直接導致呼叫方的對外服務也出現延遲,若此時呼叫方的請求不斷

第五章 服務容錯保護:Spring Cloud Hystrix

目標 作用 發熱 防止 第三方庫 控制 信號 系列 微服務   在微服務架構中,我們將系統拆分為很多個服務,各個服務之間通過註冊與訂閱的方式相互依賴,由於各個服務都是在各自的進程中運行,就有可能由於網絡原因或者服務自身的問題導致調用故障或延遲,隨著服務的積壓,可能會導致服務

Spring Cloud 八:服務容錯保護(Hystrix斷路器)【Dalston版】

sta 什麽 star 根據 .com 一次 href 進行 響應 斷路器 斷路器模式源於Martin Fowler的Circuit Breaker一文。“斷路器”本身是一種開關裝置,用於在電路上保護線路過載,當線路中有電器發生短路時,“斷路器”能夠及時的切斷故障電路,防止

Spring Cloud構建微服務架構Hystrix監控面板

Spring Cloud Spring Boot mybatis 在Spring Cloud中構建一個Hystrix Dashboard非常簡單,只需要下面四步: 創建一個標準的Spring Boot工程,命名為:hystrix-dashboard。編輯pom.xml,具體依賴內容如下: <

Spring Cloud構建微服務架構—Hystrix斷路器

能夠 電路 處理 觸發 就會 熔斷器 邏輯 響應 保險絲 斷路器模式源於Martin Fowler的Circuit Breaker一文。“斷路器”本身是一種開關裝置,用於在電路上保護線路過載,當線路中有電器發生短路時,“斷路器”能夠及時的切斷故障電路,防止發生過載、發熱、甚