Spring Cloud for Alibaba's Sentinel For the transformation of production (5)

 

  In the last of the simple integration Sentinel Spring Cloud for Alibaba's (4) , I just Demo inside, simple introduction of the Sentinel components, Dashboard console.

  These data are stored in memory, once the restart of the application, the rules previously configured, monitored data will be reset.

  Production practice, the use of Sentinel, to solve two problems: 1. Rules Management and Push, 2 monitoring data preservation and display.

  Because resources (individuals with no machine deployment kafaka elasticsearch cluster), this major show rules management.

 

First, the rules push mode

 The official recommended to use Push mode, configure the rule, deposited in the center of remote configuration, such as Ali nacos,

 See in particular the official wiki to use:  Use Sentinel in a production environment

Rule flow direction is: Center console configuration / Sentinel arranged center console → → → the Sentinel the Sentinel data source  

As shown below:

 

Two, Sentinel client transformation

     nacos sentinel access storage more convenient, can refer to the official further wiki: dynamic rule extension

1. jar introduced pom     

I used a little simple way, direct introduction of spring-cloud-starter-alibaba-sentinel and sentinel-datasource-nacos components

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>${sentinel.version}</version>
        </dependency>

 

2. New configuration file 

After introduction of the components, the bootstrap configuration file (also can be configured to be placed nacos, not experiment), add the following configuration:

    #sentinel configuration 
spring.cloud: 
    Sentinel: 
        Transport: 
          Port: 7083 # local port for dashboard push rule 
          dashboard: 127.0.0.1:7070 # dashboard console 
        datasource.ds.nacos: 
          Server-addr: $ {nacos.server- address}: $ {nacos.port} #nacos address, according to the actual configuration 
          dataId: $ {spring.application.name} -flow- rules # rule configuration DataID see: dashboard NacosConfigUtil.java source of 
          groupId: groupId see rule SENTINEL_GROUP # : dashboard of NacosConfigUtil.java source 
          rule-type: flow # rule type

The above configuration explain:

  dataId: recommended that $ {spring.application.name} -flow-rules mode, if the rule is flow, with the suffix -flow-rules, different ruleType, with a different suffix, this is the dashboard when given a push rule to die, unanimously recommended to the dashboard.

  groupId: SENTINEL_GROUP This dashboard is given, it is recommended to maintain consistency.  

  ruleType: Value can see RuleType enumeration of sentinel-datasource

Ds Each data source can configure only one rule, if multiple rules, according to the source, the plurality of ds should be configured, such as ds1, ds2 (theory confirmed that subsequent actual) 

 

 

3. nacos new rule configuration

Once configured, we then open nacos, add configuration information, as follows:

dataId: order-demo-flow-rules

groupId: SENTINEL_GROUP

Configuring Content:

[
  {
    "clusterMode": false,
    "controlBehavior": 0,
    "count": 2,
    "grade": 1,
    "resource": "/test/testProductByName/哈哈",
    "strategy": 0
 }
]

 

4. Start the application, dashboard view results

After starting the Sentinel application, access times, refresh the dashboard console, you can see which rules appear in the console:

 

Resources can be called multiple times to view the current limit does take effect, in the console modify, delete rules, although they would take effect, but no change when configuring nacos above, the Sentinel application restart, or use the configuration on nacos.

Operation described above, a: Configuration → center console center configuration data flow → → Sentinel Sentinel the data source,

Basically be used to produce, but there is a problem in that each hand, it may be wrong, so the production is preferably a graphical interface access operation.

  

Three, Dashboard console reform

 Currently Sentinel console for production also requires a lot of transformation, the transformation of the official for a variety of situations and provides api example, you need to rest is to contribute to open source, to transform it!

 Transformation refer to the official wikii console: Sentinel Console (Cluster flow control management)

 1. Download Source introducing IDE

  Console transformation, we need to modify the source code demonstration, we start github above to download the source code, import IDE: Sentinel Source  https://github.com/alibaba/Sentinel

 2. Modify pom, import component

  Dashboard is built components need only be put into test packages directory. 

  Find the sentinel-dashboard module pom, the sentinel-datasource-nacos reference range into compile, remove the test that is good.

      <!-- for Nacos rule publisher sample -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
           <!-- <scope>test</scope>-->
        </dependency>

