日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Cloud对Hystrix的支持

發(fā)布時間:2023/12/3 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud对Hystrix的支持 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Spring Cloud項目為Netflix OSS Hystrix庫提供了全面的支持。 之前我已經(jīng)寫過有關如何使用原始Hystrix庫包裝遠程調用的文章。 在這里,我將探討如何將Hystrix與Spring Cloud結合使用

基本

實際上并沒有什么大不了的,這些概念僅在特定于Spring引導的增強中保留下來。 考慮一個簡單的Hystrix命令,該命令包含對Remote服務的調用:

import agg.samples.domain.Message; import agg.samples.domain.MessageAcknowledgement; import agg.samples.feign.RemoteServiceClient; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory;public class RemoteMessageClientCommand extends HystrixCommand<MessageAcknowledgement> {private static final String COMMAND_GROUP = "demo";private static final Logger logger = LoggerFactory.getLogger(RemoteMessageClientCommand.class);private final RemoteServiceClient remoteServiceClient;private final Message message;public RemoteMessageClientCommand(RemoteServiceClient remoteServiceClient, Message message) {super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP));this.remoteServiceClient = remoteServiceClient;this.message = message;}@Overrideprotected MessageAcknowledgement run() throws Exception {logger.info("About to make Remote Call");return this.remoteServiceClient.sendMessage(this.message);}@Overrideprotected MessageAcknowledgement getFallback() {return new MessageAcknowledgement(message.getId(), message.getPayload(), "Fallback message");} }

這里沒有與Spring相關的類,此命令可以直接在基于Spring的項目中使用,例如在控制器中以下列方式使用:

@RestController public class RemoteCallDirectCommandController {@Autowiredprivate RemoteServiceClient remoteServiceClient;@RequestMapping("/messageDirectCommand")public MessageAcknowledgement sendMessage(Message message) {RemoteMessageClientCommand remoteCallCommand = new RemoteMessageClientCommand(remoteServiceClient, message);return remoteCallCommand.execute();} }

Hystrix命令的行為自定義通常是通過NetflixOSS Archaius屬性執(zhí)行的,但是Spring Cloud提供了一個橋梁,使Spring定義的屬性顯示為Archaius屬性,這簡而言之意味著我可以使用Spring特定的配置文件來定義我的屬性,自定義命令行為時將可見。

因此,如果較早進行自定義,則可以使用Archaius屬性來表示HelloWorldCommand的行為,如下所示:

hystrix.command.HelloWorldCommand.metrics.rollingStats.timeInMilliseconds=10000 hystrix.command.HelloWorldCommand.execution.isolation.strategy=THREAD hystrix.command.HelloWorldCommand.execution.isolation.thread.timeoutInMilliseconds=1000 hystrix.command.HelloWorldCommand.circuitBreaker.errorThresholdPercentage=50 hystrix.command.HelloWorldCommand.circuitBreaker.requestVolumeThreshold=20 hystrix.command.HelloWorldCommand.circuitBreaker.sleepWindowInMilliseconds=5000

這可以在Spring Cloud世界中以完全相同的方式在application.properties文件或application.yml文件中通過以下方式完成:

hystrix:command:HelloWorldCommand:metrics:rollingStats:timeInMilliseconds: 10000execution:isolation:strategy: THREADthread:timeoutInMilliseconds: 5000circuitBreaker:errorThresholdPercentage: 50requestVolumeThreshold: 20sleepWindowInMilliseconds: 5000

基于注釋的方法

我個人更喜歡基于直接命令的方法,但是在Spring世界中使用Hystrix的更好方法可能是使用基于hystrix-javanica的注釋。 最好通過一個示例說明如何使用此注釋。 這是包裝在Hystrix命令中并帶有注釋的遠程調用:

import agg.samples.domain.Message; import agg.samples.domain.MessageAcknowledgement; import agg.samples.feign.RemoteServiceClient; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Service public class RemoteMessageAnnotationClient {private final RemoteServiceClient remoteServiceClient;@Autowiredpublic RemoteMessageAnnotationClient(RemoteServiceClient remoteServiceClient) {this.remoteServiceClient = remoteServiceClient;}@HystrixCommand(fallbackMethod = "defaultMessage", commandKey = "RemoteMessageAnnotationClient" )public MessageAcknowledgement sendMessage(Message message) {return this.remoteServiceClient.sendMessage(message);}public MessageAcknowledgement defaultMessage(Message message) {return new MessageAcknowledgement("-1", message.getPayload(), "Fallback Payload");}}

這些注解使用方面轉換為幕后的常規(guī)Hystrix命令,但很妙的是,在Spring Cloud項目中沒有使用該注解的儀式,它只是工作而已。 與以前一樣,如果需要自定義行為,則可以使用命令特定的屬性來完成。 一個小問題是默認情況下命令名稱是方法名稱,因此在我的示例中,命令名稱將是“ sendMessage”,我已使用注釋將其自定義為其他名稱。

  • 如果您有興趣進一步探索該示例,請參閱我的github項目 。

翻譯自: https://www.javacodegeeks.com/2015/11/spring-cloud-support-hystrix.html

總結

以上是生活随笔為你收集整理的Spring Cloud对Hystrix的支持的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。