[5]Hystrix-指标流

要使Hystrix指标流包需要引入依赖spring-boot-starter-actuator,这将作为一个管理端点暴露为/hystrix.stream

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

英文原文:
13.4 Hystrix Metrics Stream
To enable the Hystrix metrics stream include a dependency on spring-boot-starter-actuator. This will expose the /hystrix.stream as a management endpoint.


org.springframework.boot
spring-boot-starter-actuator

[4]Hystrix-健康指标

连接断路器的状态也在调用应用程序的/health端点中暴露。

{
    "hystrix": {
        "openCircuitBreakers": [
            "StoreIntegration::getStoresByLocationLink"
        ],
        "status": "CIRCUIT_OPEN"
    },
    "status": "UP"
}

英文原文:
13.3 Health Indicator
The state of the connected circuit breakers are also exposed in the /health endpoint of the calling application.

{
“hystrix”: {
“openCircuitBreakers”: [
“StoreIntegration::getStoresByLocationLink”
],
“status”: “CIRCUIT_OPEN”
},
“status”: “UP”
}

[1]Hystrix-断路器:Hystrix客户端

Netflix创建了一个名为Hystrix的库,它实现了断路器模式。在微服务体系结构中,有多个服务调用层是很常见的。

HystrixGraph

图1所示。Microservice图

服务级别较低的服务失败可能导致级联故障一直到客户端浏览器。当对某个特定服务的调用达到某个阈值时(5秒内的20次故障是在Hystrix上的默认值),断路器就会打开,调用也不会被调用。在出现错误和开放断路的情况下,开发人员可以提供备用方案。

HystrixFallback

图2。Hystrix的fallback避免了级联故障

拥有一个开放的断路器可以阻止连锁的失败,并允许被淹没或失败的服务时间来治愈。fallback可以是另一个受保护的断路器调用、静态数据或一个正常的空值。fallback可能是一个链式调用,所以第一个fallback会产生一些其他的业务调用,而这些调用反过来又会返回到静态数据。

英文原文:
13. Circuit Breaker: Hystrix Clients
Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.

Figure 13.1. Microservice Graph

HystrixGraph

A service failure in the lower level of services can cause cascading failure all the way up to the user. When calls to a particular service is greater than circuitBreaker.requestVolumeThreshold (default: 20 requests) and failue percentage is greater than circuitBreaker.errorThresholdPercentage (default: >50%) in a rolling window defined by metrics.rollingStats.timeInMilliseconds (default: 10 seconds), the circuit opens and the call is not made. In cases of error and an open circuit a fallback can be provided by the developer.

Figure 13.2. Hystrix fallback prevents cascading failures

HystrixFallback

Having an open circuit stops cascading failures and allows overwhelmed or failing services time to heal. The fallback can be another Hystrix protected call, static data or a sane empty value. Fallbacks may be chained so the first fallback makes some other business call which in turn falls back to static data.

[3]Hystrix-传播安全上下文或使用Spring作用域

如果您希望某些线程本地上下文传播到@HystrixCommand中,默认的声明将不起作用,因为它在线程池中执行该命令(以防超时)。您可以使用一些配置,或者直接在注解中使用与调用者相同的线程,通过要求它使用不同的“隔离策略”。例如:

@HystrixCommand(fallbackMethod = "stubMyService",
    commandProperties = {
      @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
    }
)
...

如果使用@SessionScope或@RequestScope,该方法也同样适用。你会知道什么时候你需要这么做因为一个运行时异常说它不能找到作用域的上下文。
您还可以设置hystrix.shareSecurityContext属性为true。这样做将自动配置一个Hystrix并发策略插件钩子,它将把安全上下文从主线程转移到Hystrix命令使用的一个线程。Hystrix不允许多个Hystrix并发策略注册这样一种扩展机制可以通过声明自己HystrixConcurrencyStrategy为Spring bean。Spring Cloud将在Spring上下文中查找它的实现,并将其封装到自己的插件中。

英文原文:
13.2 Propagating the Security Context or using Spring Scopes
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”)
}
)

