SpringCloud (five)-Hystrik (fuse)

Hytrixs: Mainly used for fuse and degradation processing in microservices, in short: when the program is abnormal or does not respond, it is done to avoid the global service from being paralyzed by a service , when a service unit fails , Through the fault monitoring of the fuse, the service will be downgraded, and an alternative response that meets expectations and can be returned to the caller. When the failed call reaches a certain threshold (for example: 20 calls fail within 5s), then Start the fuse mechanism

1. Service fusing:

Create a new SpringBoot project and import it in pom

        <!--hystrix熔断-->        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <!--用于被consul发现-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <!--把consul作为配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <!--actuator是监控系统健康情况的工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

Add configuration information in bootstrap, if it is not used for the time being, please comment out the configuration center

server:
  port: 8080


spring:
  application:
    name: hystrix
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
#      config:
#        enabled: true
#        format: yaml
#        default-context: ${spring.application.name}
#        prefix: config
#        data-key: data

Then write the test interface class

@RestController
public class TestController {

    @RequestMapping(value="test/{id}",method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "invalidProcess") //一旦方法失败并抛出错误信息,则自动调用@HystrixCommmand对应的方法
    public User get(@PathVariable("id") int id){
        if(id>=0&&id<=5){
            User user=new User();
            user.setId(id);
            user.setUsername("123");
            user.setPassword("123");
            user.setInfo("123");
            return user;
        }else{
            throw new RuntimeException("invalid");
        }
    }

    public User invalidProcess(@PathVariable("id") int id){
        User user=new User();
        user.setId(id);
        user.setUsername("该id"+id+"没有对应的信息");
        return user;
    }
}
public class User {
    private int id;
    private String username;
    private String password;
    private String info;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}

Then declare the fuse mechanism in the startup class

Finally we can test it: 

2. Service degradation

We will find that one method is fine, but if there are more, do you have to add a fuse method to each method as described above, which is too troublesome

 So we define all methods to an interface interface, and then let the interface call the fallback method, and uniformly handle the fuse of each method

The service degradation processing is done on the client side, it has nothing to do with the server side, so it must be done in Feign

 

Test: When I turn off the C service in the three services of A, B, and C, the degradation information returned by fallback will be displayed after calling again

 

3. Fuse monitoring (Hystrix dashboard)

Hystrix will continue to record the execution information of all requests initiated through Hystrix, and display it to users in the form of a graphical interface, including how many requests per second, how many successes, and how many failures.

1. Create a SpringBoot project, add maven dependency, configure the bootstrap.yml file (port number and consul)

2. Add @EnableHystrixDashboard to the startup category

3. Start the service, and then visit localhost:9001/hystrix, see a porcupine appears to indicate successful startup

4. How to monitor:

Pure data access: first ensure that the monitored service has been started, and then access http://localhost:8050/hystrix.stream (8050 is a service configured with a fuse mechanism)

Graphical interface: fill in http://localhost:8050/hystrix.stream to the previous hytrix porcupine startup page, delay writes the check interval, Title writes the title, and then submit it to view

7 colors: respectively represent the colors corresponding to each menu

1 circle: the circle in the upper left corner, its color represents: health (green<yellow<orange<red), the size (radius) will change according to the flow, the larger the flow, the larger the radius

Line 1: The line below the circle, the change in flow rate within 2 minutes (increasing or decreasing trend)

When you visit the 8050 port service, these graphics will change accordingly

 

 

Guess you like

Origin blog.csdn.net/hzkcsdnmm/article/details/108256671