Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

原文連接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorialsjava

Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

TIPSios

This article based on:agit

- Spring Boot 2.1.5github

- Spring Cloud Greenwich.SR1web

- Spring Cloud Alibaba 0.9.0spring

- Nacos 1.0.0shell

1. What is Sentinel ?

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.app

In a nutshell, Sentinel is a lightweight flow control, blowdown and degraded Java library.less

Sentinel has the following characteristics:ide

  • Rich application scenarios:Sentinel undertakes the core scene of Alibaba's "Double Eleven" promotion traffic for nearly 10 years. For example, spikes (that is, burst flow control can be tolerated in the system capacity), message peaking and valley filling, cluster flow control, real-time fuse downstream applications that are not available.
  • Complete real-time monitoring:Sentinel also provides real-time monitoring. You can see the single machine second-level data of the access application in the console, or even the aggregate operation of clusters of less than 500 sizes.
  • Extensive open source ecology:Sentinel Provides out-of-the-box integration modules with other open source frameworks/libraries. For example, integration with Spring Cloud, Dubbo, gRPC. You only need to introduce the appropriate dependencies and perform a simple configuration to quickly access Sentinel.
  • Complete SPI extension point:Sentinel provides an easy-to-use, comprehensive SPI expansion interface。You can quickly customize the logic by implementing an extension interface. Such as custom rule management, adapting dynamic data sources, etc.

2. Guides

  • Add dependencies:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>0.2.1.RELEASE</version>
  </dependency>複製代碼

  • Add configurations:
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:
@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)!

3. Test

  • 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.

4. Summary

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.

5. Sample Code

GitHub

Gitee

原文連接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

本文由博客一文多發平臺 OpenWrite 發佈!

相關文章
相關標籤/搜索