Eclipse configured to run SpringCloud (Hoxton + 2.2.4) micro-fuse structures Services Framework Model + Hystrix Hystrix and the Dashboard

Brief introduction

In a distributed system dependencies between services and service complex, an unavoidable situation is that some services will fail, leading to other services that depend on them appear thread blocking remote scheduling. A single point of failure for a service request will cause a user's request is blocked, the end result is a thread of depleted resources of the entire service. Because dependency services, other services that depend on will lead to the failure of the service is also in a thread is blocked state, eventually leading thread depleted resources of these services, until not available, resulting in the entire service system is not available, that the avalanche effect. In order to prevent the avalanche effect, resulting in a fuse model.

SpringCloud Netflix realized the name breaker library called Hystrix, provides a fuse function, can prevent failures in distributed systems linked appears. Hystrix is ​​prevented by isolating service access point linkage failure, and failure to provide a solution that increases the elasticity of the entire distributed system.

When the number of failures of a service API interface is less than the threshold value is set within a certain time, the fuse is in a closed state, the normal service API interface. When the number of failures API interface processing request is set greater than a threshold, it is determined that the API interface MAMMALIA, fails, the fuse opens, when requesting the API interface performs a logical fail-fast (i.e., back to the logical fallback retreat), do not perform business logic, not a request thread is blocked. Fuse in the open state, after a period of time in a semi-open state, and a normal request to perform a certain number of logic to perform fast the remaining requests will fail, if requested to perform normal logic fails, the fuse continues to open, if successful, turn off the fuse. Such fuses have the ability to repair itself.
Here Insert Picture Description
Figure B service fails for some reason, becomes unavailable, all calls to the service B timeout. B when the call fails to reach a particular threshold value (within 5 seconds occurs 20 times the default value Hystrix failure is defined above), a link will be in the open state, then all calls to the service B will not be execution, replaced by a circuit breaker provided a link open message Fallback representation. Hystrix provides the appropriate mechanism that allows developers to define the Fallbak news.

open link blocked waterfall error, can be submerged or let the wrong time to repair service. This fallback may call another Hystrix protection, static data, or legally null values. Fallbacks can form a chain structure, so the bottom of the first call Fallback other business services to return static data.

Fuses on the Ribbon and RestTemplate

POM.xml add dependencies

<!--ribbon中使用断路器-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

Modify the startup class

Plus @EnableHystrix notes, open hystrix fuse function

  • RibbonApplication.java
