原文連接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorialsjava
TIPSios
This article based on:agit
- Spring Boot 2.1.5
- Spring Cloud Greenwich.SR1
- Spring Cloud Alibaba 0.9.0
- Nacos 1.0.0
With the popularity of microservices, the stability between services and services is becoming more and more important. Sentinel uses traffic as an entry point to protect the stability of services from multiple dimensions such as flow control, blowdown, and system load-balance protection.github
In a nutshell, Sentinel is a lightweight flow control, blowdown and degraded Java library.web
Sentinel has the following characteristics:spring
Add dependencies:shell
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>0.2.1.RELEASE</version> </dependency>
Add configurations:json
server: port: 8010 spring: application: # Specify the name of the service registered to the nacos server name: microservice-consumer-movie cloud: nacos: discovery: server-addr: 127.0.0.1:8848 management: endpoints: web: exposure: include: '*'
Add Controller:app
@RequestMapping("/movies") @RestController public class MovieController { @Autowired private RestTemplate restTemplate; @GetMapping("/users/{id}") public User findById(@PathVariable Long id) { // Use the placeholder of the RestTemplate User user = this.restTemplate.getForObject( "http://microservice-provider-user/users/{id}", User.class, id ); // ...Movie microservices business... return user; } }
It can be seen from the code that this one can't be normal controller! Because Sentinel starter will provide a current limit for all HTTP services by default, the Controller can be protected by Sentinel (but there are no rules for configuring protection yet, so it has not been protected yet)!less
Access http://localhost:8010/actuator/sentinel
,the following results can be obtained:
{ "DegradeRules": [], "datasources": {}, "ParamFlowRule": [], "SystemRules": [], "FlowRules": [], "properties": { "eager": false, "enabled": true, "datasource": {}, "transport": { "port": "8719", "dashboard": "localhost:8080", "heartbeatIntervalMs": null }, "metric": { "fileSingleSize": null, "fileTotalCount": null, "charset": "UTF-8" }, "servlet": { "blockPage": null }, "filter": { "order": -2147483648, "urlPatterns": ["/*"] }, "flow": { "coldFactor": "3" }, "log": { "dir": null, "switchPid": false } } }
At the moment, we don't know what the meaning of the result exposed by /actuator/sentinel
is. It doesn't matter, please read on.
Just add the spring-cloud-starter-alibaba-sentinel
dependency to your app, and all HTTP interfaces get Sentinel protection! Of course, we currently have no rules for configuring protection for Sentinel.
原文連接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials
本文由博客一文多發平臺 OpenWrite 發佈!