Sentinel integrated Nacos data source

Posted by promovi on Fri, 18 Feb 2022 16:17:56 +0100

1.1 installation of Sentinel console

stay Download the jar package of the console from the page and put it in a folder on the server

Start the console program with the following command, - dserver Port specifies that the port is 9010. If there is a port conflict, you can specify another port

java -Dserver.port=9010 -Dcsp.sentinel.dashboard.server=localhost:9010  -jar -Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError  $APP_NAME >/dev/null 2>
&1 &

adopt http://localhost:9010 You can access the console. The user name and password are sentinel/sentinel by default

1.2 configuring Sentinel

Add the corresponding dependency in the pom file of the shop user module


Then configure the communication port between the client and the console and the console address in the yaml file

      	# Communication port number between sentinel client and console (the default is 8719, which can be specified arbitrarily)
        port: 8719 

Start the shop user application, call any interface, and you can see the relevant information of the shop user service on the sentinel console

Note: Sentinel is a lazy loading mode. You can only see the service on the console after calling the interface. You can also set spring cloud. sentinel. Eager: true, the heartbeat connection is established when the service is started

Detailed use reference:

1.3 configuration persistence

In Sentinel, various rules can be set for each client through the console, but these rules are stored in memory by default. Each time the service is repeated, the configuration will be changed, which is not applicable to the production environment. Therefore, Sentinel provides dynamic extensions of pull and push, which correspond to different data sources respectively

patternSupport extended data sourcesGet rule methodadvantageshortcoming
pullDynamic file data source, Consul, EurekaPeriodic pollingSimple accessPoor real-time performance
pushZookeeper,Redis,Nacos,ApolloListening rule changeHigh consistency and real-timeSynchronizing rules to data sources is complex

Take Sentinel integrating Nacos as a data source as an example

Add the corresponding dependency in the pom file of shop user

<!-- sentinel integrate nacos As a data source  -->

Then configure the relevant properties of the data source in the configuration file, because spring cloud. sentinel. The datasource attribute corresponds to a Map, so it needs to customize the KEY of the specified Map. In the following configuration, ds is used as the KEY, and then different data sources can be configured later

            namespace: 24712b7c-05ad-4b79-af97-1d202431f521
            dataId: shop-user-sentinel.json
            groupId: LZ_GROUP_MASTER
            rule-type: flow
            data-type: json

You can see the property classes corresponding to different data sources in DataSourcePropertiesConfiguration. Refer to these property classes for specific parameters

public class DataSourcePropertiesConfiguration {

   private FileDataSourceProperties file;

   private NacosDataSourceProperties nacos;

   private ZookeeperDataSourceProperties zk;

   private ApolloDataSourceProperties apollo;

   private RedisDataSourceProperties redis;

   private ConsulDataSourceProperties consul;

Then create the configuration of current limiting rules in the console of Nacos

The configuration contents are as follows:

        "resource": "/user/test",
        "limitApp": "default",
        "grade": 1,
        "count": 2,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
  • Resource: resource name, that is, the object of the flow restriction rule
  • limitApp: the call source of flow control. If it is default, the call source will not be distinguished
  • grade: current limiting threshold type (QPS or number of concurrent threads); 0 means to limit the current according to the concurrent quantity, and 1 means to control the flow according to QPS
  • count: current limiting threshold
  • Strategy: call relationship flow limiting strategy
  • controlBehavior: flow control effect (direct rejection, Warm Up, uniform queuing)
  • clusterMode: whether it is a cluster mode

Sentinel console does not have the ability to synchronously modify the configuration of Nacos, which can be updated automatically by using Listener in the client. Therefore, after integrating Nacos as rule storage, we need to know that there are different effects of modification in the following two places:

  • Modification rule in Sentinel console: it only exists in the memory of the service, and the configuration value in Nacos will not be modified. The original value will be restored after restart.

  • Modify rules in the Nacos console: the in memory rules of the service will be updated, and the persistence rules in Nacos will also be updated, which will remain after restart.

The principle of this book is synchronized with that of Alibaba cloud console

When configuring Sentinel's data source, be sure to add an additional string after the datasource attribute as the KEY of the data source Map

Topics: Java Spring Cloud sentinel