SpringCloud-监控管理

SpringCloud文章系列

  1. SpringCloud
  2. SpringCloud-注册中心
  3. SpringCloud-配置中心
  4. SpringCloud-链路跟踪
  5. SpringCloud-消息总线
  6. SpringCloud-API网关
  7. SpringCloud-异步消息
  8. SpringCloud-同步调用
  9. SpringCloud-断路降级
  10. 【当前文章】SpringCloud-监控管理
  11. SpringCloud-番外篇-临时任务
  12. SpringCloud-番外篇-文档生成
  13. SpringCloud-番外篇-源码解析

接入说明

  1. 目前采用最新的1.xRelease版本:1.5.15.RELEASE
  2. SpringCloud项目可以在start.spring.io下载,不过更方便的应该是通过idea新建项目,建立Spring Initializr项目
  3. 我一般习惯将具体实现服务用具体的服务名,而公共组件用service-xxx来命名,当然还有一些比较固定名字的公共组件

Admin监控

1. 新建项目

新建artifactId为service-admin的服务

2. 导入依赖

依赖说明

  1. actuator用于暴露监控的接口
  2. eureka-server是Eureka服务器的依赖
  3. config是Config客户端,从配置中心拉取配置
  4. admin-starter-server是admin监控平台的依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>

3. 编写启动类

打开ServiceAdminApplication,在class上加入@EnableAdminServer注解

4. 修改配置

1
2
3
4
5
6
7
server.port=7088
spring.application.name=service-admin

# public
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config

5. 启动main

6. 验证

访问 http://localhost:8761/ 看到service-admin已经注册了一台
访问 http://localhost:7088 可以看到admin的列表,首页就是与注册中心信息差不多,全部的应用,还有detail可以点进去看到各个endpoint数据

客户端使用

admin其实是基于SpringBoot诞生的,在注册中心引入之前本来也是有一个admin的client的,用于发现服务维持心跳的
但是基于SpringCloud的注册中心就不需要在客户端依赖了

扩展

集成Hystrix大盘

之前介绍过断路降级的hystrix大盘,其实可以集成在admin中更加方便统一
导入依赖

1
2
3
4
5
6
7
8
9
10
 <dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-hystrix</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-turbine</artifactId>
<version>1.5.4</version>
</dependency>

配置文件中引入

1
2
spring.boot.admin.routes.endpoints=env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream,turbine.stream,activiti
spring.boot.admin.turbine.location=turbine-service

这样打开页面时在每个服务的detail中可以看到hystrix的信息,同时在大菜单上加入了Tuibine的视图

同时有个方便的方式是将turbine服务直接在admin中去集成,不用单独开turbine服务

集成登录验证

简单的方式引入spring-boot-starter-security即可用basic验证了
不过admin也有一个登录界面
引入如下

1
2
3
4
5
6
7
8
9
  <dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

然后建立登录认证的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity web) throws Exception {
//忽略css.jq.img等文件
web.ignoring()
.antMatchers("/**.html", "/**.css", "/img/**", "/**.js", "/third-party/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {

http
.csrf().disable() //HTTP with Disable CSRF
.authorizeRequests() //Authorize Request Configuration
.antMatchers("/login",
"/api/**",
"/**/heapdump",
"/**/loggers",
"/**/liquibase",
"/**/logfile",
"/**/flyway",
"/**/auditevents",
"/**/jolokia").permitAll() //放开"/api/**":为了给被监控端免登录注册并解决Log与Logger冲突
.and()
.authorizeRequests()
.antMatchers("/**").hasRole("USER")
.antMatchers("/**").authenticated()
.and() //Login Form configuration for all others
.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login").permitAll()
.defaultSuccessUrl("/")
.and() //Logout Form configuration
.logout()
.deleteCookies("remove")
.logoutSuccessUrl("/login.html").permitAll()
.and()
.httpBasic();

}
}

配置文件中引入

1
2
3
4
# 权限
security.user.name=admin
security.user.password=123
security.basic.enabled=false

这样在访问监控时会先要求登录 使用admin与123访问

集成监控报警

可以在应用状态变更与endpoin异常时自动邮件报警
导入

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

配置中需要开启通知与邮件的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring.boot.admin.notify.mail.to=456@qq.com
spring.boot.admin.notify.mail.from=123@qq.com
#从任何状态到up状态都不要发邮件通知
#spring.boot.admin.notify.mail.ignore-changes: *:UP
# 邮件
spring.mail.host=smtp.qq.com
spring.mail.username=123@qq.com
spring.mail.password=xxxxx
spring.mail.properties.mail.debug=false
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.port=465

上面配置是开启SSL的邮件配置

------ 本文结束 ------

版权声明

dawell's Notes by Dawell is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Dawell创作并维护的dawell's Notes博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于dawell's Notes 博客( http://dawell.cc ),版权所有,侵权必究。

坚持原创技术分享,您的支持将鼓励我继续创作!