当前位置:网站首页>Alibaba microservice component Sentinel
Alibaba microservice component Sentinel
2022-06-29 04:42:00 【Promise_ J_ Z】
Catalog
Possible problems in distributed environment
Common fault tolerance mechanisms
Sentinel Console deployment (docker install )
Sentinel Integrate springAlibaba
sentinel— Integrate openfeign Downgrade
Possible problems in distributed environment
Service not available :
In a distributed environment , There are many factors that cause service unavailability , For example, the traffic surge leads to the system cpu Biao Gao is unable to process normal requests 、 Message delivery is too fast, resulting in a squeeze of unprocessed messages 、sql Too slow jam connection pool 、 Business call exception , A large number of side effects may result in service unavailability
Service avalanche effect :
The unavailability of the service provider results in the unavailability of the service caller , And will not be available for gradual amplification of the process , It's called the service avalanche effect
When the service provider is unavailable , There will be a lot of retries : User retries 、 Code logic retries , These retries eventually lead to : Further increase request traffic . So in the final analysis, the most fundamental reason for the avalanche effect is : Resource exhaustion caused by a large number of request threads waiting synchronously . When the service caller uses synchronous invocation , It will generate a large number of waiting threads and occupy system resources . Once thread resources are exhausted , Services provided by service callers will also be unavailable , So the avalanche of services came into being .

