Actual combat of microservice architecture development: the difference between fusing and degradation and how to integrate Hystrix

Posted by Spaceboy on Sat, 26 Feb 2022 07:41:34 +0100

Difference between fusing and degradation

Many developers will be confused about the difference between fusing and downgrade. The similarities and differences between the two are summarized below.

Similarities between fusing and degradation

Service degradation and service fusing are similar from some angles.

·Same purpose. Both are technical means to prevent the overall slowness or even collapse of the system from the perspective of availability and reliability.

·Behave similarly. The ultimate performance of both is to let users experience that some services are temporarily unreachable or unavailable.

·Consistent particle size. Generally, it is service level. Of course, there are many more fine-grained practices in the industry, such as data persistence layer (query is allowed, addition, deletion and modification are not allowed); they all rely on automation. Service fusing is generally triggered automatically based on service policy. Although service degradation can be intervened manually, it is obviously unrealistic to rely entirely on people under the micro service architecture, so it will be included in automatic configuration.

Difference between fusing and degradation

There are two main differences between the two.

·The trigger conditions are different. Service fusing is generally caused by a service (downstream service) failure, and service degradation is generally considered from the overall load.

. different levels of management objectives. Service fusing is aimed at the processing of the whole framework level. Each micro service is required without hierarchy; Service degradation generally needs to be divided into business levels. For example, degradation generally starts from the most peripheral services.

How to integrate Hystrix

In the Spring Cloud framework, the circuit breaker mechanism is implemented through hystrix. Hystrix will monitor the status of calls between microservices. When the failed call reaches a certain threshold, it will start the circuit breaker mechanism. The annotation of the fuse mechanism is @ HystrixCommand. Hystrix will find the method with this annotation and associate this kind of method with the agent connected with the fuse.

In this section, we will implement the circuit breaker based on Hystrix.

stay
Based on Micro Weather cueka client feign, it can become a new application Micro Weather Er Eureka client feign hystrix, and take it as an example.

Required environment

To demonstrate this example, you need to use the following development environment.

  • JDK8.
  • Gradle 4.0.
  • Spring Boot 2.0.0.M3.
  • Spring Cloud Starter Netflix Eureka Client Finchley.M2.
  • Spring Cloud Starter OpenFeign Finchley.M2.
  • .Spring Cloud Starter Netflix Hystrix Finchley.M2.

Change configuration

The easiest way to use Hystrix is to add Hystrix dependencies.

dependencies{
//...
//Add Spring Cloud Starter Netflix Hystrix dependency
compile('org.springframework.cloud:spring-cloud-starter-netflix-
hystriz')
)

Using Hystrix

To enable Hystrix´╝îThe simplest way is in the root directory of the application Application Add on class org.springframe-work.cloud.client.circuitbreaker.EnableCircuitBreaker Notes.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.Enable
CircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscovery
Client;
import org.springframework.cloud.netflix.feign.EnableEeignClients;
/**

Main application
*
*@since 1.o.0 2017 November 12
* @author <a href="https://waylau.com">Way Lau</a>
*/
@SpringBootApplication
EnableDiscoveryClient
@EnableFeignClients
EnableCircuitBreaker
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}

Add circuit breaker

Original
Micro Weather Eureka client Feign. The Feign client cityclient has been defined import org.springframework.cloud.netflix.feign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;
/**
*Client for accessing city information
*
*@since 1.0.0 2017 November 4
*@author <a href="https://waylau.com">Way Lau</a>
*/
FeignClient("msa-weather-city-eureka")
public interface CityClient {
@GetMapping("/cities")
String listCity();
}

CityClient realizes obtaining city information from the city data API micro service MSA weather city Eureka.

We are calling citycontroller of CityClient On top of listcity() method, add
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand annotation.

package com.waylau.spring.cloud.weather.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
iavanica.annotation.HystrixCommand;
import com.waylau.spring.cloud.weather.service.cityClient;
/**
*City controller.
*
*since 1.o.o 2017 November 4
* @author <a href="https://waylau.com">Way Lau</a>
*/
@RestController
public class CityController {
@Autowired
private CityClient cityClient;
@GetMapping("/cities")
@HystrixCommand(fallbackMethod = "defaultCities")
public String listCity({
//Find through Feign client
String body = cityClient.listCity(;
return body;
/*large
*Customize the default returned content of the circuit breaker
*@return
*/
public String defaultCities(){
return"City data API The service is temporarily unavailable!";
}
}

In the HystrixCommand annotation, we set the value of fallbackMethod to "defaultCities". fallbackMethod is a method used to set callback. Here we define a method whose return default value is "city data API service is temporarily unavailable!" Methods.

Modify application configuration

The application configuration is modified as follows.

spring.application.name: micro-weather-eureka-client-feign-hystrix
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
feign.client.config.feignName.connectTimeout:5000
feign.client.config.feignName.readTimeout: 5000

Operation and test

Start the created in the previous chapter
Micro Weather Eureka server and MSA weather city Eureka, as well as micro weather Eureka client feign hystrix in this example. Among them, micro weather cueka server starts at port 8761 by default, MSA weather city cueka starts at port 8085, and Micro Weather Eureka client feign hystrix starts at port 8080.

java -jar micro-weather-eureka-server-1.0.0.jar --server.port=8761
java -jar msa-weather-city-eureka-1.0.0.jar--server.port=8085
java -jar micro-weather-eureka-client-feign-hystrix-1.0.0.jar --server.
port=8080

If everything is normal, then
The management interface of Micro Weather cueka server can see the information of the above services.

Access in browser
Micro Weather cueka client feign hystrix service (in this case, the address is http:/localhost:8080). When we try to access the http:/localhost:8080/cities interface, if everything is normal, we can see the response of MSA weather city Eureka service when it is normal, as shown in Figure 15-2.

We close the MSA weather city Eureka service process to simulate the state when the urban data API micro service is unavailable. At this point, visit again
http://localhost:8080/cities Interface, you can see the content responded by the HystrixCommand callback method as shown in Figure 15-3.

Source code

For the source code involved in this section, see
micro-weather-eureka-server,micro-weather-cureka-client-feign. MSA weather city Eureka, and Micro Weather Eureka client feign hystrixo

This article explains the difference between fusing and degradation and how to integrate Hystrix

  1. The next article will explain to you the circuit breaker mechanism for realizing micro services;
  2. Friends who think the article is good can forward this article and pay attention to the Xiaobian;
  3. Thank you for your support!

Topics: Java Programming Programmer Spring Cloud architecture