背景
微服务多了,需要监控和管理微服务。
功能
显示健康状况
显示详细信息,例如
JVM和内存指标
micrometer.io指标
数据源指标
缓存指标
显示构建信息编号
关注并下载日志文件
查看jvm system-和environment-properties
查看Spring Boot配置属性
支持Spring Cloud的postable / env-和/ refresh-endpoint
轻松的日志级管理
与JMX-beans交互
查看线程转储
查看http-traces
查看auditevents
查看http-endpoints
查看计划任务
查看和删除活动会话(使用spring-session)
查看Flyway / Liquibase数据库迁移
下载heapdump
状态变更通知(通过电子邮件,Slack,Hipchat,......)
状态更改的事件日志(非持久性)
实战
服务端
1 pom 配置
1.82.1.2.RELEASEGreenwich.RELEASE2.1.5org.springframework.boot spring-boot-starter com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery de.codecentric spring-boot-admin-starter-server ${spring.boot.admin.version}org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test testorg.springframework.cloud spring-cloud-dependencies ${spring.cloud.version}pomimportcom.alibaba.cloud spring-cloud-alibaba-dependencies ${spring.cloud.alibaba.version}pomimport
2 application.yml 配置
server: port: 8095spring: application: name: admin-server cloud: nacos: discovery: server-addr: 127.0.0.1:8848
3 启动类注解
@SpringBootApplication@EnableAdminServer # 关键public class SpringBootAdminServerExampleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminServerExampleApplication.class, args); }}
客户端
- 1 pom 配置
1.82.1.2.RELEASEGreenwich.RELEASE2.1.5org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery de.codecentric spring-boot-admin-starter-client ${spring.boot.admin.version}org.springframework.boot spring-boot-starter-test testorg.springframework.cloud spring-cloud-dependencies ${spring.cloud.version}pomimportcom.alibaba.cloud spring-cloud-alibaba-dependencies ${spring.cloud.alibaba.version}pomimport
- 2 application.yml 配置
server: port: 8096spring: application: name: admin-client cloud: nacos: discovery: server-addr: 127.0.0.1:8848 boot: admin: client: # admin 服务端的地址 url: http://localhost:8095management: endpoints: web: exposure: include: '*' endpoint: health: # 展示详情 show-details: always
启动程序
分别启动程序服务端(8095)和客户端(8096),访问地址:http://localhost:8095 ,出现如下界面。
进入客户端admin-client,在菜单栏 Details 有线程、内存等显示
服务端整合 security
依赖
需要在 admin-server 工程的文件引入以下的依赖:
org.springframework.boot spring-boot-starter-security
配置
在 application.yml中配置 security 账号和密码
security: user: name: admin password: admin
配置类
权限配置类修改
@Configurationpublic class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties adminServerProperties) { this.adminContextPath = adminServerProperties.getContextPath(); } @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter( "redirectTo" ); http.authorizeRequests() // 配置签名后放行路径 .antMatchers( adminContextPath + "/assets/**" ).permitAll() .antMatchers( adminContextPath + "/login" ).permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and() .logout().logoutUrl( adminContextPath + "/logout" ).and() .httpBasic().and() .csrf().disable(); }}
重启测试
请求 http://192.168.2.105:8095,便会来到登录界面。