部署sentinel控制臺
- 拉取sentinel代碼摇展,自己修改后增加nacos持久化https://github.com/hcq0514/Sentinel 注意可自行使用master分支楞卡,未修改可正常運行,my分支是修改完的
- 進入到dashboard項目中,使用如下命令將代碼打包成一個 sentinel-dashboard.jar
mvn clean package
3.將jar包打包成docker鏡像
- Dockerfile
# 該鏡像需要依賴的基礎鏡像
FROM java:8
# 將當前目錄下的jar包復制到docker容器的/目錄下
ADD sentinel-dashboard.jar /sentinel-dashboard.jar
# 聲明服務運行端口
EXPOSE 8885
# 指定docker容器啟動時運行jar包
ENTRYPOINT ["java", "-jar","/sentinel-dashboard.jar"]
# 指定維護者的名字
MAINTAINER hcq
- run.sh
#!/usr/bin/env bash
# 定義應用組名
group_name='mall'
# 定義應用名稱
app_name='sentinel2'
# 定義應用版本
app_version='1.0-SNAPSHOT'
# 定義應用環(huán)境
profile_active='test'
echo '----copy jar----'
docker stop ${app_name}
echo '----stop container----'
docker rm ${app_name}
echo '----rm container----'
docker rmi ${group_name}/${app_name}:${app_version}
echo '----rm image----'
# 打包編譯docker鏡像
docker build -t ${group_name}/${app_name}:${app_version} .
echo '----build image----'
docker run -p 8885:8885 --name ${app_name} \
-e TZ="Asia/Shanghai" \
-e Dserver.port="8885" \
-e Dcsp.sentinel.dashboard.server="8885" \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/${app_name}/logs:/var/logs \
-d ${group_name}/${app_name}:${app_version}
echo '----start container----'
- 運行. run.sh
- 登錄控制臺查看
http://xxx:8885/#/login
網(wǎng)關集成 (部署的應用不管是網(wǎng)關還是項目都需要與sentinel的控制臺發(fā)送心跳秒赤,需要ip等妆档,盡量都使用云服務器等訪問的到的主機萍悴,用本地有可能會導致鏈接不到岩齿,無法展示請求鏈路等其他信息)
- 網(wǎng)關層添加依賴(這邊只添加與sentinel有關的依賴尊浓,其他組件不展示)
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.0</version>
</dependency>
- 增加配置文件
如果用docker部署的話記得在dockerFile里面指定project.name這個是客戶端注冊到控制臺的名稱
示例文件
# 該鏡像需要依賴的基礎鏡像
FROM java:8
# 將當前目錄下的jar包復制到docker容器的/目錄下
ADD mall-portal-1.0-SNAPSHOT.jar /mall-portal-1.0-SNAPSHOT.jar
# 聲明服務運行在8085端口
EXPOSE 8085
# 指定docker容器啟動時運行jar包
ENV JAVA_OPTS="\
-Xmx1g \
-Xms1g \
-Xmn512m \
-XX:SurvivorRatio=8 \
-XX:MetaspaceSize=256m "
ENTRYPOINT java ${JAVA_OPTS} -Dproject.name=mall-portal-service -jar mall-portal-1.0-SNAPSHOT.jar
application.pro
spring:
main:
allow-bean-definition-overriding: true
cloud:
sentinel:
transport:
dashboard: localhost:8885 #部署的dashboard地址
client-ip: localhost #本機的訪問ip
port: 8720 #與sentinel交互心跳信息的端口(docker部署項目一點要開啟這個端口,每個項目都不一樣)
datasource:
# 名稱隨意,這些是dashboard做持久化后添加規(guī)則纯衍,會在nacos的配置文件中生成該配置(沒用可不添加)
flow:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
rule-type: flow
degrade:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
rule-type: degrade
system:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
dataId: ${spring.application.name}-system-rules
groupId: SENTINEL_GROUP
rule-type: system
authority:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
dataId: ${spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
rule-type: authority
param-flow:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
rule-type: param-flow
eager: true
MVC項目集成
- 添加依賴(因為此處有版本沖突栋齿,則自己制定版本號)
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.3.RELEASE</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.0</version>
</dependency>
- 增加配置文件(與網(wǎng)關相同,這邊不重復)
open feign集成(默認已導入springcloud與openFeign包)
- 開啟sentinel feign配置
feign:
sentinel:
enabled: true
- 在feign客戶端上添加處理函數(shù)就行(也可添加fallback,不過就不能取到異常)
@FeignClient(value = "mall-member-service", fallbackFactory = MemberFeignClientFallbackFactory.class)
@Component
@Slf4j
public class MemberFeignClientFallbackFactory implements FallbackFactory<MemberFeignClient> {
@Override
public MemberFeignClient create(Throwable throwable) {
log.error("fallback reason:{}", throwable.getMessage());
return new MemberFeignClient() {
@Override
public CommonResult<String> getMemberTest() {
String info = "該鏈接發(fā)生異常襟诸,請稍后重試";
if (throwable instanceof FlowException) {
info = "該鏈接已流控瓦堵,請稍后重試";
} else if (throwable instanceof DegradeException) {
info = "該鏈接已降級,請稍后重試";
} else {
info = "該鏈接發(fā)生異常歌亲,請稍后重試";
}
Asserts.fail(info + throwable.getMessage());
return null;
// return CommonResult.failed(info + throwable.getMessage());
}
};
}
}
到這邊基礎的集成已經(jīng)完成
如果正常的話在sentinel的機器列表可以看到有注冊信息
sentinel配置持久化到nacos
- 把官方源碼拉下來
- 將sentinel-dashboard的pom文件中把范圍test注釋掉
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!-- <scope>test</scope>-->
</dependency>
-
將示例文件拷貝到dashboard里面(這里面有3種框架的集成方法)
把 nacosConfig里面的nacos地址修改為自己的nacos地址
@Bean
public ConfigService nacosConfigService() throws Exception {
return ConfigFactory.createConfigService("your nacos address");
}
- 修改controller的方法讓它直接推到nacos(以FlowControllerV1 限流controller為例)
添加依賴
@Autowired
private FlowRuleNacosPublisher flowRuleNacosPublisher;
@Autowired
private FlowRuleNacosProvider flowRuleNacosProvider;
將所有sentinelApiClient.fetchFlowRuleOfMachine(app, ip, port);
調用的地方都改成 flowRuleNacosProvider.getRules(app)
將所有sentinelApiClient.setFlowRuleOfMachineAsync(app, ip, port, rules);
調用的地方都改成 flowRuleNacosPublisher.publish(app, rules);
- 修改其他的controller
- 可直接參考我sentinel下面的my分支菇用,已經(jīng)實現(xiàn)了持久化