3. Modify the source code 

   Source There are several places you need to modify: 

 a) The test case file package, the clip to the java directory.

   test package under the path: com.alibaba.csp.sentinel.dashboard.rule.nacos, following a total of FlowRuleNacosProvider, FlowRuleNacosPublisher, NacosConfig, NacosConfigUtil four files.

 b) If nacos deployed in the machine, and the default port number is 8848, then without modification. Otherwise, according to the actual need to modify the code NacosConfig

@Bean
     public the ConfigService nacosConfigService () throws Exception {
         return ConfigFactory.createConfigService ( "localhost"); // if not native, localhost nacos address may be modified, such as: IP: Port 
    }

 c) modify FlowControllerV2

  The ruleProvider, rulePublisher, modify flowRuleNacosProvider, flowRuleNacosPublisher, as follows:

    @Autowired
    @Qualifier("flowRuleNacosProvider")
    private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("flowRuleNacosPublisher")
    private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

d) Modify sidebar.html

  The dashboard.flowV1 into dashboard.flow, as follows:

        <li ui-sref-active="active" ng-if="!entry.isGateway">
            <a ui-sref="dashboard.flow({app: entry.app})">
              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a>
          </li>

After doing the above changes, recompile the source code to start.

4, start testing 

   After the start, we should be able to flow from inside the control rules, see the corresponding rules. 

  Now we modify the threshold of 3, click Save, view nacos the above configuration is as follows:

[{"app":"order-demo","clusterMode":false,"controlBehavior":0,"count":3.0,"gmtModified":1566381446083,"grade":1,"id":14,"limitApp":"default","resource":"/test/testProductByName/哈哈","strategy":0}]

 We delete this, and then build a re-test again next:

 Newly added configuration is as follows:

[
    {
        "app": "order-demo",
        "clusterConfig": {
            "fallbackToLocalWhenFail": true,
            "sampleCount": 10,
            "strategy": 0,
            "thresholdType": 0,
            "windowIntervalMs": 1000
        },
        "clusterMode": false,
        "controlBehavior": 0,
        "count": 1,
        "gmtCreate": 1566381613072,
        "gmtModified": 1566381613072,
        "grade": 1,
        "id": 15,
        "ip": "192.168.55.1",
        "limitApp": "default",
        "port": 7083,
        "resource": "/test/testProductById/123",
        "strategy": 0
    }
]

Newly added configuration than the previous handwritten by nacos console, is to increase the number of content.

After the modification, by accessing the resources, you can see the relevant request is being blocked in.

 

 IV Summary

     By modifying the above configuration nacos dashboard, into the path for the flow control rules into the menu. From the   cluster-point link menu, the right button to add flow control, is inaccessible, this button is still accessible v1 / flow / rules path.

    For rule downgrade, hot, authorization, and other storage systems, reference may be the above example, to modify their Dashboard code.

    For storage and display of data storage requires a combination of their respective companies big data framework, combined with the company's show also operation and maintenance platform, for the average small company feel demand is not too urgent, after all, the first guarantee stable operation of the system. 

    Thanks to Sentinel's well-designed, relatively small core source code, including: Context, Node organizations, sliding window algorithm, various trigger downgrade rules are relatively simple, around the perimeter of the core source code is more, such as heart rate, receiving dashboard command

 For cluster traffic of Cluster and Client mode, the general feeling is also less than the current scene have not met, need to be so precise flow control will require precise control of the scene, with the Sentinel to achieve, is not a good solution. 

 

 Source Address: https://github.com/xujianguo1/alicloud-demo   Release-Tag: Sentinel-Config

 Comments are welcome, any questions welcome to explore together.

Guess you like

Origin www.cnblogs.com/keep-code/p/11390456.html