0, foreword
sentinel has powerful current limiting and degradation functions. It can make rules in the control panel and push them to the microservice;
Rules can be made individually according to the URL or in batches according to the resource name;
We need to pay attention to the following points: 1. GITHUB files have been completely blocked on Amazon servers abroad and can't be downloaded. We can only seek domestic sharing to seek more benefits
2. The rules made by the control panel are only saved in memory, and the restart will disappear. You need to cooperate with other methods to implement persistent storage rules, which should be noted in the production environment
1. Run sentinel
sentinel is just a JAR package. After downloading it, you can directly command to run the JAR. The default port is 8080. You can make other ports to run. As follows, you can specify port 8849 to run:
java -Dserver.port=8849 -jar sentinel-dashboard-1.6.3.jar
2. Project integration
Here we need to find out:
Flow restriction: it means that there are many requests, and customized quick response processing is applied to the service provider itself;
Downgrade: it's a fuse. To put it simply, it means that the service is disconnected, unavailable, and crashed,
So the downgrade logic should be applied to consumers (callers), adding to the service provider itself is meaningless because the service has been disconnected
2-1. Current limiting
2-1-1. Add dependency
<!-- Integrate sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.0.1.RELEASE</version> </dependency>
2-1-2. Add configuration
Note here: project integration sentinel will use a port to open http service, which is used to send heartbeat detection to detect health status,
The sentinel console uses 8719 by default. In the integrated project, after port 8719, an available port will be found automatically. For example, if port 87208720 is not available, 8721 will be found, and then search down in this order;
Some people directly specify the HTTP service port in the configuration file: for example, sentinel.port=8720; this method is not recommended, because our system contains many services, no service will start many instances, if you want to specify, then many instances should be configured one by one, and port conflicts should be avoided, which is tedious and confusing;
Therefore, it is not necessary to specify the port and let the system automatically find the available port. In port allocation, we can avoid the ports of 8700-8800 and deal with large clusters, leaving it reserved. There are many ports
server: port: 8761 spring: application: name: nacos-user cloud: nacos: discovery: server-addr: 127.0.0.1:8848 sentinel: transport: dashboard: localhost:8849 eager: true
2-1-3. Add current limiting in the controller
Add @ SentinelResource annotation to the method,
value is the resource name, which corresponds to a rule specified in sentinel. Multiple methods can use the same name, that is, batch application rules
blockHandler is the name of the method executed after current restriction;
@RequestMapping("/hello") @SentinelResource(value = "userblock1",blockHandler = "BlockHello") //sentinel Current limiting public String Hello() { return ("hello world" +serviceUtil.getPort()); } //-----------Current limiting, degraded handler----------------- public String BlockHello(BlockException exception) //1,Current limiting { return ("Youth, the current is limited --Port: " +serviceUtil.getPort()); } //---------------------------------------------
2-1-4. Operation test
1. Run the program and execute the user/hello method once
2. In the sentinel control panel, add the current restriction rule of userblock1 and run / user/hello again to see the effect:
2-2. Fusing degradation (feign mode)
2-2-1. Add dependency
<!-- Integrate sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.0.1.RELEASE</version> </dependency> <!-- Integrate feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2-2-2. Increase configuration
server: port: 8765 spring: application: name: nacos-order cloud: nacos: discovery: server-addr: 127.0.0.1:8848 sentinel: transport: dashboard: localhost:8849 eager: true feign: sentinel: enabled: true #Turn on sentinel's support for feign
2-2-3. Implementation
Add feign interface
@FeignClient(name = "nacos-user",fallback = UserFallbackService.class) public interface UserClient { @RequestMapping("/user/hello") String hello(); }
Add implementation class
@Component public class UserFallbackService implements UserClient { @Override public String hello() { String rt=""; rt="Junior, the service has been demoted and stopped. Come back later!!"; return rt; } }
controller injection usage
@Autowired private UserClient userClient; @RequestMapping("/order2") public String order2() { return userClient.hello(); }
2-2-4. Operation test
Start order, do not start user, run order/order2, and you can see the running result
sentinel is so simple and flexible, persistence and cluster processing will be discussed later