In terms of interface calls, Spring Cloud will generally go through the cooperation of the following components:
Feign - > hystrix - > ribbon - > HTTP client (apache http components or Okhttp), as shown in the following figure:
(1) Interfaced request call when calling the interface modified by @ FeignClient annotation, convert the request into Feign's request instance Feign Request, which is handled by Feign framework.
(2) Feign: transformation request feign is a lightweight framework for HTTP request invocation. It can invoke HTTP requests in the way of Java interface annotation, encapsulating the HTTP invocation process.
(3) Hystrix: the calling relationship of the fuse handling mechanism Feign will be intercepted by the hystrix agent. For each Feign call, please
Please, Hystrix will package it into HystrixCommand and participate in the flow control and fuse rules of Hystrix. If the request determines that the fuse needs to be blown, Hystrix will blow directly, throw an exception, or use FallbackFactory to return the result of fusing Fallback; If passed, the call request is passed to the Ribbon component.
(4) Ribbon: service address selection after the request is delivered to the ribbon, the ribbon will select the appropriate service instance from the list according to the service list maintained by itself, according to the service quality of the service, such as average response time, Load, etc., combined with specific rules, select the machine, and then deliver the information request of the machine instance to the Http Client, HttpClient client to execute the real Http interface call;
(5) HttpClient: the Http client actually executes the Http call. If the service address has been specified according to the request passed by the upper Ribbon, the HttpClient starts to execute the real Http request
Hystrix concept
Hystrix is a distributed system that provides delay and fault tolerance functions to ensure the inevitable failure of complex distributed systems
It is still elastic.
For example, there are many services in the system. When some services are unstable, the user threads using these services will be blocked. If there is no isolation mechanism, the system may hang up at any time, resulting in great risks. Spring cloud uses Hystrix components to provide circuit breaker, resource isolation and self-healing functions. The following figure shows that service B triggered the circuit breaker to prevent cascading failure
The following is a simple demonstration of spring boot combined with hystrix
First introduce the required dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
Add a hystrix configuration to the configuration file
#Turn on the fuse mechanism, which is off by default feign.hystrix.enabled=true #Set the hystrix timeout, which is 1000ms by default hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
Create an implementation class to implement the interface of feign service discovery. When it is fused, the methods in it can be executed
package com.atguigu.eduservice.client.impl; import com.atguigu.commonutils.R; import com.atguigu.eduservice.client.VodClient; import org.springframework.stereotype.Component; import java.util.List; /** * <p> * VodClient The implementation class, the hystrix fuse, will execute the methods in it * </P> * * @author Kk * @since 2022/3/6 20:20 */ @Component public class VodFileDegradeFeignClient implements VodClient { //Will execute after error @Override public R deleteAliYunVideo(String videoId) { return R.error().message("Error deleting video"); } @Override public R deleteBatch(List<String> videoIdList) { return R.error().message("Error deleting videos in batch"); } }
Modify the feign interface and add the fallback attribute on the @ FeignClient interface. The attribute value is the corresponding implementation class
package com.atguigu.eduservice.client; import com.atguigu.commonutils.R; import com.atguigu.eduservice.client.impl.VodFileDegradeFeignClient; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; /** * <p> * Map the method path of the called server * </P> * * @author Kk * @since 2022/3/6 15:31 */ @Component @FeignClient(name = "service-vod",fallback = VodFileDegradeFeignClient.class) //This annotation contains the service name registered by the called server in nacos public interface VodClient { /* Defines the method path to call Delete Alibaba cloud video based on video id */ @DeleteMapping("eduvod/video/deleteAliYunVideo/{videoId}") R deleteAliYunVideo(@PathVariable("videoId") String videoId); /* Batch delete Alibaba cloud videos */ @DeleteMapping("eduvod/video/deleteBatch") R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList); }
In this way, the effect of fuse is simply realized