SpringCloud distributed micro-cloud infrastructure services Part IV: Breaker (Hystrix) (Finchley version)

In the micro-service architecture, according to the service to be split into a number of service can call each other (RPC) services between a service and can be used RestTemplate + Ribbon Feign and call this Spring Cloud. In order to ensure availability, single service often cluster deployment. As the network reasons or for their own reasons, the service does not guarantee 100% available, if a single service issue arise, to call this service will be thread is blocked, at this time if the influx of a large number of requests, Servlet container thread resources will be consumed completed , resulting in paralysis of service. Between services and service dependencies, faults spread, understand springcloud architecture can be added to beg: 3536247259, micro whole service system will have disastrous consequences serious, this is the fault of the service "avalanche" effect.

To solve this problem, the industry proposed circuit breaker model.

I. Introduction breaker

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. ----摘自官网

Netflix Hystrix open source components, to achieve the cut-out mode, SpringCloud of the components were integrated. In the micro-service architecture, a request need to call multiple services are very common, as follows:
SpringCloud distributed micro-cloud infrastructure services Part IV: Breaker (Hystrix) (Finchley version)
the deeper service If a fault occurs, it will lead to cascading failures. When the call is not available for a particular service reaches a threshold (Hystric 5 seconds 20 times) circuit breaker will be opened.
SpringCloud distributed micro-cloud infrastructure services Part IV: Breaker (Hystrix) (Finchley version)
Second, prepare for work
This article is based on an article in the engineering, construction started on the first article, start the eureka-server project; project start service-hi, the port is 8762.

三、在ribbon使用断路器
改造serice-ribbon 工程的代码,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

在程序的启动类ServiceRibbonApplication 加@EnableHystrix注解开启Hystrix:


@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceRibbonApplication.class, args );
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

改造HelloService类,在hiService方法上加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串,字符串为"hi,"+name+",sorry,error!",代码如下:


@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }

}

启动:service-ribbon 工程,当我们访问http://localhost:8764/hi?name=forezp,浏览器显示:

hi forezp,i am from port:8762

此时关闭 service-hi 工程,当我们再访问http://localhost:8764/hi?name=forezp,浏览器会显示:

hi ,forezp,orry,error!

这就说明当 service-hi 工程不可用的时候,service-ribbon调用 service-hi的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。

Four, Feign used in circuit breakers
Feign are self-breaker, after the D version of the Spring Cloud, it is not turned on by default. You need to configure to open it in the configuration file, the configuration file add the following code:

feign.hystrix.enabled=true

SchedualServiceHiHystric SchedualServiceHi need to implement the interface, and Ioc is injected into the container, as follows:


@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

SchedualServiceHiHystric SchedualServiceHi need to implement the interface, and Ioc is injected into the container, as follows:


@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }
}

Start four servcie-feign project browser to open http: // localhost:? 8765 / hi name = forezp, note that at this time the project does not start service-hi, web page display:

sorry forezp

Open service-hi engineering, accessed again, the browser displays:

hi forezp,i am from port:8762

This proves that the circuit breaker played a role.

Guess you like

Origin blog.51cto.com/14622290/2458380