SpringCloud文章系列
- SpringCloud
- SpringCloud-注册中心
- SpringCloud-配置中心
- SpringCloud-链路跟踪
- SpringCloud-消息总线
- SpringCloud-API网关
- SpringCloud-异步消息
- SpringCloud-同步调用
- SpringCloud-断路降级
- 【当前文章】SpringCloud-监控管理
- SpringCloud-番外篇-临时任务
- SpringCloud-番外篇-文档生成
- SpringCloud-番外篇-源码解析
接入说明
- 目前采用最新的1.xRelease版本:1.5.15.RELEASE
- SpringCloud项目可以在start.spring.io下载,不过更方便的应该是通过idea新建项目,建立Spring Initializr项目
- 我一般习惯将具体实现服务用具体的服务名,而公共组件用service-xxx来命名,当然还有一些比较固定名字的公共组件
Admin监控
1. 新建项目
新建artifactId为service-admin的服务
2. 导入依赖
依赖说明
- actuator用于暴露监控的接口
- eureka-server是Eureka服务器的依赖
- config是Config客户端,从配置中心拉取配置
- admin-starter-server是admin监控平台的依赖
1 | <dependency> |
3. 编写启动类
打开ServiceAdminApplication,在class上加入@EnableAdminServer注解
4. 修改配置
1 | server.port=7088 |
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
2spring.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
true, prePostEnabled = true, proxyTargetClass = true) (securedEnabled =
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
public void configure(WebSecurity web) throws Exception {
//忽略css.jq.img等文件
web.ignoring()
.antMatchers("/**.html", "/**.css", "/img/**", "/**.js", "/third-party/**");
}
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 | spring.boot.admin.notify.mail.to=456@qq.com |
上面配置是开启SSL的邮件配置