监控单体应用
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
Admin Server 端
de.codecentric
spring-boot-admin-starter-server
2.2.1
org.springframework.boot
spring-boot-starter-web
2.2.0以后支持中文界面
配置文件
server.port=8000
server.servlet.context-path=/monitor
spring.application.name=Dimples-Monitor-Admin
启动类
@Slf4j
@SpringBootApplication
@EnableAdminServer
public class ActuatorApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorApplication.class, args);
log.info("监控中心启动");
}
}
访问
启动服务端,浏览器访问http://localhost:8000/monitor可以看到以下界面
Admin Client 端
de.codecentric
spring-boot-admin-starter-client
2.2.1
org.springframework.boot
spring-boot-starter-web
配置文件
server.port=8000
spring.application.name=Admin Client
spring.boot.admin.client.url=http://localhost:8000/monitor
management.endpoints.web.exposure.include=*
- spring.boot.admin.client.url 配置 Admin Server 的地址
- management.endpoints.web.exposure.include=* 打开客户端 Actuator 的监控
启动
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
配置额外信息展示
info.app.name="@project.name@"
info.app.description="@project.description@"
info.app.version="@project.version@"
info.app.spring-boot-version="@project.parent.version@"
参考链接:
https://mrbird.cc/Spring-Boot-Admin.html
Security安全控制
org.springframework.boot
spring-boot-starter-security
配置用户名密码
spring.security.user.name=dimples
spring.security.user.password=123456
此时访问 http://localhost:8000/monitor会先跳到security的登陆验证界面,然后登陆成功后再跳到Admin的界面
我们继续配置,使用Admin的登陆界面,新建配置类,配置免认证路径,比如/assets/**静态资源和/login登录页面;配置了登录页面为/login,登出页面为/logout
@EnableWebSecurity
public class DimplesSecurityConfigure extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public DimplesSecurityConfigure(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
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://localhost:8000/monitor
但是此时,我们看项目启动(server,client一体时)的控制台会发现,我们的client不能注册到server,是因为我们开启了security,所以需要额外的配置
在Admin中是通过spring-boot-starter-actuator提供的/actuator/监控接口来实现的,所以我们需要将/actuator/资源纳入到免认证路径中,同时配置client的用户名密码
在上方的DimplesSecurityConfigure 类中新增一行配置,开启免认证路径
http.authorizeRequests().antMatchers("/actuator/**").permitAll()
配置用户名密码
# 指定服务端的用户名
spring.boot.admin.client.username=dimples
# 制定服务端的地址
spring.boot.admin.client.password= 123456
完整的配置如下图
补充
在上方的配置中,我们是将server和client放在同一个应用中的,这样对于单个应用来说只需要部署一个jar包,方便管理。
但是有时我们需要用一个server去监控多个client,此时建议使用注册中心,server自动去注册中心拉取client。当然你也可以将上方的项目拆分成server和client,相信不难实现,此处不再赘述。
监控微服务
后续更新,关注本帖
参考链接:
https://www.kancloud.cn/mrbird/spring-cloud/1263712
异常
在monitor-admin控制台中,经常会抛出如下异常:
java.lang.IllegalStateException: Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]
at org.apache.coyote.AsyncStateMachine.asyncError(AsyncStateMachine.java:440) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:512) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.coyote.Request.action(Request.java:430) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.core.AsyncContextImpl.setErrorState(AsyncContextImpl.java:401) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:239) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.coyote.AbstractProcessor.dispatch(AbstractProcessor.java:241) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) [tomcat-embed-core-9.0.21.jar:9.0.21]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.21.jar:9.0.21]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_211]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.21.jar:9.0.21]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]
该异常不影响程序正常运行,如果要解决这个异常,可以将Tomcat容器替换为Jetty,修改monitor-admim的pom:
dependency>
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-jetty