一.sentinel介紹
随着微服務的流行,服務和服務之間的穩定性變得越來越重要。Sentinel 是面向分布式服務架構的流量控制元件,主要以流量為切入點,從限流、流量整形、熔斷降級、系統負載保護、熱點防護等多個次元來幫助開發者保障微服務的穩定性。
官網 git:
https://github.com/alibaba/Sentinel/wiki本文主要是講解如何将sentinelDashboard頁面配置的規則資料如何與nacos進行互相轉化和同步。sentinelDashboard頁面如圖所示:

使用sentinel流控的方式有基于注解的方式,但是代碼侵入較大,本文主要講解的是使用sentinel控制台最小化的對應用進行流控改造。
二.下載下傳dashborad與啟動
在github上面下載下傳sentinel(
sentinel源碼),然後找到sentinelDashboard子產品進行啟動即可,預設端口8080,本文修改了端口為8020,啟動之後如圖所示:
三.應用對接sentinel
在pom檔案中加入依賴
< dependency >
< groupId>org.springframework.cloud< /groupId >
< artifactId>spring-cloud-starter-openfeign< /artifactId >
< version>2.1.1.RELEASE</version >
< exclusions >
< exclusion >
< groupId>com.netflix.archaius< /groupId >
< artifactId>archaius-core< /artifactId >
</exclusion >
</exclusions >
</dependency >
< !-- sentinel start -- >
< !-- sentinel核心庫 -- >
< dependency >
< groupId >com.alibaba.csp< /groupId >
< artifactId >sentinel-core< /artifactId >
< version >1.8.1< /version >
< /dependency >
< !-- 通過nacos持久化流控規則 -- >
< dependency >
< groupId >com.alibaba.csp< /groupId >
< artifactId >sentinel-datasource-nacos< /artifactId >
< version >1.8.1< /version >
< /dependency >
< !-- sentinel 整合spring cloud alibaba -- >
< dependency >
< groupId >com.alibaba.cloud< /groupId >
< artifactId >spring-cloud-starter-alibaba-sentinel< /artifactId >
< version >2.1.2.RELEASE< /version >
< exclusions >
< exclusion >
< artifactId >sentinel-core< /artifactId >
< groupId >com.alibaba.csp< /groupId >
< /exclusion >
< /exclusions >
< /dependency >
< !-- sentinel用戶端與dashboard通信依賴 -- >
< dependency >
< groupId >com.alibaba.csp< /groupId >
< artifactId >sentinel-transport-simple-http< /artifactId >
< version >1.8.1< /version >
< /dependency >
配置檔案中新增配置:
spring:
application:
name: sentinel-demo
cloud:
sentinel:
transport:
dashboard: localhost:8020
port: 8719
###心跳檢測開啟
eager: true
###配置資料源
datasource:
###流控規則
flow:
nacos:
server-addr: localhost:8848
###與dashboard中的設定的dataId保持一緻
dataId: ${spring.application.name}-flow-rules
###與dashboard中的設定的groupId保持一緻
groupId: SENTINEL_GROUP
data-type: json
rule-type: flow
###熔斷規則
degrade:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
data-type: json
rule-type: degrade
至此,用戶端對接sentinle已完成,啟動應用之後可以在dashboard頁面檢視到相應的服務.
四.sentinel規則資料持久化
-
sentinelDashboard下載下傳與啟動
1.1 從github下載下傳sentinel-master到本地,打開sentinel-dashboard子產品,如圖所示:
1.2 将pom檔案中的sentinel-datasource-nacos 的scope注釋掉,如圖所示:
1.3 代碼改造
sentinel的規則資料是由DynamicRuleProvider(讀取)和DynamicRulePublisher(推送)兩個接口定義的,我們隻需要實作這兩個接口即可;代碼如下
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
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 xu.ming
* @since 1.4.0
*/
@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<FlowRuleEntity>> converter;
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String rules = configService.getConfig(
appName + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
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 xu.ming
* @since 1.4.0
*/
@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<List<FlowRuleEntity>, String> converter;
@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 + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, converter.convert(rules));
}
}
由于sentinelDashboard需要依賴nacos,我們還需要在配置檔案中添加nacos相關的配置:
如圖所示:
####xu.ming 新增nacos配置
nacos.address=localhost:8848
nacos.namespace=30e60261-cfc4-4176-a816-1db3b1e0cb17
nacos.username=nacos
nacos.password=nacos
@Configuration
public class NacosConfig {
@Value("${nacos.address}")
private String address;
@Value("${nacos.namespace}")
private String namespace;
@Value("${nacos.username}")
private String username;
@Value("${nacos.password}")
private String password;
@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, address);
properties.put(PropertyKeyConst.NAMESPACE, namespace);
properties.put(PropertyKeyConst.USERNAME, username);
properties.put(PropertyKeyConst.PASSWORD, password);
return ConfigFactory.createConfigService(properties);
}
}
将sentineDashboard--controller-->FlowControllerV2中的provider和publisher修改為剛才自定義的兩個類.
修改頁面代碼:将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> 流控規則</a>
</li>
修改為:
<li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控規則</a>
</li>
至此.流控規則dashboard頁面修改完成,我們修改流控規則之後可以在nacos中看到其生成的規則資料.
同時,我們也可以在nacos中修改規則,dashboard頁面也可以相應同步.