SpringCloud 如何包括Hystrix

2023-11-22 11:44 更新

要將Hystrix包含在您的項(xiàng)目中,請(qǐng)使用起始者,其組ID為org.springframework.cloud,工件ID為??spring-cloud-starter-netflix-hystrix。有關(guān)使用當(dāng)前Spring Cloud版本Train設(shè)置構(gòu)建系統(tǒng)的詳細(xì)信息,請(qǐng)參見(jiàn)Spring Cloud項(xiàng)目頁(yè)面。

以下示例顯示了具有Hystrix斷路器的最小Eureka服務(wù)器:

@SpringBootApplication
@EnableCircuitBreaker
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

@Component
public class StoreIntegration {

    @HystrixCommand(fallbackMethod = "defaultStores")
    public Object getStores(Map<String, Object> parameters) {
        //do stuff that might fail
    }

    public Object defaultStores(Map<String, Object> parameters) {
        return /* something useful */;
    }
}

@HystrixCommand由一個(gè)名為“ javanica ”的Netflix contrib庫(kù)提供。Spring Cloud將帶有注釋的Spring beans自動(dòng)包裝在與Hystrix斷路器連接的代理中。斷路器計(jì)算何時(shí)斷開(kāi)和閉合電路,以及在發(fā)生故障時(shí)應(yīng)采取的措施。

要配置@HystrixCommand,可以將commandProperties屬性與@HystrixProperty批注一起使用。有關(guān) 更多詳細(xì)信息,請(qǐng)參見(jiàn) 此處。有關(guān) 可用屬性的詳細(xì)信息,請(qǐng)參見(jiàn)Hystrix Wiki。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)