The 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.

You also have the option to set the hystrix.shareSecurityContext property to true. Doing so will auto configure an Hystrix concurrency strategy plugin hook who will transfer the SecurityContext from your main thread to the one used by the Hystrix command. Hystrix does not allow multiple hystrix concurrency strategy to be registered so an extension mechanism is available by declaring your own HystrixConcurrencyStrategy as a Spring bean. Spring Cloud will lookup for your implementation within the Spring context and wrap it inside its own plugin.

[2]Hystrix-怎样引入Hystrix

在你的项目中引入Hystrix
MAVEN依赖方式如下:
group: org.springframework.cloud
artifact-id: spring-cloud-starter-hystrix

SpringBoot例子程序:

@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设计库提供的。Spring Cloud自动将Spring bean封装在与Hystrix断路器连接的代理中。断路器计算何时开启和关闭电路,以及万一发生故障时该做什么。

要配置@HystrixCommand,您可以使用commandProperties属性和一个@HystrixCommand注释列表。详情请见这里。有关属性的详细信息,请参阅Hystrix wiki

英文原文:
13.1 How to Include Hystrix
To include Hystrix in your project use the starter with group org.springframework.cloud and artifact id spring-cloud-starter-hystrix. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

Example boot app:

@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 */;
    }
}

The @HystrixCommand is provided by a Netflix contrib library called “javanica”. Spring Cloud automatically wraps Spring beans with that annotation in a proxy that is connected to the Hystrix circuit breaker. The circuit breaker calculates when to open and close the circuit, and what to do in case of a failure.

To configure the @HystrixCommand you can use the commandProperties attribute with a list of @HystrixProperty annotations. See here for more details. See the Hystrix wiki for details on the properties available.

编程到底应该怎么学

作为一个资深的码农,凭借多年的坎坷,我想对于这个问题我可以说些什么。算是给大家抛砖引玉,自己也想想这个问题。

1、未来的世界里,我们是否都会被淘汰

正所谓难者不会、会者不难,有些人总是说自己脑子笨,天生就不是学技术的料。如果你现在还没到50岁,那么你最好不要这么想(虽然Kevin也没到50岁)。

为什么这么说?现在的社会进步、科技进步的速度太快了,我作为一个在北京互联网行业打拼的技术人员,每天接触大量的前沿技术、资讯,依然感觉到不可思议。似乎自己的能力总是赶不上社会的进步。
>阅读全部_

《JAVA高并发访问限流》

 

 

 

 

 

课程概述

在互联网时代,高并发、大数据量访问已经成为常态,怎样避免系统资源被耗尽,怎样防止系统被高并发攻击、怎样控制服务的访问速度?
本课程从为什么要进行访问限流入手,由浅入深的讲解接口限流的算法、编程实现、模拟测试。

课程地址:点击此处~立刻学习

 

 

《Memcached系列课程》

 

 

 

 

 

课程概述:
Memcached作为一款高性能的NoSql数据库、缓存中间件。被Facebook、阿里、新浪等公司广泛使用。是当今互联网行业必备技能之一、也是NoSql数据库学习的入门首选。本课程让你0基础变为Memcached大牛!
合集篇-课程地址:
点击此处~立刻学习

>阅读全部_

《Disruptor高并发框架课程》

 

 

 

 

 

课程概述:
Disruptor是目前性能最高的无锁并发编程框架之一,具有单线程每秒处理6百万订单的恐怖能力,是架构师的首选,高薪职位的垫脚石,技术大牛的不传秘技。

课程地址:
点击此处~立刻学习

>阅读全部_

《JAVA并发编程系列课程》

 

 

 

 

 

课程概述:
全球已经全面进入互联网时代,如果你还不会并发编程,那么终将被淘汰!本系列课程,总计42课时,合集篇包含基础篇、进阶篇、精通篇课程。学院可根据自身水平选择不同阶段的课程进行学习。

合集篇-课程地址:
点击此处~立刻学习

>阅读全部_