Fun springcloud (IV): fuse -hystrix, hystrix-dashboard

Hystrix


 

 

I. Introduction


        Characteristics of micro-service architecture is more service applications, and communicate over the network between the service layer so that support from the application, so that the presence of unavoidable coupling service dependencies between each micro. But any service application instances are not always healthy or networks are not always at peace, so once a service or a local business fails, the system will lead to unusable, we know that when accumulated to a certain degree of fault it will cause system-level disaster, that is, cascading failure, also called the avalanche effect, so micro-services need to prevent or clear these failures before they accumulated to a ceiling in order to guarantee the stability of the security system in the market has a lot of this framework to solve this problem, such as Twitter, Finagle, Netflix and Google Stubby's Hystrix etc., following a brief introduction and application under Hystrix Hystrix in SpringCloud in.

 Hystrix ( https://github.com/Netflix/Hystrix ) open source Netflix is a delay and fault-tolerant library for accessing remote isolation systems, services or third-party libraries, prevent cascading failures, so as to enhance system availability, fault tolerance resilience of the local application is an implementation tool for libraries and supermarkets mechanism breaker mode.

Two, Hystrix how to resolve dependencies isolation


       

       1, package Request: Use HystrixCommand wrapping call logic-dependent, each command executed in a separate thread using a design pattern of the "command mode";
  2, tripping mechanisms: when the error rate of a service exceeds a predetermined threshold value , MAMMALIA, either automatically or manually tripped, stopping requesting the service period;
  3, resource isolation: Hystrix for each dependent maintains a small thread pool (or semaphores). If the thread is full, the request made to the dependent will be rejected immediately, rather than waiting to accelerate the failure determination;
  4, monitoring: Hystrix near real-time monitoring of changes in configuration and operation indicators, such as success, failure , overtime, and the request is rejected and the like;
  5, the fallback mechanism: when the request fails, the timeout, rejected, or when the circuit breaker is opened, a rollback logic rollback logic provided by a developer itself, such as lack of a return provincial value;
  6, self-healing: the circuit breaker to open for some time, will automatically enter the "half-open" state, then the circuit breaker can allow access to a request dependent services, if the request is successful, the circuit breaker is closed, otherwise the circuit breaker switch "on" state;

Third, practice drills


 Caller to the service

   Because the fuse just call this side effect in the service, so we just need to change spring-cloud-consumer project-related code based on a sample code on it. Because, Feign has been so dependent on the Hystrix maven configuration without any changes.

1, corny, first on the pom-dependent (which is added before on the basis of dependence)

<!--熔断器 hystrix-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

 

 

 2, the configuration file follows the increase

Feign: 
  Hystrix: 
    enabled: Open blown to true # 
Hystrix: 
  Command: 
    default: #default effective global, service id specified application validation 
      Execution: 
        timeout: 
          # If enabled is set to false, the request times out to the ribbon control is true, then a timeout as a fuse according 
          Enabled: to true 
        Isolation: 
          the Thread: 
            timeoutInMilliseconds: 1000 # breaker timeout, default 1000ms

 

 

2, creation failed callback class

Package com.oldmonk.cloud.service.hystrix; 

Import com.oldmonk.cloud.service.remote.HelloRemote;
 Import org.springframework.stereotype.Component; 

/ ** 
 * @program: Cloud-LX 
 * @description: fuses, downgrade 
 * @author : xujingyang 
 * @Create: 2019-10-09 13:23 
 * * / 
@Component 
public  class HelloFallback the implements HelloRemote { 
    @Override 
    public String the Hello (String name) {
         return "service did not, and also transfer a hammer " ; 
    } 
}

 

 

3, service layer knows fallback attribute, a service call fails when the specified method will automatically return to class

package com.oldmonk.cloud.service.remote;

import com.oldmonk.cloud.service.hystrix.HelloFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @program: cloud-lx
 * @description: 远程服务调用
 * @author: xujingyang
 * @create: 2019-10-09 13:07
 **/
@FeignClient(name = "spring-cloud-eureka-producer", fallback = HelloFallback.class)
public interface HelloRemote {

    @RequestMapping("/hello")
    String hello(@RequestParam("name") String name);
}

 

 

4, the test (and other configuration related classes are used in the foregoing, with reference to the foregoing can not understand the blog)

    In turn start the registration center, service provider, service caller

    Enter the browser: http: // localhost: 9009 / hi / Xu Jinglei

   normal status:

    

   After manually stop service provider

   Blown state:

  

 Clearly visible, blown success!

 

 

 

Hystrix Dashboard


 

 

I. Introduction


        

        In addition to call-dependent services other than isolation, Hystrix also provides near real-time monitoring, Hystrix real time, the cumulative record of all information on the implementation of HystrixCommand, including the implementation of how many requests per second, much success, how many failures and so on. Netflix realized the monitoring of these indicators by hystrix-metrics-event-stream projects.

Second, practice drills


   

 1, corny, continue to add dependent

<!--熔断器实时监控 Dashboard-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

<!--熔断器 健康检查-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 

 

 2、继续添加配置  

# 手动注册 boot2.0以后必须加这个不然面板会有问题
management:
  endpoints:
    web:
      exposure:
        include: ["hystrix.stream","turbine.stream","health","info"]
turbine:
  appConfig: node1,node2 #配置Eureka中的serviceId列表,表明监控哪些服务
  aggregator:
    clusterConfig: default  #指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
#1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;
#2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;
#3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
  clusterNameExpression: new String("default")

 

 

2、启动类增加注解

@EnableHystrixDashboard
@EnableCircuitBreaker
package com.oldmonk.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @program: cloud
 * @description: 启动类
 * @author: xujingyang
 * @create: 2019-10-09 12:06
 **/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ErkaHystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(ErkaHystrixDashboardApplication.class);
    }
}

 

 

2、测试走起

继续依次启动那些个玩意儿~~~

 启动后访问http://localhost:9029/hystrix,会出现下面界面

 

  图中会有一些提示:

Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream
Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
Single Hystrix App: http://hystrix-app:port/hystrix.stream

Probably means that if you view the default cluster using the first url, check the specified cluster using the second url, monitor the use of a single application of the last one, we do so only demonstrate a single application in the input box:
HTTP: // localhost: 9029 / actuator / hystrix.stream, click on the monitor after you enter into the page.

If there is no request will first showLoading ...

Visit http: // localhost: 9029 / actuator / hystrix.stream will continue to display ping.

 

Service request http: // localhost: 9029 / hi / Xu, you can see the results of the monitoring, the first visit http: // localhost: 9029 / actuator / hystrix.stream, shown below:

 

It explained that it had returned the results of the monitoring

To the monitoring page will be shown in the following figure:

 

 

In fact, http: // localhost: 9029 / actuator / hystrix.stream return the results of the graphical display, on Hystrix Dashboard Wiki detailed description of the meaning of each indicator on the chart, as shown below

 

Fuse monitoring this single application has been completed!

 

Guess you like

Origin www.cnblogs.com/xujingyang/p/12157634.html