Detailed Explanation of SpringCloud Microservice Architecture II

Call Feign remotely

​ Feign is an http client component provided by Spring Cloud, which is used to access remote services using the http protocol; it allows us to call remote services as conveniently as calling local services.

1. Add Feign's dependency to the pom.xml file

<!--fegin组件-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2. Paste **@EnableFeignClient** scanning annotation on the startup class

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

3. Provide an interface API

/**
 * 专门用于调用商品服务的接口
 * \@FeignClient 标识这个类是 Feign 的客户端
 * 当这个接口被扫描到时, 就会自动创建为 Feign 客户端代理对象
 *
 * 大致实现原理:
 *  1 扫描所有的 \@FeignClient 注解的接口, 创建代理对象, 并且以 name 作为服务 id
 *  2 当调用该代理类中的任意方法时, 获取到 \@RequestMapping 中的请求地址, 以及对应的请求参数
 *  3 给予服务 id 从注册中心获取到对应的服务访问地址
 *  4 利用负载均衡算法, 获取到服务调用的服务地址,并构建请求路径和参数
 *  5 发起 http 请求到远程服务
 *  6 将远程服务返回的 json 字符串转换为方法返回类型的对此昂, 并进行返回
 	
 	注意:参数列表需要贴上相应的注解
 		a、路径参数:@PathVariable
 		b、表单参数(普通类型):@RequestParam
 		c、自定义对象:@RequestBody
 */
@FeignClient(name = "product-service")
public interface ProductFeignApi {
    
    

    @RequestMapping("/product/{pid}")
    Product findByPid(@PathVariable("pid") Long pid);
}

4. In the place where the business is used, just call the findById method directly after injecting the attribute

Service fuse downgrade Sentinel

3.1. Problems caused by high concurrency

​ Due to network reasons, the service cannot be guaranteed to be 100% available. If there is a problem with a service, there will be a network delay when calling this service.

3.2. Service avalanche effect

Avalanche effect : Due to the dependencies between services, faults will propagate, which can have catastrophic consequences for the entire microservice system.

There are many reasons for avalanches, such as unreasonable capacity design, a corresponding slowdown of a certain service under high concurrency, and the exhaustion of a certain machine resource. It is impossible to completely eliminate the source of avalanches, only to minimize fault tolerance.

insert image description here

3.3 Common Fault Tolerance Schemes

3.3.1. Isolation mechanism

​For example, service A has 100 threads. At this time, service A may allocate 90 threads to service B, and services C and D allocate a total of 10 threads. If service A hangs up at this time, then services B, C, and D will also It is in a state of waiting for paralysis; now divide three services in service A to serve A, B, and C respectively. Even if a service in service A fails, at least two of services A, B, and C can provide services normally .

insert image description here

3.3.2. Timeout mechanism

​ When the upstream service calls the downstream service, set a maximum response time. If the downstream service does not respond after exceeding this time, the request will be disconnected and the thread will be released.

insert image description here

3.3.3. Current limiting mechanism

​ Current limiting is to limit the input and output flow of the system to achieve the purpose of protecting the system. To ensure the stable operation of the system, once the required threshold is reached, it is necessary to restrict the flow and take a few measures to complete the purpose of restricting the flow.

insert image description here

3.3.4. Fuse mechanism

​ When the downstream service responds slowly or fails due to excessive access pressure, the upstream service temporarily cuts off the call to the downstream service in order to protect the overall availability of the system. This measure of sacrificing the part to preserve the whole is called fusing

insert image description here

Three states of fusing :

  • Closed state of the fuse (Closed): The state of the fuse when there is no fault, without any restrictions.
  • Fuse open state (Open): Subsequent calls to the service interface do not go through the network, and directly execute the local fallback method.
  • Half-Open: Attempt to restore the service. If the success rate meets expectations, the service will enter the half-open state; if the success rate is low, it will re-enter the half-open state.

