SpringCloud 微服務監控方案sentinel + spring boot admin
前言:項目基于springcloud架構,服務注冊中心使用的是Eureka
spring boot admin 完整建構過程
- 隻需要搭建一個spring boot admin用戶端
所需依賴
spring-cloud-starter-netflix-eureka-client将spring boot admin用戶端注冊到Eureka
spring boot admin 底層還是調用的actuator的接口擷取的資料,需要添加actuator的依賴
spring-boot-admin-starter-server用戶端依賴,該依賴應該與spring boot的版本一緻,否則可能會導緻依賴沖突,spring boot admin 2.2.0以後的頁面才開始支援中文
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.1.5</version> </dependency>
啟動類中添加@EnableAdminServer注解
application.yml中需要添加
management: health: redis: enabled: false #防止因為redis而導緻健康檢查不過,導緻在admin中顯示應用狀态為down mail: enabled: false endpoints: web: exposure: include: "*" #這裡因為是測試可以用*,暴露所有,但生産環境需要特别設定 endpoint: health: show-details: always
- 其他想要注冊到spring boot admin用戶端的應用隻需要注冊到Eureka并添加actuator依賴就可以在頁面看到了
application.yml中也需要添加<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
server: port: 9002 servlet: context-path: /test eureka: client: service-url: defaultZone: http://127.0.0.1:6868/eureka/ instance: prefer-ip-address: true ##如果項目有上下文請求路徑的配置需要加以下配置,不然admin掃描不到對應的應用 metadata-map: management: context-path: ${server.servlet.context-path}/actuator health-check-url-path: actuator/health status-page-url-path: actuator/info management: health: redis: enabled: false endpoints: web: exposure: include: "*" endpoint: health: show-details: always
後面發現個很嚴重的問題,我們項目使用的druid多資料源,配置springbootadmin後發現資料庫連接配接池被占滿了,這個時候需要修改配置檔案加上以下配置後解決
management:
endpoint:
configprops:
enabled: false
通路spring boot admin用戶端應用對外暴露的端口就可以打開頁面了
如果項目使用了spring security 需要把spring boot admin的相關請求放過,不然無法通路首頁
@Override
public void configure(HttpSecurity http) throws Exception {
//所有請求必須認證通過
http.authorizeRequests()
.antMatchers("/","/instances/**","/sba-settings.js","/assets/**","/actuator/**","/applications") //配置位址放行
.permitAll()
.anyRequest()
.authenticated(); //其他位址需要認證授權
super.configure(http);
}
通路位址:http://ip:port/context-path/#/applications
這裡我注冊了五個應用進來,有2個健康檢查有問題,我沒去管,反正隻是測試
還可以檢視應用當天的日志
日志這個需要在應用的application.yml中添加配置
logging:
level:
xxx.xxx.**: debug
# 友善Spring Boot Admin頁面上實時檢視日志
file: D:/log/file/info.log
應用的日志配置檔案logback.xml也需要特别設定,下面隻說關鍵位置的修改
注釋掉,可以列印出項目的所有日志,和同時存在,當天的日志名稱就為info.log,當過渡到第二天的一瞬間會把info.log名字變更為{yyyy-MM-dd}info.log前一天的日期,同時産生一個新的info.log記錄當天的日志,是以上面application.ym配置中隻需要監聽file: D:/log/file/info.log 這個日志檔案名就可以了
<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- <filter class="ch.qos.logback.classic.filter.LevelFilter">-->
<!-- <level>INFO</level>-->
<!-- <onMatch>ACCEPT</onMatch>-->
<!-- <onMismatch>DENY</onMismatch>-->
<!-- </filter>-->
<file>${FILE_PATH}/info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${FILE_PATH}/%d{yyyy-MM-dd}info.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} %yellow(%-5level) %red(%logger) --- [%thread] %highlight(%class %method) %line -%msg%n
</Pattern>
</encoder>
</appender>
阿裡的sentinel搭建過程
檢視https://github.com/alibaba/Sentinel sentinel github位址
sentinel有三種規則的推送模式,生産一般使用push模式
push模式需要下載下傳源碼進行一些改造,這裡使用将sentinel的(流控規則、熔斷規則、熱點規則、系統規則、授權規則)五種規則持久化到nacos配置中心,配置我們自己的nacos位址,實作sentinel和nacos配置的雙向的流轉和實時變更,即使重新開機應用服務,也會從nacos讀取之前的規則配置資訊,實作持久化
下載下傳Sentinel源碼,打開sentinel-dashboard子產品,對該子產品進行如下的改造
- 修改pom檔案中的如下依賴,注釋掉test scope
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!--<scope>test</scope>-->
</dependency>
- 打開application.properties檔案,添加Nacos相關配置。
nacos.address=localhost:8848 # 如果要指定namespace就填入,不填預設是public。groupId預設值SENTINEL_GROUP。 nacos.namespace=32263b9a-140f-4121-baf1-11e57bf63b21 # 如果Nacos開啟了鑒權,添加賬号密碼。 nacos.username=nacos nacos.password=nacos
- 修改
類,引入剛剛填寫的配置資訊com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfig
- 複制src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos整個檔案夾到src/main/java/com/alibaba/csp/sentinel/dashboard/rule檔案夾下
- 打開
類,注入我們剛剛的限流規則類。com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2
- 打開src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html,找到dashboard.flowV1({app: entry.app})修改為dashboard.flow({app: entry.app})。
- 打開
,把src/main/webapp/resources/app/scripts/controllers/identity.js
改為FlowServiceV1
FlowServiceV2
到這裡限流規則持久化到nacos就改造完了,其他幾種規則差别不大,對應的controller需要單獨修改
使用方法:修改sentinel-dashboard子產品下的application.properties檔案中Nacos相關配置為自己的nacos配置 前往源碼根目錄執行mvn package -DskipTests打包。 運作sentinel-dashboard/target檔案夾下的jar包即可。
jar包可以找我要,哈哈哈
項目如何接入sentinel-dashboard
- 啟動sentinel-dashboard的jar包,端口已經修改為9000
- 項目pom中添加如下依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.1.4.RELEASE</version> </dependency> <!--sentinel規則持久化到nacos--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <version>1.8.0</version> </dependency>
getway網關加入到sentinel需要多加配置
pom中加 <!--sentinel整合gatway--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId> <version>2.1.4.RELEASE</version> </dependency> application.yml中加 gateway: metrics: enabled: true
- bootstrap.yml中添加配置
spring: cloud: nacos: config: server-addr: ip:8848 # nacos服務位址
- application.yml中添加配置
spring: cloud: sentinel: transport: port: 9025 #用戶端端口 dashboard: localhost:9000 #控制台位址 client-ip: localhost #本服務的ip namespace: 32263b9a-140f-4121-11e57bf63b25 datasource: #持久化規則到nacos flow: nacos: server-addr: xxx:xxx username: nacos password: nacos namespace: 32263b9a-140f-4121-baf1-11e57bf63b25 groupId: SENTINEL_GROUP dataId: ${spring.application.name}-flow-rules rule-type: flow degrade: nacos: server-addr: xxx:xxx username: nacos password: nacos namespace: 32263b9a-140f-4121-baf1-11e57bf63b25 groupId: SENTINEL_GROUP dataId: ${spring.application.name}-degrade-rules rule-type: degrade param-flow: nacos: server-addr: xxx:xxx username: nacos password: nacos namespace: 32263b9a-140f-4121-baf1-11e57bf63b25 groupId: SENTINEL_GROUP dataId: ${spring.application.name}-param-rules rule-type: param-flow system: nacos: server-addr: xxx:xxx username: nacos password: nacos namespace: 32263b9a-140f-4121-baf1-11e57bf63b25 groupId: SENTINEL_GROUP dataId: ${spring.application.name}-system-rules rule-type: system authority: nacos: server-addr: xxx:xxx username: nacos password: nacos namespace: 32263b9a-140f-4121-baf1-11e57bf63b25 groupId: SENTINEL_GROUP dataId: ${spring.application.name}-authority-rules rule-type: authority eager: true #開啟心跳,服務啟動不需請求就可以注冊到sentinel中 management: health: redis: enabled: false endpoints: web: exposure: include: "*" endpoint: health: show-details: always
五種規則都持久化到nacos中了,nacos和sentinel任意一方對規則的修改都能夠互相影響了,重新開機服務會從nacos拉取之間的規則配置,持久化成功
通路sentinel的首頁,我将sentinel-dashboard源碼裡面的端口改為了9000了
http://localhost:9000/#/login
還可以做的優化項
- sentinel監控資訊的持久化還沒做,需要持久化到資料庫(最好是時序資料庫influxdb),用grafana做展示,由于項目暫時沒有必要就沒做