Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Hystrix to realize Circuit Breaker in Spring Cloud

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of how to use Hystrix to achieve circuit breaker in Spring Cloud, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Spring Cloud article on how to use Hystrix to achieve circuit breaker. Let's take a look.

1. Hystrix Dashboard monitoring

In addition to isolating calls from dependent services, Hystrix also provides quasi-real-time call monitoring (Hystrix Dashboard). Hystrix continuously records the execution information of all requests made through Hystrix and displays them to users in the form of statistical reports and graphs, including how many requests are executed per second, how many successes or failures, and so on. Spring Cloud also provides the integration of Hystrix Dashboard, turning the monitoring content into a visual interface.

two。 How to monitor? Actual landing operation

We create a new project: microservice-order-consumer-hystrix-dashboard, which is used to complete visual monitoring. The port number is set to 9002.

2.1 Import monitoring related dependencies

The first step is to import dashboard's dependencies:

Org.springframework.cloud

Spring-cloud-starter-netflix-hystrix

Org.springframework.cloud

Spring-cloud-starter-netflix-hystrix-dashboard

2.2 add startup comments

In the startup class, you need to add the @ EnableHystrixDashboard annotation.

@ SpringBootApplication

@ EnableHystrixDashboard

Public class OrderConsumerHystrixDashboard {

Public static void main (String [] args) {

SpringApplication.run (OrderConsumerHystrixDashboard.class, args)

}

}

2.3 Service providers need to add monitoring dependencies

At the service provider, you also need to add a Spring Boot monitoring dependency:

Org.springframework.boot

Spring-boot-starter-actuator

2.4 Test it

OK, in this case, the hystrix dashboard is basically configured. Start the new monitoring project we created, and type: http://localhost:9002/hystrix in the browser. If the following pig appears, it means that there is no problem with dashboard.

3. How to monitor other services

OK, above we can start the porcupine normally, but how to monitor other services? Let's take an order service with hystrix as an example (port number is 8001). If we want to be monitored, we need to make some preparations for the services being monitored.

3.1 dependency Import

First add the hystrix dependency to the order service (of course, the order service at port 8001 has already added the dependency):

Org.springframework.cloud

Spring-cloud-starter-netflix-hystrix

3.2 Startup class comments

Add a comment to the startup class: @ EnableCircuitBreaker

@ SpringBootApplication

@ EnableEurekaClient

@ MapperScan ("com.itcodai.springcloud.dao")

@ EnableCircuitBreaker

Public class OrderProvider01 {

Public static void main (String [] args) {

SpringApplication.run (OrderProvider01.class, args)

}

}

3.3 application.yml Profil

Finally, open the indicator flow endpoint in the configuration file, which will be used later in monitoring, which will be described below.

# # enabling Metrics flow Endpoint

Management:

Endpoints:

Web:

Exposure:

Include: hystrix.stream

3.4 Test it

Everything is ready, only the east wind is left, let's test the effect. First of all, using the order service with port 8001 as an example, start the eureka cluster first, and then start the order service with hsytrix. We can enter http://localhost:8001/provider/order/get/1 in the browser to get the order service information and make sure that the order service is normal.

Then open http://localhost:9002/hystrix and you can see Brother Haozhu, indicating that there is nothing wrong with dashboard.

We can see that this url is used to monitor the status of a service, and the url configured in the application.yml file above is used for this purpose. We open a new browser window and enter: http://localhost:8001/actuator/hystrix.stream. We can see that some information has been being brushed. This information is the information of the service-related interfaces, but it is not very intuitive. Let's take a look at the intuitive effect.

Enter http://localhost:8001/actuator/hystrix.stream on brother porcupine's page, set the delay delay to 2000 (2 seconds), and Title can choose its own name. After opening it, you can monitor the API call information of port 8001 on the visual page. We call two APIs:

Http://localhost:8001/provider/order/get/1

Http://localhost:8001/provider/order/get/list

If you refresh several calls, you can see some information about the visual interface, as follows:

The status of the service being invoked can be clearly seen from this monitoring dashboard

This is the end of the article on "how to use Hystrix to implement circuit breakers in Spring Cloud". Thank you for reading! I believe you all have a certain understanding of "how to use Hystrix to realize circuit breakers in Spring Cloud". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report