How to use the ystrix component of SpringCloud

Circuit Breaker Solution Spring Cloud Hystrix Component.

Take 2.2.0.release as an example to show you how to get started with this component.

Core concepts

  • hystrix: is commonly referred to as a circuit breaker component
  • hystrix-dashboard: breaker monitoring statistics display panel component
  • Turbo: Multi-node breaker statistics consolidation display component, commonly used in cluster scenarios

Introduced jar package

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

Build projects

This example uses Maven for project build ing and so on.The following is about what the discover and config module configurations for spring clouds are, in order to reduce the size of the content, but there must be some in the actual spring cloud scenario.

cloud-service-hystrix-dashboard

This project is intended to show the monitoring statistics for Circuit Breaker, which is the application thread interface break statistics reported by hystrix.stream.In the micro-service system, dashboard needs to build projects independently, as additional customization may be required in the future.Dashboard needs to support both single-node and cluster statistics, because at least two applications are deployed in a microservice, and it is impossible to view these fused data individually from one node, so it needs to be aggregated. The turbine component is used here.

pom.xml:

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

bootstrap.yml

spring:
  application:
    name: service-hystrix-dashbaord
  profiles:
    active: ${ENV:local}

server:
  port: 8888

turbine:
  app-config: service-demo3
  cluster-name-expression: "'default'"

applicatin.java

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixApp {
    public static void main( String[] args ) {
        SpringApplication.run(HystrixApp.class,args);
    }
}

cloud-service-demo3

demo3 is an example of the actual integration of an application system.Only hystrix components are required in the application system, but actuator components are also required for springboot projects.In addition, the openfeign component was introduced to provide another example of using hystrix, which is used in two ways in the application system.

pom.xml:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

bootstrap.yml

spring:
  application:
    name: service-demo3
  profiles:
    active: ${ENV:local}

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

application.java

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableFeignClients
public class Demo3App {
    public static void main( String[] args ) {
        SpringApplication.run(Demo3App.class,args);
    }
}

hystrix.stream

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

This is a small BUG for hystrix2.0, requiring the addition of @Bean to expose the interface for monitoring statistics to the /actuator/hystrix.stream interface path.

Use hystrix

Native hystrix mode

@RestController
public class UserController {

    @Autowired(required = false)
    ITestService iTestService;
    @Autowired(required = false)
    IUserService iUserService;

    @GetMapping("/user/users")
    public String getUsers(){
        return "users is success" + iUserService.getUserId("13567890123");
    }

    @HystrixCommand(fallbackMethod = "defaultFindUser")
    @GetMapping("/user/find")
    public String getFindUser(){
        return iTestService.sayHello("test");
    }

    public String defaultFindUser(){
        return "hystrix default data";
    }
}

openFeign mode

OpenFeign, a HTTP Client tool for front-end routing, has integrated hystrix functionality itself.

@FeignClient(value = "service-demo2",fallback = UserServiceFallbackImpl.class)
public interface IUserService {

    @RequestMapping(value="/user/{mobile}", method= RequestMethod.GET)
    @ResponseBody
    Long getUserId(@PathVariable(name = "mobile") String mobile);
}

@Service
public class UserServiceFallbackImpl implements IUserService {
    @Override
    public Long getUserId(String mobile) {
        System.out.println(mobile);
        return 0L;
    }
}

openfeign package dependency on hystrix:

dashboard panel

Introduction of Indicators

Sample code: https://github.com/owen-jia/cloud-parent

Author: Owen Jia

Blog address: https://blog.shareworld.vip

Tags: Programming Spring Mobile xml Java

Posted on Thu, 26 Mar 2020 21:28:31 -0700 by ronverdonk