3.3.5. Downgrade mechanism

​ It is a bottom-up solution provided by microservices. Once the service cannot be called normally, the bottom-up solution is used.

insert image description here

Getting started with Sentinel

4.1 Introduction to Sentinel

Sentinel : It is a comprehensive solution for service fault tolerance open sourced by Ali . Provides fault- tolerant solution control for flow control / fuse degradation / system dimension .

Two cores in Sentinel :

  • The core library (Java client) can run on all Java runtime environments
  • The console (Dashboard) is developed based on SpringBoot and can be run directly after packaging without the need for containers such as Tomcat.

4.2, install Sentinel console

1. Introduce sentinel dependency in shattered.xml

<!--sentinel组件-->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

2. Download the jar package https://github.com/alibaba/Sentinel/releases

3. Start the console

# 直接使用jar命令启动项目(控制台本身是一个SpringBoot项目) 
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.0.jar

4. Add the following configuration to the application.yml configuration file:

spring:
  cloud:
    sentinel: 
      transport: 
        port: 9999 #跟控制台交流的端口,随意指定一个未使用的端口即可 
        dashboard: localhost:8080 # 指定控制台服务的地址

5. Access localhost:8080 through the browser to enter the console (the default username and password are sentinel/sentinel)

insert image description here

Sentinel Fault Tolerance Dimensions

insert image description here

Flow control : adjust the data of the network packet, request random uncontrollable system processing capacity is limited, so the flow needs to be controlled according to the processing capacity of the system.

Circuit breaker degradation : When a certain resource is detected to be unstable, the resource is restricted, so that the request fails quickly, and other resources are affected to avoid cascading failures. If the request takes a long time, etc.

System load protection : Provides adaptive protection capabilities in the system dimension. Continuous requests under high load will cause the system to crash and become unresponsive.

Sentinel rule types

5.1. Flow control rules (key points)

Flow control : monitor the QPS (query rate per second) or the number of concurrent threads and other indicators of application traffic, and control the flow when the threshold is reached to avoid instantaneous traffic peaks.

Three flow control modes :

  • Direct (default): When the interface reaches the current limit condition, the current limit is enabled
  • Association : When the associated resource reaches the current limit condition, open the current limit [appropriately make application concessions]
  • Link : When the resource coming from an interface reaches the current limit condition, the current limit is turned on

5.2. Fuse rules (emphasis)

Three criteria for measurement:

  • Slow call ratio : take the slow call ratio as the threshold, you need to set RT (maximum response time), the request response time is greater than the threshold is slow call, the number of requests in the unit is greater than the minimum number of requests and the call ratio is greater than the threshold, and then the request is automatically broken .
  • Abnormal ratio : The number of requests per unit time is greater than the minimum number of requests and the abnormal ratio is greater than the threshold, and the request is automatically broken. The fuse enters the detection recovery state. If the next request is successful, the fuse will end, otherwise it will be blown again.
  • Number of exceptions : The number of exceptions per unit time is greater than the threshold and automatically fuses. The fuse enters the detection recovery state. If the next request is successful, the fuse will end, otherwise it will be blown again.

5.3. Hot spot rules

Hotspots : Frequently accessed data

In many cases, it is necessary to count the TOP K data with the highest access frequency in a certain hotspot data, and restrict its access. For example, count and control the most frequently purchased commodities within a period of time.

5.4. Authorization rules

Use the function of open source access control (black and white list control) to determine whether the request is allowed or not. Only requests originating from the whitelist can pass, and requests originating from the blacklist cannot pass.

5.5. System Rules

It controls the ingress traffic at the application level, monitors the load, CPU usage, average RT, ingress QPS, and number of concurrent threads of a single machine, so that the system can run at the maximum throughput as much as possible while ensuring the overall stability of the system sex.

Guess you like

Origin blog.csdn.net/weixin_49137820/article/details/128244801