Common fault tolerance mechanisms
Timeout mechanism
Without any treatment , The unavailability of the service provider will cause the consumer request thread to force a wait , And cause system resource depletion . Add a timeout mechanism , Once timeout , Just release the resources . Due to the faster release of resources , To some extent, it can restrain the problem of resource depletion .
Service restriction
When the flow reaches a certain level , You can restrict access to the server , Achieve the purpose of flow relief
Thread pool isolation
principle : The user's request will no longer directly access the service , Instead, the service is accessed through idle threads in the thread pool , If the thread pool is full , It will be degraded , The user's request will not be blocked , You can see at least one execution result ( For example, return friendly prompts ), Instead of waiting endlessly or seeing the system crash .
Semaphore isolation
Signal isolation can also be used to limit concurrent access , To prevent obstruction from spreading , The biggest difference from thread isolation is that the thread executing dependent code is still the request thread ( The thread needs to apply through signals , If the client is trusted and can return quickly , You can use signal isolation instead of thread isolation , Reduce overhead . The size of semaphore can be adjusted dynamically , Thread pool size cannot .
Service failure
Temporarily shut down when the remote service is unstable or the network jitters , Just call service .
service degradation
Service fuse , There must be service degradation . So called demotion , It's when a service melts , The service will no longer be called , At this point the client can prepare a local one itself fallback( Back off ) Callback , Returns a default value . for example :( Spare interface / cache /mock data ) . To do so , Although the level of service has declined , But somehow it can be used , Better than just hanging up , Of course, it also depends on the appropriate business scenario .
Sentinel Console deployment (docker install )
Pull Sentinel-dashboard Mirror image
docker pull bladex/sentinel-dashboard
see Sentinel-dashboard Is the image download complete
docker images
start-up Sentinel-dashboard
docker run --name sentinel -d -p 8858:8858 -d bladex/sentinel-dashboard
visit Sentinel-dashboard page
The username and password are both : sentinel
Sentinel Integrate springAlibaba
One 、 Introduce coordinates
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>Two 、 Set up sentinel Of ip port
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:80803、 ... and 、 Create an accessible interface
@RestController
@RequestMapping("/order")
public class OrderController {
@RequestMapping("/flow")
public String flow() throws InterruptedException {
return " Normal visit ";
}
}Four 、 Access interface , Then you can sentinel The console sees relevant information

5、 ... and 、 Open the cluster link to view all links , And add flow control rules


Threshold type :
- QPS: Throughput per second
- Number of threads : Number of threads accessible per second
Stand alone threshold : Use with threshold type , for example QPS, Allow access twice per second
Flow control mode :
direct : For example, the resources of flow control are /order/flow, Then when the flow control is triggered , What limits is its own resources
relation : Such as below , When associating resources /order/flowThread Is accessed more than twice per second , Will be on the resources /order/flow Flow control .

link : Throughout the call link , You can add the following configuration to the configuration file , Give Way sentinel Help us maintain a call link tree

server:
port: 8861
spring:
application:
name: order-sentinel
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:8080
web-context-unify: false # Link convergence will be invoked by default , The link flow control effect is invalid here getUser Method is registered as sentinel Resources for ( Be careful , Used sentinelResource You can't use the same exception management , You have to use sentinelResource Of blockHandler Indicate the fallback method )

Then both interfaces call the change method

Then a call link tree will be formed here .

At this time, you can limit the current of a link by selecting the link mode , Otherwise, both links will be current limited

Flow control effect :
Fast failure : When the threshold is reached , The following request fails directly
Warm UP( Suitable for surge traffic ): Can play a preheating effect , A default cold load factor is 3, That is, from the threshold /3 Start , then QPS Gradually increase until the threshold is reached , The threshold here is set to 10, So in the first second , The number of requests allowed to access is 10/3=3, The second second second increments , Direct increment reaches the threshold 10. But the request that is not allowed limits the current directly

Waiting in line ( Suitable for pulse flow ): When QPS When the threshold is reached , The request will not be rejected immediately , It's just waiting in line , Set if you pass within the timeout , Then visit normally , If I run out of time , Then it returns failure

6、 ... and 、 Add downgrade rule
Slow call ratio : The largest of the following RT Set to 1000 millisecond , When a request exceeds 1000 Milliseconds are slow calls , Combine the proportional threshold with the minimum number of requests . When the number of requests is greater than 5 And the requests that are called slowly account for one tenth 、 Then open the fuse , Fusing time is five seconds , After five seconds , It will enter the half open state , For new requests, if the first one is a slow call request , Then it will fuse again immediately , Instead of judging by proportion , If the first is a normal request , Then only when the proportional threshold is reached will the fuse be blown

Abnormal proportion : When the ratio of abnormal times to the total times reaches the threshold, the fuse will be opened
Number of abnormal : When the number of exceptions in the request reaches the threshold, the fuse is turned on
sentinel— Integrate openfeign Downgrade
One 、 Introduce dependencies
<!--nacos- Service registration found -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--1. add to openfeign rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--sentinel rely on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>Two 、 Turn on openfeign Integrate sentinel
server:
port: 8041
# apply name (nacos This name will be used as the service name )
spring:
application:
name: order-service
cloud:
nacos:
server-addr: 120.79.10.137:8849
discovery:
username: nacos
password: nacos
namespace: public
feign:
sentinel:
# openfeign Integrate sentinel
enabled: true3、 ... and 、 To write feign Call interface , And in feignclient Medium fallback Attribute indication fallback Class bytecode of method
@FeignClient(value="stock-nacos",path = "/stock",fallback = StockFeignServiceFallback.class)
public interface StockFeignService {
@RequestMapping("/reduct")
public String reduct2();
}
Four 、 establish fallback Class bytecode of method , Realization feign Called interface
@Component
public class StockFeignServiceFallback implements StockFeignService {
@Override
public String reduct2() {
return " Degraded !!!";
}
}Hotspot resource flow control
The first thing to emphasize is sentinel For hotspot rules , You have to use @SentinelResource Use , If you only control the flow of hotspot resources for the interface, it will not work
/**
* Hot spot rules , You have to use @SentinelResource
* @param id
* @return
* @throws InterruptedException
*/
@RequestMapping("/get/{id}")
@SentinelResource(value = "getById",blockHandler = "HotBlockHandler")
public String getById(@PathVariable("id") Integer id) throws InterruptedException {
System.out.println(" Normal visit ");
return " Normal visit ";
}
public String HotBlockHandler(@PathVariable("id") Integer id,BlockException e) throws InterruptedException {
return " Hot spot exception handling ";
}As shown in the figure below , New hotspot rules , Current limiting mode can only be passed through QPS Carry out current limiting
Parameter index : It indicates the number of parameters to control current limiting , The following figure means ,1 The maximum request throughput of the first parameter in seconds is 10, Over current limit

Click Add , You can edit the current limit of the advanced part


This means that when the value of the first parameter is 1 When , Only two requests can be passed per second , But there may be some problems when saving bug, Save back failed . In this case, you need to add two parameter exceptions , Save and then delete the unused one .

边栏推荐
- Five thousand years of China
- Template method pattern
- 044. (2.13) add value to yourself
- Actual combat! Another opening method of magic modified swagger and knife4j
- Research Report on the overall scale, major manufacturers, major regions, product and application segmentation of disposable hearing aid batteries in the global market in 2022
- How to create a subtype like relationship between two generic classes when the classes are generic related
- 波形记录仪MR6000的实时波形运算功能
- Memo pattern
- Log in to the MySQL database and view the version number on the command line
- 1015 德才论
猜你喜欢

Quelles sont les méthodes de simulation et de gravure des programmes? (comprend les outils communs et la façon dont ils sont utilisés)

1018 hammer scissors cloth

Blue Bridge Cup ruler method

Redis 缓存穿透、缓存击穿、缓存雪崩

Technical parameters of Tektronix DPO4104 digital fluorescence oscilloscope
![[Verilog quick start of Niuke network question brushing series] ~ asynchronous reset Series T trigger](/img/e3/cf40fb0131ddeb26bc5beeca03d183.png)
[Verilog quick start of Niuke network question brushing series] ~ asynchronous reset Series T trigger

软件体系结构实验汇总

MySQL subquery

It is said on the Internet that a student from Guangdong has been admitted to Peking University for three times and earned a total of 2million yuan in three years

Memo pattern
随机推荐
PostgreSQL has a cross database references are not implemented bug
Real time waveform calculation function of Waveform Recorder mr6000
How to quickly install MySQL 5.7.17 under CentOS 6.5
20年秦皇岛D - Exam Results(二分+思维,附易错数据)
Composite pattern
Microsoft Pinyin IME personal preferences
2022-2028 global and Chinese industrial electronic detonator Market Status and future development trend
基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
What exactly does GCC's -Wpsabi option do? What are the implications of supressing it?
Research Report on the overall scale, major manufacturers, major regions, products and applications of magnetron sputtering coaters in the global market in 2022
What are the MySQL database constraint types
Mediator pattern
1019 数字黑洞
Go Foundation (I)
安捷伦数字万用表软件NS-Multimeter,实时数据采集数据自动保存
Live broadcast appointment AWS data everywhere series activities
Canoe- how to parse messages and display information in the trace window (use of program node and structure type system variables)
Cucumber test practice
Research Report on the overall scale, major manufacturers, major regions, products and application segments of semiconductor wafer metal stripping platform in the global market in 2022
Gbase 8s must be a DBSA. Solution to failure to start due to path change