We have been using Hystrix for circuit breaker downgrade, but hystrix officially no longer maintains Sentinel. Alibaba is behind Sentinel and has been maintaining Sentinel. So let me know first, hystrix can be replaced with Sentinel when hystrix is not enough
And feature rich, here use the following two core functions
- fusing
- Flow control
Start Sentinel console
Sentinel console is developed by Spring Boot, special requirements can be modified source code customization, source code reference: Sentinel-Dashboard can use the Docker one-click start console to write sentinel-Dashboard Dockerfile
FROM its: 8 - JDK - alpine ADD https://github.com/alibaba/Sentinel/releases/download/1.6.0/sentinel-dashboard-1.6.0.jar / sentinel - dashboard - 1.6.0. Jar ENTRYPOINT ["java"."-Dserver.port=8080"."-Dcsp.sentinel.dashboard.server=localhost:8080"."-Dproject.name=sentinel-dashboard"."-jar"."/ sentinel - dashboard - 1.6.0. Jar"."-Dfile.encoding=utf-8"]
Copy the code
Docker build-t sentinel-Dashboard. Then start
docker run --name sentinel-dashboard \
-it --rm -p 8719:8719 -p 8780:8080 sentinel-dashboard
Copy the code
8080 is the sentinel Web control interface port, and 8719 is the communication port between the Sentinel application and the console. For details, see Configuring console information
Open local IP :8780 to view the effect. The default username and password are both sentinel
Create the test project and configure it
Integrating Sentinel with Spring Boot is easy. Find an existing Spring Boot project or go to https://start.spring.io/ to create one
The Sentinel package was introduced, which is used for fuse control and also communicates with the Sentinel console
<! -- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-alibaba-sentinel -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>. 0.9.0 RELEASE</version>
</dependency>
Copy the code
Remember to reimport the POM.xml file after modifying it
In configuration and communication configuration and sentinel console: SRC/main/resources/application properties
spring.application.name=MySentinel spring.cloud.sentinel.eager= true spring.cloud.sentinel.transport.port= 8720 Spring. Cloud. Sentinel. Transport. Dashboard = 127.0.0.1:8780 spring. Cloud. Sentinel. Transport. The heartbeat interval - ms = 500Copy the code
Note that port 8720 here is the port for sentinel and Sentinel console communication on the application side
Start the project and view the effect on the Sentinel console
You can see that the project has successfully established communication with the Sentinel console
Sentinel can fuse multiple HTTP clients, see Feign & RestTemplate support
The most common RestTemplate is used for the request
The spring Boot main class allows sentinel to fuse the RestTemplate.
@Bean
@SentinelRestTemplate
public RestTemplate restTemplate(a) {
return new RestTemplate();
}
Copy the code
The SentinelRestTemplate annotation has several parameters for event callbacks that can perform custom processing logic during a meltdown. See RestTemplate Support
You can study it in detail when you have time
Write fuse test code
Write test controllers such as myController.java in your project. The core code is as follows
@RestController
@RequestMapping("test")
public class MyController {
@GetMapping(value = "/hello")
@SentinelResource("hello")
public String hello(a) {
return "Hello Sentinel"; }}Copy the code
The values in the SentinelResource annotations are resource identifiers, for which you can specify traffic limiting, fusing rules, and so on
Request this address in your browser and then look at the Sentinel console
The drop test
Write a controller route, so that the route can trigger the degradation threshold, the core code is as follows:
@GetMapping(value = "/mye")
@SentinelResource("mye")
public String mye(a) {
if (true) {
throw new RuntimeException("mye");
}
return "mye Sentinel";
}
Copy the code
You can see that this code will throw an exception 100%
Configure a degrade rule click The Degrade Rule -> New Degrade Rule button to add a degrade rule
Right click on the new window to see the larger picture. The fuse rule is to fuse the interface for 30 seconds after the abnormal percentage in the request exceeds 50%
It then requests the exception interface several times, looks at the console log, and after a few exceptions, you can see that Sentinel has started fusing the interface
Current limiting test
Write an output string interface to test, the core code is as follows:
@GetMapping(value = "/myrate")
@SentinelResource("myrate")
public String myrate(a) {
return "myrate Sentinel";
}
Copy the code
Add a traffic limiting rule
Then access the interface in your browser and hold Down CTRL + R to simulate frequent access. Since the threshold is set to an extreme, you can immediately see in the application console that sentinel’s traffic limiting mechanism has been triggered
The “No Message” message is displayed
A couple of points to note
The setinel console occupies two ports (8719, 8080), and the application also occupies two ports (8720, 8666). The 8666 application Web port can be set by yourself
The Sentinel client of the application will cache the sentinel console’s fusing rules, and if the Sentinel console goes down, the application will continue to fuse using the cached fusing rules. You can restart the application to disable the circuit breaker rule
By default, the docker sentinel fuse rule will be lost after the sentinel is restarted. If you need to configure the data source to save the fuse rule, refer to dynamic Data Source support