继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Sentinel Getting Started Tutorials

子木聊出海
关注TA
已关注
手记 74
粉丝 19
获赞 43

Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

TIPS

This article based on:a

  • Spring Boot 2.1.5
  • Spring Cloud Greenwich.SR1
  • Spring Cloud Alibaba 0.9.0
  • Nacos 1.0.0

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.

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

Sentinel has the following characteristics:

  • 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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP