上節(jié)我們在springcloud Alibaba 入門之采用Sentinel實現(xiàn)接口限流文章中簡單知道了sentinel其中的一個作用,我們這里只用它來做限流的操作,但是有個問題不知道大家知道不,當(dāng)我們將sentinel控制臺重啟之后,我們自己定義的限流規(guī)則會丟失,那么本篇
博客就是為了解決該問題而寫的,全程采用的都是本地服務(wù),大致分為了兩步走:
- 一是修改sentinel Dashboard的規(guī)則然后進(jìn)行同步到nacos中
- 二是在nacos管理臺對我們限流的規(guī)則進(jìn)行自定義的過程
首先我們來看修改sentinel Dashboard的規(guī)則,我們需要fork sentinel的源碼在其基礎(chǔ)上修改,fork 源碼的地址為:Sentinel源碼地址,具體的步驟如下:
- a.首先我們來修改pom.xml中的sentinel-datasource-nacos的依賴,將<scope>test</scope>注釋掉,這樣做的目的是為了在我們的主程序中起到作用,代碼如下:
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
</dependency>
- b.接著我們需要修改resources/app/scripts/directives/sidebar/sidebar.html頁面中的一段代碼如:
<li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flowV1({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控規(guī)則</a>
</li>
修改之后為:
<li ui-sref-active="active">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控規(guī)則
</a>
</li>
- c.在com.alibaba.csp.sentinel.dashboard.rule包下新建一個nacos包,該包下主要存放我們對nacos的拓展規(guī)則以及配置的書寫.
- d.在nacos包下創(chuàng)建Nacos的配置類,代碼如下:
''''
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Properties;
/**
* @author cb
*/
@Configuration
public class NacosConfig {
@Bean
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "localhost");
return ConfigFactory.createConfigService(properties);
}
}
- e.我們需要對nacos的配置進(jìn)行遠(yuǎn)程拉取,代碼如下:
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @author cb
*/
@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<FlowRuleEntity>> converter;
public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
public static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
簡單的來說下該類中的方法getRules的作用,我們看到參數(shù)為appName,其中appName為我們sentinel控制臺中的應(yīng)用名稱,這個我們在上節(jié)大概都知道,還有我們看到配置了public static final String FLOW_DATA_ID_POSTFIX = "-sentinel"這個參數(shù),該參數(shù)主要的目的是我們在nacos中的dataId的后綴,代碼如下:
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
那么方法中的configService#getConfig(...)該方法就是為了動態(tài)獲取我們在nacos管理臺上的配置屬性.
- f.完成了拉取遠(yuǎn)程的配置操作,我們需要將拉取的配置進(jìn)行推送到sentinel平臺完成展示,具體代碼如下:
''''
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author cb
*/
@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<List<FlowRuleEntity>, String> converter;
public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
public static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules));
}
}
- g.這也是最后一步,我們需要修該com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2中DynamicRuleProvider和DynamicRulePublisher注入的bean操作,代碼如下:
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
到這里我們的sentinel Dashboard的修改完成了,啟動它,我們會在控制臺看到這樣的日志輸出:
接著訪問127.0.0.1:8080,會看到如圖的界面:
同樣我們輸入用戶名和密碼均為:sentinel,會看到如下圖的主界面:
那么以上便是關(guān)于sentinel Dashboard的修改操作,接下來是我們的重點,如何在nacos中定義我們的限流的規(guī)則
限流規(guī)則的定義
首先我們需要創(chuàng)建一個服務(wù),我這里名字為alibaba-nacos-sentinel,沒啥別的作用,主要是為了跟之前服務(wù)的對稱,至于創(chuàng)建過程之前已經(jīng)說了,接著我們來看需要的依賴,代碼如下:
</parent>
<groupId>com.alibaba.sentinel</groupId>
<artifactId>alibaba-nacos-sentinel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>alibaba-nacos-sentinel</name>
<description>利用sentinel實現(xiàn)限流</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--nacos整合sentinel-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 接下來我們來看看配置文件,代碼如下:
server.port=9007
spring.application.name=alibaba-nacos
spring.cloud.sentinel.transport.dashboard=127.0.0.1:8080
spring.cloud.sentinel.datasource.ds.nacos.server-addr=127.0.0.1:8848
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow
目前是學(xué)習(xí)階段所以只是簡單的配置我所用到的,這樣看著簡潔,當(dāng)然大家可以配置成.yml文件格式,看個人
- 主程序,代碼入下:
package com.alibaba.sentinel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author cb
*/
@SpringBootApplication
public class AlibabaNacosSentinelApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaNacosSentinelApplication.class, args);
}
}
- 測試接口代碼入下;
package com.alibaba.sentinel.controller
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello Sentinel";
}
}
一個簡單的接口測試,到這里我們需要的服務(wù)代碼的編寫完成了,接著啟動我的本地注冊中心nacos,接著啟動我的服務(wù),然后在nacos的管理臺上配置我們的限流規(guī)則,如圖所示:
- 配置限流格式的代碼如下:
{
"resource": "/hello",
"limitApp": "default",
"grade": 1,
"count": 5,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
標(biāo)準(zhǔn)的json格式,同樣我們就會在后臺服務(wù)的日志中看到如圖的信息:
看到如圖的信息,說明我們的配置能被服務(wù)加載到了,接著用postman來進(jìn)行接口調(diào)用的測試,于此同時在我們sentinel 管理臺上會出現(xiàn)相應(yīng)服務(wù)的請求信息顯示,如圖:
接著我們來查看流控規(guī)則,你會發(fā)現(xiàn):
那看到如圖的信息,就說明我們借助于nacos管理臺實現(xiàn)了限流規(guī)則的拉取和讀取的過程,當(dāng)然最重要的是持久化的過程,不信你可以關(guān)閉頁面重新打開,配置的規(guī)則依然是存在的,到這里我們的目的也就達(dá)成了,關(guān)于sentinel的深入學(xué)習(xí),大家可以看看官方文檔....