1, Concept introduction
1. What is fusing?
2. What is downgrade?
3. Difference?
4. What is current limiting?
2, Solution
The comparison between Hystrix of springcloud and Sentinel of Alibaba is as follows:
3, Integrated use
1. springboot introduces Sentinel dependency
Specify version number directly
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.3.RELEASE</version> </dependency>
Or introduce spring cloud Alibaba. Without the version number, the appropriate version will be selected according to the spring cloud by default
<!--Alibaba Sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2. Download and 2.2 3. Core package core-1.8 in release Console corresponding to 0
https://github.com/alibaba/Sentinel/releases/tag/v1.8.0
3. Use Java - jar sentinel-dashboard-1.8 0.jar to start the console (the default port is 8080, which can be modified by using -- server.port=8888)
4. springboot configuration Sentinel related information
spring: cloud: sentinel: transport: port: 8719 dashboard: localhost:8888
5. The configuration rule can be requested at most twice per second. Press F5 to quickly refresh the test request
Note: Sentinel can only monitor direct calls by default. If you want to monitor Feign remote calls, you need to add configuration
feign: sentinel: enabled: true
Here are three questions,
① The monitoring chart is not displayed (the version has solved this problem, such as 1.8.0 I use)
② All configurations are cached in memory, and restart is invalid
③ The information returned after exceeding the configured visits is Sentinel's own and needs to be customized
5-1. Solve the problem that the low version monitoring chart does not come out
Introduce dependency
<!-- spring-boot-starter-actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Add configuration
management.endpoints.web.exposure.include=*
5-2. Configure persistence
5-3. For user-defined rule violation processing, refer to the following: IV. extended use
4, Extended use
1. Create a new configuration class SentinelConfig, which is used for all default rule resources. For example, for requests such as @ GetMapping("/testSentinel"), customize the return content of the request address resource
Note: if it is springboot1 x. Built in spring4, use the following code
If it is springboot2 x. Built in spring5 and using weblux, the code is as follows:
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler; import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; @Configuration public class SentinelConfig { public SentinelConfig() { WebFluxCallbackManager.setBlockHandler(new BlockRequestHandler() { @Override public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) { Mono<ServerResponse> body = ServerResponse.ok().body(Mono.just("{\"msg\":\"Request traffic is too large\",\"code\":0}"), String.class); return body; } }); } }
2. Subdivide multiple monitoring downgrades in the whole request
For example, in @ GetMapping("/testSentinel"), there are two steps A and B for processing business code. It is necessary to monitor the current limiting degradation of step B separately. The following methods can be used
(1) try{}catch {} code block, custom exception
try (Entry entry = SphU.entry("B");){ //... Code for executing B business } catch (BlockException e) { e.printStackTrace(); }
At this time, a new flow restriction rule is added. After setting the flow restriction to a maximum of one request per second, an exception will be thrown when the request is refreshed quickly
(2) , annotation based
① . extract the B business into the B method, and add the annotation @ SentinelResource(value = "B",blockHandler = "handlerB") on the B method. / / parameter 1: rule name; Parameter 2: processing method after method B violates rules
② . create a new handlerB method. The return value and parameters of this method must be exactly the same as those of method B. just return the user-defined prompt in it
5, Gateway monitoring
Sentinel can not only monitor requests and methods and define rules at the business layer, but also limit current at the Gateway layer, such as monitoring and limiting current at the Gateway
1. Add dependency in pom of gateway service
<!--Alibaba Sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId> <version>2.1.0.RELEASE</version> </dependency>
2. Add configuration
spring: cloud: sentinel: transport: port: 8719 dashboard: localhost:8888
3. Test use
4. The unified handling of gateway exceptions is the same as that of business exceptions
Create a new SentinelGatewayConfig class with the following code
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler; import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; @Configuration public class SentinelGatewayConfig { public SentinelGatewayConfig() { GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() { @Override public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) { Mono<ServerResponse> body = ServerResponse.ok().body(Mono.just("{\"msg\":\"Request traffic is too large\",\"code\":0}"), String.class); return body; } }); } }