This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging
Reading reminder:
- This article is intended for those with some springBoot background
- The Hoxton RELEASE of Spring Cloud is used in this tutorial
- Since Knife4j is more friendly than Swagger, this article integrates Knife4j
- This article relies on the project from the previous article, so check out the previous article for a seamless connection, or download the source directly: github.com/WinterChenS…
Before a summary
- The beginning of the SpringCloud series (part 1)
- Nacos of SpringCloud series (2) | August more challenges
- SpringCloud series (3) of the Open Feign | August more challenges
- SpringCloud series (4) the SpringCloud Gateway | August more challenges
- Swagger Knife4j and login permission verification
This paper gives an overview of
- What is a fuse
- What is a sentinel
- Spring Cloud integrates Sentinel
- Actual application scenarios
What is a fuse
Surely we all know a common object in life, fuse, in fact, it is a fuse, when the electrical short circuit fault leads to the instantaneous current exceeds the instantaneous value will trigger the fuse, leading to disconnect the circuit, so as to protect the safety of the entire circuit and electrical appliances.
** Fuse (fuse) refers to an electrical appliance that, when the current exceeds a specified value, fuses the melt with its own heat and disconnects the circuit. Fuse is based on the current exceeds the specified value for a period of time, with its own heat to melt, so that the circuit is disconnected; A current protector made of this principle. Fuse is widely used in high and low voltage distribution system, control system and electrical equipment, as a short circuit and over current protector, is one of the most common protection devices.
Some of you may want to ask, what does this have to do with the fuse of service we said? In fact, many things of principle are closely related to life, circuit fusing, protection of circuits and electrical appliances. In the world of code, there is the ability to flow control and degrade the circuit breaker of services, so that the failure of some services does not lead to an avalanche of the entire service cluster.
What is a sentinel
Sentinel is alibaba’s open source service governance framework. Sentinel, translated as Sentinel in Chinese, provides flow control, circuit breaker and downgrade functions for microservices. Like Hystrix, Sentinel can effectively solve the “avalanche” effect caused by microservice calls and provide a stable solution for microservice systems.
With Hytrxi entering maintenance and no longer providing new functionality, Sentinel is a good alternative. In general, Hystrix uses thread pools to isolate service calls, and Sentinel uses user threads to isolate interfaces. Compared to Hystrxi, which provides service-level isolation, Sentinel provides interface-level isolation, and Sentinel has a finer isolation level. In addition, Sentinel directly uses user threads for restriction, which reduces the overhead of thread switching compared to Hystrix’s thread pool isolation. In addition, Sentinel’s DashBoard provides online configuration for changing flow limiting rules, which is more optimized.
According to the official documents, Sentinel has the following characteristics:
- Rich application scenarios: Sentinel undertakes the core scenarios of Alibaba’s Double Eleven Traffic promotion in the past 10 years, such as SEC kill (that is, burst flow control in the system capacity can withstand the range), message peak filling, real-time fusing downstream unavailable applications, etc.
- Complete real-time monitoring: Sentinel also provides real-time monitoring. In the console, you can see second-level data from a single machine accessing an application, or even a summary of the performance of clusters of less than 500 units.
- Extensive open source ecosystem: Sentinel offers integration modules with other open source frameworks/libraries out of the box, such as Spring Cloud, Dubbo, and gRPC. You just need to introduce the appropriate dependencies and do a simple configuration to quickly access Sentinel.
- Sophisticated SPI extension points: Sentinel provides easy-to-use, sophisticated SPI extension points. You can quickly customize the logic by implementing extension points. For example, custom rule management, data source adaptation, and so on.
Sentinel’s main features:
Sentinel features and design concepts
Flow control
Flow control is a commonly used concept in network transmission, which is used to adjust the transmitted data of network packets. However, from a system stability perspective, there are also a lot of concerns about how fast requests can be processed. Requests coming at any time are often random and uncontrollable, and the system’s processing capacity is limited. We need to control the flow according to the capacity of the system. Sentinel, as a modulator, can adjust random requests into appropriate shapes as required, as shown in the figure below:
Flow control has the following angles:
- The calling relationship of resources, such as the calling link of resources, and the relationship between resources;
- Performance metrics, such as QPS, thread pool, system load, etc.
- Effects of control, such as direct flow limiting, cold start, queuing, etc.
Sentinel is designed to give you the freedom to choose the angles of control you want and to combine them flexibly to achieve the desired effect.
Fusing the drop
In addition to flow control, reducing unstable resources in the call link is one of Sentinel’s missions. Due to the complexity of the calling relationship, if a resource in the calling link becomes unstable, the requests will eventually pile up. This problem is the same one described in Hystrix.
Sentinel and Hystrix have the same principle: When a resource in the call link is unstable, such as timeout, and the abnormal proportion increases, the call of this resource is restricted and the request fails quickly, so as to avoid affecting other resources and finally producing the effect of avalanche.
How does Sentinel work
The main working mechanism of Sentinel is as follows:
- Provides adaptive or display apis to mainstream frameworks to define resources to be protected, and provides facilities for real-time resource statistics and call link analysis.
- Traffic is controlled based on preset rules and real-time resource statistics. Sentinel also provides an open interface for you to define and change the rules.
- Sentinel provides a real-time monitoring system so that you can quickly learn about the current system status.
Spring Cloud integrates Sentinel
Note: The project integrated in this article is based on the previous articles, so it needs to be built step by step according to the content of the previous articles
Download and install the Sentinel Dashboard
Download the latest release from the official Github repository: github.com/alibaba/Sen…
After downloading, start the service (port 8748) by using the following command:
java -Dserver.port=8748 -Dcsp.sentinel.dashboard.server=localhost:8748 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.82..jar
Copy the code
After startup, log in to the console: http://localhost:8748
Account number: Sentinel Password: Sentinel
Transformation Project Consumer
Add Maven dependencies:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
Copy the code
Modify the configuration application.yml(only the configuration to be modified is displayed) :
spring:
cloud:
nacos:
discovery:
server-addr: 127.0. 01.: 8848
sentinel:
transport:
port: 18763 # 1
dashboard: 127.0. 01.: 8748
feign:
sentinel:
enabled: true # 2
Copy the code
- Here,
spring.cloud.sentinel.transport.port
The port configuration starts an Http Server on the application’s corresponding machine, which interacts with the Sentinel console. For example, if the Sentinel console adds a flow limiting rule, it will push the rule data to the Http Server for receiving, and the Http Server will register the rule in Sentinel. - through
feign.sentinel.enable
Enable automatic adaptation for Feign and Sentinel.
test
Start the provider/consumer/gateway service respectively, and then repeated requests: http://localhost:16011/nacos/feign-test/hello
Note: You need to request the interface before you can see the corresponding service and interface in the console.
Open the console to see:
Real-time monitoring:
Cluster point link:
Add flow control rules
You can limit some of the functionality of the interface by modifying some of the controls that follow, and you can try them out, but I won’t go into detail here.
Follow the same steps for other services: Provider and auth.
Spring Cloud Gateway uses Sentinel
Modify it in the project Gateway
Add Maven dependencies:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
Copy the code
Modify the application.yml configuration file:
spring:
cloud:
sentinel:
transport:
port: 15000
dashboard: localhost:8748
Copy the code
Create a traffic limiting rule for a gateway group and gateway:
@Configuration
public class GatewayConfiguration {
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public GatewayConfiguration(ObjectProvider
> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer)
{
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler(a) {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@PostConstruct
public void doInit(a) {
initCustomizedApis();
initGatewayRules();
}
private void initCustomizedApis(a) {
Set<ApiDefinition> definitions = new HashSet<>();
ApiDefinition api1 = new ApiDefinition("consumer")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/consumer/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
ApiDefinition api2 = new ApiDefinition("provider")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/provider/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
definitions.add(api1);
definitions.add(api2);
GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
private void initGatewayRules(a) {
Set<GatewayFlowRule> rules = new HashSet<>();
rules.add(new GatewayFlowRule("consumer")
.setCount(10)
.setIntervalSec(1)); rules.add(new GatewayFlowRule("consumer")
.setCount(2)
.setIntervalSec(2)
.setBurst(2)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)
)
);
rules.add(new GatewayFlowRule("provider")
.setCount(10)
.setIntervalSec(1)
.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)
.setMaxQueueingTimeoutMs(600)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER)
.setFieldName("X-Sentinel-Flag"))); rules.add(new GatewayFlowRule("provider")
.setCount(1)
.setIntervalSec(1)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("pa"))); rules.add(new GatewayFlowRule("provider")
.setCount(2)
.setIntervalSec(30)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("type")
.setPattern("warn")
.setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS)
)
);
rules.add(new GatewayFlowRule("provider")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
.setCount(5)
.setIntervalSec(1)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("pn"))); GatewayRuleManager.loadRules(rules); }}Copy the code
test
Through the above configuration gateway has successfully integrated the Sentinel, then we can request interface: http://127.0.0.1:15010/consumer/nacos
Normal results:
{
"code": 401."message": "Not logged in"
}
Copy the code
If the token value in the header is used as the token value in the request header, the correct return value will be obtained. This is irrelevant for the current test. Check out the previous tutorial to see why.
By modifying the current limit:
Multiple clicks can get the exception information:
Blocked by Sentinel: FlowException
Copy the code
In this way, unified interface flow control can be carried out through the gateway layer. Of course, Sentinel is more than that, and you can master the control of the interface through other functions.
Application scenarios
Traffic limiting and fusing through the interface can make the modules in microservices more stable and not panic when a large amount of traffic comes.
In addition to some extreme functions such as seckill buying coupons, it is also widely used in applications with high traffic.
The source address
Github.com/WinterChenS…
reference
introduction
SpringCloud 2020 Tutorial 3: Using Sentinel as a fuse _ Zhipeng Fang’s column -CSDN blog