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 fuse in SpringCloud

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to use Hystrix fuses in SpringCloud. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

A brief introduction what is Hystrix

The word itself means circuit breaker. Pronounce the word "hype st'r circuit breaker". How to pronounce it here https://www.koolearn.com/dict/wd_73657.html

Hystrix is an open source project of NetFlix, which provides the function of fuses to prevent linkage failures in distributed systems.

Hystrix prevents linkage failures by isolating access points of services and provides fault solutions to improve the resilience and fault tolerance of distributed systems.

Problems solved by Hystrix

Prevent resource exhaustion caused by service failure.

Through the circuit breaker mechanism, you can prevent a single service failure from causing thread resources that affect the entire Servlet container.

Avoid waiting too long.

Quick failure mechanism, if a service fails, then the service will not process the request and quickly return the failure.

Increase the flexibility of services.

A fallback scheme is provided, and fallback processing can be called when the request fails.

Provide circuit breaker mechanism to prevent fault from spreading to other services.

Keep trying after the circuit breaker, and the service can be repaired automatically under specific conditions.

Provide surveillance.

Provides the fuse monitoring component Hystrix Dashboard, which can monitor the status of the circuit breaker in real time.

2. Easy to use

Whether you are using Ribbon or Feign, you need the first two steps

Increase dependency in pom.xml

Org.springframework.cloud spring-cloud-starter-hystrix

Adding comments to the startup class indicates that the fuse function is turned on

@ SpringBootApplication@EnableEurekaClient@EnableHystrixpublic class ServerApplication {private static final Logger logger = LoggerFactory.getLogger (ServerApplication.class); public static void main (String [] args) {SpringApplication.run (ServerApplication.class,args); logger.info ("hystrix server start up finished!");}} Ribbon with @ Autowiredprivate RestTemplate restTemplate / * * on each remote method that needs a circuit breaker, add comments and define callback * @ HystrixCommand to define the service degradation. Here, the fallbackMethod service invokes the method when it fails. * @ return * / @ HystrixCommand (fallbackMethod = "hiError") public String getHi () {return restTemplate.getForObject ("http://hystrix-service/hi", String.class);} / * fallbackMethod points to this method, indicating that if the remote call fails, quickly use this method instead of * / public String hiError () {return" error ";} Fiegn.

Add a callback to the FeignClient comment of the remote service to define the callback class

@ FeignClient (value = "common-service", configuration = FeignConfiguration.class, fallback = RemoteCommonServiceCallback.class) public interface RemoteCommonServiceClient {/ * call remote method * @ return * / @ GetMapping (value = "/ hello") String remoteCommonServiceHello ();}

Implement the callback class defined in FeignClient

/ *

* @ Component registers as a component, implements RemoteCommonServiceClient, and overrides method definition callback *

* @ author Calvin * @ date 2019-10-24 * @ since 1.0 * / @ Componentpublic class RemoteCommonServiceCallback implements RemoteCommonServiceClient {@ Override public String remoteCommonServiceHello () {return "something error, this is callback";}}

Enable Feign's support for Hystrix in application.yml

Feign: hystrix: enabled: true

Specific testing can be done by stopping the remote service that needs to be called, or by creating some operations such as making the service unavailable for a certain period of time and putting the thread to sleep.

Third, monitor Hystrix Dashboard

Increase dependence

Org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-hystrix-dashboard

Add annotations to the startup class to open dashboard

/ *

* Startup class, which integrates service registration, FeignClient declarative call, fuse opening, fuse monitoring *

* * @ author Calvin * @ date 2019-10-24 * @ since 1.0 * / @ SpringBootApplication@EnableEurekaClient@EnableFeignClients@EnableHystrix@EnableHystrixDashboardpublic class HystrixServerApplication {private static final Logger logger = LoggerFactory.getLogger (HystrixServerApplication.class); public static void main (String [] args) {SpringApplication.run (HystrixServerApplication.class,args); logger.info ("hystrix server start up finished!");}}

Monitoring page observation

Step1: enter http://localhost:8080/hystrix for browser

Step2: fill the page with http://localhost:8080/hystrix.stream Dalay and Title.

Step3: there is no step3. Here are the pits you stepped on.

The input in the address bar must not be the same as that in the page. If you type something wrong, you can only see it in ping all the time, like this.

When you first go in, you are worried when you are in loading status. Like the figure below, you only need to call the remote service.

Index meaning

Note: the source of this figure is https://blog.csdn.net/wm6752062/article/details/86136204.

Turbine aggregation monitoring

Create a new service turbine-monitor

Pom.xml

Hystrix-test com.calvin.hystrix 1.0-SNAPSHOT4.0.0turbine-monitor org.springframework.cloud spring-cloud-starter-turbine org.springframework.cloud spring-cloud-starter-hystrix org.springframework.cloud spring-cloud-starter-hystrix-dashboard org.springframework.boot spring-boot-starter-actuator Org.springframework.boot spring-boot-maven-plugin

TurbineMonitorApplication

/ *

* Startup class *

* * @ author Calvin * @ date 2019-10-24 * @ since * / @ SpringBootApplication@EnableTurbine@EnableHystrix@EnableHystrixDashboardpublic class MonitorApplication {public static void main (String [] args) {SpringApplication.run (MonitorApplication.class,args);}}

Application.yml

Spring: application: name: turbine-monitorserver: port: 8040eureka: client: service-url: defaultZone: the name of the service to be monitored by http://localhost:8010/eureka/turbine:# app-config: common-service, cluster cluster-name-expression of hystrix-service# service name: new String ("default")

Start the service

Browser: http://localhost:8040/hystrix

Invoke remote service

The above is how to use Hystrix fuses in SpringCloud. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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

Internet Technology

Wechat

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

12
Report