Hystrix Health Indicator及Metrics Stream
生活随笔
收集整理的這篇文章主要介紹了
Hystrix Health Indicator及Metrics Stream
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Propagating the Security Context or using Spring Scopeshttps://cloud.spring.io/spring-cloud-static/Brixton.SR7/傳播安全上下文,使用Spring Security的上下文,或者使用Spring的Scope,Spring他有幾種Scope,有SessionScope,和RequestScope,還有其他的ScopeThe same thing applies if you are using @SessionScope or @RequestScope. You will know when you need to do this because of a runtime exception that says it can’t find the scoped context.如果你想要線程本地的上下文,去傳播到這個(gè)注解,我們這個(gè)@HystrixCommand,它會(huì)在線程池中執(zhí)行HystrixCommand,直接使用這種注解,讓他使用不同的隔離策略If you want some thread local context to propagate into a @HystrixCommand the default declaration will not work because it executes the command in a thread pool (in case of timeouts). You can switch Hystrix to use the same thread as the caller using some configuration, or directly in the annotation, by asking it to use a different "Isolation Strategy". For example:@HystrixCommand(fallbackMethod = "stubMyService",commandProperties = {@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")}
)@HystrixProperty然后用"execution.isolation.strategy",SEMAPHORE信號(hào)量https://github.com/Netflix/Hystrix/wiki/Configuration第一個(gè)就是execution.isolation.strategy這個(gè)屬性表明哪一個(gè)隔離策略,這個(gè)方法去執(zhí)行了隔離策略This property indicates which isolation strategy HystrixCommand.run() executes with, one of the following two choices:一個(gè)是Thread,一個(gè)是SEMAPHORETHREAD — it executes on a separate thread and concurrent requests are limited by the number of threads in the thread-poolSEMAPHORE — it executes on the calling thread and concurrent requests are limited by the semaphore count一個(gè)是線程,一個(gè)是信號(hào)量,如果你配置線程的話(huà),他就會(huì)使用隔離的線程,如果你用信號(hào)量的話(huà),它會(huì)用你請(qǐng)求的線程,并發(fā)的請(qǐng)求會(huì)被信號(hào)量所限制,那我們可能就要問(wèn)了,那他這個(gè)默認(rèn)是什么呢,默認(rèn)是Thread,默認(rèn)并且推薦的配置呢,默認(rèn)并且推薦的是Thread,我們先來(lái)寫(xiě)個(gè)demo吧The default, and the recommended setting, is to run HystrixCommands using thread isolation (THREAD) and HystrixObservableCommands using semaphore isolation (SEMAPHORE).@GetMapping("/movie/{id}")@HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties = @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"))public User findById(@PathVariable Long id) {return this.restTemplate.getForObject("http://microservice-simple-provider-user/simple/" + id, User.class);}public User findByIdFallback(Long id) {User user = new User();user.setId(0L);return user;}這樣配就意味著在一個(gè)線程里運(yùn)行,請(qǐng)求它是一個(gè)線程,@HystrixCommand它是一個(gè)隔離的線程,就這樣的區(qū)別,@SessionScope,Spring的Bean他有幾種Scope,這是@SessionScope,其實(shí)就等同于Scope里面的Session,acts as a shortcut for {@code @Scope("session")} with the default<p>In this context, <em>scope</em> means the lifecycle of an instance,
such as {@code singleton}, {@code prototype}, and so forth. Scopes
provided out of the box in Spring may be referred to using the
{@code SCOPE_*} constants available in the {@link ConfigurableBeanFactory}
and {@code WebApplicationContext} interfaces.他有singleton,prototype,還有sessionScope,requestScope,還有g(shù)lobal session,就是application,如果你使用session scope和request scope,你會(huì)知道你什么時(shí)候需要這樣做,因?yàn)槟莻€(gè)時(shí)候會(huì)報(bào)一個(gè)運(yùn)行時(shí)異常,假如他找不到scope context,這個(gè)時(shí)候你可以配HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")你還可以把屬性設(shè)為true,你可以配置Hystrix并發(fā)的策略,Hystrix不允許多個(gè)線程的策略,不允許注冊(cè)多個(gè)并發(fā)策略,Spring Cloud會(huì)在SPRING的上下文查找實(shí)現(xiàn),并且將它包裝到自己的插件中,https://cloud.spring.io/spring-cloud-netflix/reference/html/#netflix-eureka-client-starterYou also have the option to set the hystrix.shareSecurityContext property to true. Doing so auto-configures a Hystrix concurrency strategy plugin hook to transfer the SecurityContext from your main thread to the one used by the Hystrix command. Hystrix does not let multiple Hystrix concurrency strategy be registered so an extension mechanism is available by declaring your own HystrixConcurrencyStrategy as a Spring bean. Spring Cloud looks for your implementation within the Spring context and wrap it inside its own plugin.https://cloud.spring.io/spring-cloud-netflix/reference/html/#netflix-eureka-client-starterhttps://github.com/spring-cloud/spring-cloud-netflix/issues/1330FEIGN + OAUTH2 calls from another thread not propagating security #1330Hi there, checkout the reference doc on 1.2.0.M1. A new property hystrix.shareSecurityContext=true will auto configure an Hyxtrik hook that will propagate the security context to the thread of your Hystrix command. Let me know if it fits your need.你可以查看一下官方文檔,在1.2.0MineStone,里程碑,那時(shí)候還不是正式版本,有一個(gè)新的屬性,hystrix.shareSecurityContext=true它會(huì)自動(dòng)配置Hystrix的一個(gè)鉤子,如果這個(gè)屬性滿(mǎn)足你這個(gè)需求,告訴我一下,什么叫SecurityContext呢,就是SpringSecurity的上下文,他就叫securityContext,https://github.com/spring-cloud/spring-cloud-netflix/issues/1336Hystrix shareSecurityContext is not working #1336Spring SecurityBTW, I don't see Spring Security Starter in your gradle. The hystrix.shareSecurityContext will be effective if Spring Security is present in the classpath. Share a sample project and I'll see if there is any means to adapt the mechanism specially for Spring Cloud OAuth without depending on Spring Security.正常是不要去配置,等到拋異常了再去配置
package com.learn.cloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import com.learn.cloud.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;@RestController
public class MovieController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/movie/{id}")@HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties = @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"))public User findById(@PathVariable Long id) {return this.restTemplate.getForObject("http://microservice-simple-provider-user/simple/" + id, User.class);}public User findByIdFallback(Long id) {User user = new User();user.setId(0L);return user;}}
?
總結(jié)
以上是生活随笔為你收集整理的Hystrix Health Indicator及Metrics Stream的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Hystrix简介及简单代码示例
- 下一篇: Hystrix Health Indic