hystrix熔断 简介_Hystrix简介–总结
hystrix熔斷 簡介
這是其他兩篇文章的后續文章– 動機 ,說明為什么在分布式系統中需要類似Hystrix的內容以及Hystrix的基本介紹 。
這將是我的Hystrix旅程的總結,其中包含各種屬性的詳細信息,這些屬性可以進行調整以更改Hystrix的行為,并涉及一些高級概念
調整Hystrix行為
Hystrix的配置在此Wiki中進行了解釋,簡要介紹了兩個主要的組來控制Hystrix的屬性,
屬性遵循Wiki中說明的優先順序,這里我將重點介紹通過屬性文件指定的屬性。
對于示例命令,定義了以下方式:
public class HelloWorldCommand extends HystrixCommand<String> {private static final Logger logger = LoggerFactory.getLogger(HelloWorldCommand.class);private final String name;public HelloWorldCommand(String name) {super(HystrixCommandGroupKey.Factory.asKey("default"));this.name = name;}@Overrideprotected String run() throws Exception {logger.info("HelloWorld Command Invoked");return "Hello " + name;} }可以調整的第一個行為是在線程池中執行命令還是與調用方(SEMAPHORE策略類型)執行線程相同。 如果執行在線程池中,則可以設置請求超時。
hystrix.command.HelloWorldCommand.execution.isolation.strategy=THREAD hystrix.command.HelloWorldCommand.execution.isolation.thread.timeoutInMilliseconds=1000第二種行為是斷路器,它根據在滾動時間窗內收集的信息進行工作,這種方式進行配置,例如持續10秒:
hystrix.command.HelloWorldCommand.metrics.rollingStats.timeInMilliseconds=10000在此窗口中,如果某個百分比的故障(例如50%)發生在請求閾值(例如10秒內發生20個故障),則電路斷開,其配置如下所示:
hystrix.command.HelloWorldCommand.circuitBreaker.requestVolumeThreshold=20 hystrix.command.HelloWorldCommand.circuitBreaker.errorThresholdPercentage=50電路斷開后,它將保持這種狀態并保持以下設置的時間,在這種情況下為5秒:
hystrix.command.HelloWorldCommand.circuitBreaker.sleepWindowInMilliseconds=5000線程池設置是使用指定的組密鑰控制的,在本示例中稱為默認組密鑰。 不過,也可以將特定的“ Threadpool Key”指定為構造函數的一部分。
hystrix.threadpool.default.coreSize=10 hystrix.threadpool.default.queueSizeRejectionThreshold=5在這里,可以并行運行10個命令,而其他5個則保留在隊列中,超過該隊列將拒絕請求。
要求折疊
Tomaz Nurkiewicz在他的博客網站NoBlogDefFound中做了出色的解釋Request Collapsing的工作 。 我的示例有些簡化,請考慮以下情況,其中有很多請求要檢索給定id的Person,方法如下:
public class PersonService {public Person findPerson(Integer id) {return new Person(id, "name : " + id);}public List<Person> findPeople(List<Integer> ids) {return ids.stream().map(i -> new Person(i, "name : " + i)).collect(Collectors.toList());} }該服務以固定的響應進行響應,但假定該調用是對遠程數據存儲的。 還可以看到,該服務實現了一個批處理方法,以在給定ID列表的情況下檢索人員列表。
請求折疊是一項功能,它將一段時間內發生的多個用戶請求批量處理為一個這樣的遠程呼叫,然后將響應散發回用戶。
可以通過以下方式定義采用一組id并獲得人員響應的hystrix命令:
public class PersonRequestCommand extends HystrixCommand<List<Person>>{private final List<Integer> ids;private final PersonService personService = new PersonService();private static final Logger logger = LoggerFactory.getLogger(PersonRequestCommand.class);public PersonRequestCommand(List<Integer> ids) {super(HystrixCommandGroupKey.Factory.asKey("default"));this.ids = ids;}@Overrideprotected List<Person> run() throws Exception {logger.info("Retrieving details for : " + this.ids);return personService.findPeople(this.ids);} }到目前為止,這很簡單,復雜的邏輯現在在RequestCollapser中,如下所示:
package aggregate.commands.collapsed;import com.netflix.hystrix.HystrixCollapser; import com.netflix.hystrix.HystrixCollapserKey; import com.netflix.hystrix.HystrixCollapserProperties; import com.netflix.hystrix.HystrixCommand;import java.util.Collection; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors;public class PersonRequestCollapser extends HystrixCollapser<List<Person>, Person, Integer> {private final Integer id;public PersonRequestCollapser(Integer id) {super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey("personRequestCollapser")).andCollapserPropertiesDefaults(HystrixCollapserProperties.Setter().withTimerDelayInMilliseconds(2000)));this.id = id;}@Overridepublic Integer getRequestArgument() {return this.id;}@Overrideprotected HystrixCommand<List<Person>> createCommand(Collection<CollapsedRequest<Person, Integer>> collapsedRequests) {List<Integer> ids = collapsedRequests.stream().map(cr -> cr.getArgument()).collect(Collectors.toList());return new PersonRequestCommand(ids);}@Overrideprotected void mapResponseToRequests(List<Person> batchResponse, Collection<CollapsedRequest<Person, Integer>> collapsedRequests) {Map<Integer, Person> personMap = batchResponse.stream().collect(Collectors.toMap(Person::getId, Function.identity()));for (CollapsedRequest<Person, Integer> cr: collapsedRequests) {cr.setResponse(personMap.get(cr.getArgument()));}} }這里發生了一些事情,首先,參數化類型簽名中的類型指示響應的類型(List <Person>),調用者期望的響應類型(Person)和請求的請求類型(請求的ID)。人)。 然后有兩種方法,一種是創建批處理命令,第二種是將響應映射回原始請求。
現在,從用戶的角度來看,這沒有多大變化,就好像對單個命令一樣進行調用,并且Request Collapsing處理批處理,分派和映射回響應。 這是示例測試的樣子:
@Test public void testCollapse() throws Exception {HystrixRequestContext requestContext = HystrixRequestContext.initializeContext();logger.info("About to execute Collapsed command");List<Observable<Person>> result = new ArrayList<>();CountDownLatch cl = new CountDownLatch(1);for (int i = 1; i <= 100; i++) {result.add(new PersonRequestCollapser(i).observe());}Observable.merge(result).subscribe(p -> logger.info(p.toString()), t -> logger.error(t.getMessage(), t), () -> cl.countDown());cl.await();logger.info("Completed executing Collapsed Command");requestContext.shutdown(); }結論
Hystrix的功能遠遠不止我在此介紹的內容。 它確實是一個很棒的庫,對于創建彈性系統至關重要。我已經開始欣賞設計這個出色的庫所花費的大量思考過程。
翻譯自: https://www.javacodegeeks.com/2015/11/gentle-introduction-to-hystrix-wrapup.html
hystrix熔斷 簡介
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的hystrix熔断 简介_Hystrix简介–总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓厘米秀怎么关(安卓厘米秀)
- 下一篇: ddos流量购买(5g流量攻击ddos价