This is the 22nd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Spring Cloud Sentinel integrates Nacos to realize dynamic flow control rules. The steps are as follows:

  1. Add maven dependencies for Nacos data sources

    <dependency>
    	<groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
        <version>1.7.0</version>
    </dependency>
    Copy the code
    1. Create a REST interface for testing
    @RestController
    @RequestMapping("/sentinel")
    public class SentinelController {
    
        @GetMapping(value = "/info")
        public String getName(a){
            return " Dynamic Rule"; }}Copy the code
  2. Add the data source in the application.yml file configuration

spring:
	application:
		name: spring-cloud-sentinel
	cloud:
		sentinel:
			transport:
				dashboard: 127.0. 01.: 8718
			datasource:
				nacos: 
					server-addr: 127.0. 01.: 8848
					data-id: ${spring.applicaton.name}-dev
					group-id: DEFAULT_GROUP
					data-type: json
					rule-type: flow
Copy the code

The following table describes some parameters:

  • Datasource: Supports Nacos, Redis, Zookeeper, Apollo, and File
  • Data-id: ${spring.application.name} is used to distinguish the configuration files of different applications
  • Data-type: the content format of the configuration item. Spring Cloud Alibaba Sentinel provides JSON and XML. If you need to customize the content format, Set the value to custom, and Converter-class points to the Converter class
  • Services like flow, GW-flow, param-flow, etc. are checked by the service provider.

Visit http://127.0.0.1:8848/nacos to enter Nacos console, to create a flow control configuration rules, the specific configuration information is as follows:

{
        "resource":"teaching-management"."count":1000."grade":1."limitApp":"default"."strategy":0."controlBehavior":0
    }
Copy the code

Visit http://127.0.0.1:8718 to enter Sentinel Dashboard and find [Flow Control Rules] under the corresponding module menu. It can be seen that the flow control rules configured on Nacos lock have been loaded

After modifying corresponding flow control rules on Nacos console, the modification of flow control rules can be synchronized in real time and can be viewed on Sentinel Dashboard.

There are two ways to dynamically modify flow control rules:

  1. Modify through the Nacos console.
  2. Modify it on Sentinel Dashboard.

The flow control rules can be synchronized to Sentinel Dashboard by modifying flow control rules on Nacos console. Nacos is a persistent and dynamic modification platform of flow control rules. If the corresponding flow control rules are modified on Sentinel Dashboard, they can also be synchronized to Nacos, so as to achieve bidirectional synchronization of flow control rules. However, Sentinel Dashboard has not been able to implement the synchronization of flow control rules to Nacos.