SpringCloud 如何包括Hystrix

2023-11-22 11:44 更新

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

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

@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由一個名為“ javanica ”的Netflix contrib庫提供。Spring Cloud將帶有注釋的Spring beans自動包裝在與Hystrix斷路器連接的代理中。斷路器計算何時斷開和閉合電路,以及在發(fā)生故障時應采取的措施。

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

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號