spring-boot-admin 监控
监控单体应用
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Admin Server 端
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
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 端
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
配置文件
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@"
参考链接:
Security安全控制
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
配置用户名密码
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();
}
}
但是此时,我们看项目启动(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,相信不难实现,此处不再赘述。
监控微服务
后续更新,关注本帖
参考链接:
异常
在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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 替换为 jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>