package org.springcloud.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class RibbonApplication {

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

Modify Service Service

On the hi () method plus @HystrixCommand comment. With this comment hi () method on Hystrix fuse function is enabled, in which:

@HystrixCommand shows that the method is hystrix package, can be isolated reliance on service, demotion, fail fast, fast retry hystrix related functions, etc.

  • fallbackMethod downscaling methods
  • commandProperties common configuration attribute, the corresponding attribute HystrixCommand may be configured, for example, thread pool or the amount of signal isolation, rules, etc. blown fuse
  • ignoreExceptions ignore the exception, not included in the default HystrixBadRequestException fail
  • groupKey () group name, default class name
  • commandKey command name, use the default name

fallbackMethod is rolled back (fallback) logic methods. In the present example, a direct return string. In the open state of the fuse, the logic performs fallback. The best fallback logic is to return some static string, it does not require complex logic, not remotely invoke other services, so easy to perform a quick failure, release thread resources. If you must invoke other services in remote fallback logic, the best in the remote call other services, also add a fuse.

  • RibbonService.java
package org.springcloud.ribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class RibbonService {
    @Autowired
    RestTemplate restTemplate;
    
    @HystrixCommand(fallbackMethod = "hiError")
    public String hi(String name){
        return restTemplate.getForObject("http://springcloud-eureka-provider/hi?name="+name,String.class);
    }
    
    public String hiError(String name){
        return "hi,"+name+", use Ribbon + hystrix, eureka-provider is down!";
    }
}

In order to start the project

springcloud-Eureka-Cluster-peer1
springcloud-Eureka-Cluster-peer2
springcloud-Eureka-Cluster-peer3
springcloud-eureka-provider1
springcloud-Eureka-Provider2 are
springcloud-Eureka-provider3
springcloud-Ribbon
Here Insert Picture Description
after all normal start, stop springcloud-eureka-provider1 providers, port: 8001 service
access to the command window curl http://eureka-ribbon.com:8100/hi?name=zhaojq, the circuit breaker is already in force, prompt: service provider hung up
Here Insert Picture Description

Feign in use in circuit breakers

Modify application.yml file

Feign are self-breaker, it is not turned on by default. You need to open it in the configuration file.

  • application.yml
spring:
  application:
    name: springcloud-feign
  freemarker:
    prefer-file-system-access: false
  security:
    user:
      name: admin
      password: 123456

server:
  port: 8101

eureka:
  instance:
    hostname: eureka-feign.com
    instance-id: eureka-feign
  client:
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/

feign:
  hystrix:
    enabled: true  

Modify FeignConsumer

In @FeignClient annotations fallback class configuration with fail-fast processing. The treatment class as class Feign fuse logic must implement an interface to be modified @FeignClient.

  • FeignConsumer.java
package org.springcloud.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Component
/*指定这个接口所要调用的提供者服务名称 */
@FeignClient(value = "springcloud-eureka-provider",configuration = FeignConfig.class,fallback = FeignHystrix.class)

public interface FeignConsumer {
    @GetMapping(value = "/hi")
    String sayHiFromEurekaProvider(@RequestParam(value = "name")String name);
}

New class logic fuse Feign

Here Insert Picture Description

  • FeignHystrix.java
package org.springcloud.feign;

import org.springframework.stereotype.Component;

@Component
public class FeignHystrix implements FeignConsumer {

    @Override
    public String sayHiFromEurekaProvider(String name) {
        eturn "hi,"+name+", use Feign + hystrix, eureka-provider is down!";
    }
}

In order to start the project

springcloud-Eureka-Cluster-peer1
springcloud-Eureka-Cluster-peer2
springcloud-Eureka-Cluster-peer3
springcloud-eureka-provider1
springcloud-Eureka-Provider2 are
springcloud-Eureka-provider3
springcloud-Feign
Here Insert Picture Description
after all normal start, stop springcloud-eureka-provider1 providers, port: 8001 service
access to the command window curl http://eureka-feign.com:8101/hi, the circuit breaker is already in force, prompt: service provider hung up
Here Insert Picture Description

Hystrix Dashboard

Hystrix Dashboard as a circuit breaker status of a component that provides data monitoring and friendly graphical interface.

Use Hystrix Dashboard in the Ribbon

POM.xml add dependencies

<!--ribbon中使用断路器-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

Modify application.yml file

Start actuator monitoring

  • application.yml
spring:
  application:
    name: springcloud-ribbon
  freemarker:
    prefer-file-system-access: false
  security:
    user:
      name: admin
      password: 123456
    
server:
  port: 8100

# 自定义配置负载均衡策略
Load_Balance:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

eureka:
  instance:
    hostname: eureka-ribbon.com
    instance-id: eureka-ribbon
  client:
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/

#actuator端口
management:
  endpoints:
    web:
      #修改访问路径  2.0之前默认是/, 2.0默认是 /actuator
      base-path: "/actuator"
      #开放所有页面节点  默认只开启了health、info两个节点
      exposure:
        include: '*'
  server:
    port: 9001
    servlet:
      context-path: /
    ssl:
      enabled: false
  endpoint:
    health:
      show-details: always
    hystrix:
      stream:
        enabled: true

Modify the startup class

Plus @EnableHystrixDashboard comment.

  • RibbonApplication.java
package org.springcloud.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class RibbonApplication {

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

In order to start the project

springcloud-eureka-cluster-peer1
springcloud-eureka-cluster-peer2
springcloud-eureka-cluster-peer3
springcloud-eureka-provider1
springcloud-eureka-provider2
springcloud-eureka-provider3
springcloud-ribbon

In the first browser to access the access http://eureka-ribbon.com:8100/hi, then visit http://eureka-ribbon.com:9001/actuator/hystrix.stream, the browser displays the data indicators fuse
Here Insert Picture Description

Access http://eureka-ribbon.com:8100/hystrix

Here Insert Picture Description
Click Moniter Stream
Here Insert Picture Description

This page shows the index data valley fuse, meaning that data indicators as shown in the diagram from Hystrix official document, the document address: https: //github.com/Netflix/Hystrix/wiki

Here Insert Picture Description

Hystrix Dashboard in use in Feign

POM.xml add dependencies

Feign comes Hystrix dependent not start dependent, so it needs to start adding dependency in the POM

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

Modify application.yml file

Start actuator monitoring

  • application.yml
spring:
  application:
    name: springcloud-feign
  freemarker:
    prefer-file-system-access: false
  security:
    user:
      name: admin
      password: 123456

server:
  port: 8101

eureka:
  instance:
    hostname: eureka-feign.com
    instance-id: eureka-feign
  client:
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/

feign:
  hystrix:
    enabled: true  
    
management:
  endpoints:
    web:
      base-path: "/actuator"
      exposure:
        include: "*"
  server:
    port: 9002
    servlet:
      context-path: /
    ssl:
      enabled: false
  endpoint:
    health:
      show-details: always
    hystrix:
      stream:
        enabled: true

Modify the startup class

Plus @EnableHystrix and @EnableHystrixDashboard comment.

  • FeignApplication.java
package org.springcloud.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
public class FeignApplication {

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

In order to start the project

springcloud-eureka-cluster-peer1
springcloud-eureka-cluster-peer2
springcloud-eureka-cluster-peer3
springcloud-eureka-provider1
springcloud-eureka-provider2
springcloud-eureka-provider3
springcloud-feign

In the first browser to access the access http://eureka-feign.com:8101/hi, then visit http://eureka-feign.com:9002/actuator/hystrix.stream, the browser displays the data indicators fuse
Here Insert Picture Description

Access http://eureka-feign.com:8101/hystrix

Here Insert Picture Description

Click Moniter Stream

Here Insert Picture Description

Published 72 original articles · won praise 66 · Views 150,000 +

Guess you like

Origin blog.csdn.net/miaodichiyou/article/details/104350283