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做展示,由于项目暂时没有必要就没做