springboot 版本2.1.6,对项目运行进行监控。
服务端
相关依赖
<dependencies>
<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>
<!-- 排除掉tomcat原因会出现 java.lang.IllegalStateException: Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]
异常情况,解决方式更换成 spring-boot-starter-undertow容器
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
启动类
添加注解 @EnableAdminServer
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
SecuritySecureConfig
新建SecuritySecureConfig配置服务端安全登录。
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
/**
* @author felix
* @ 日期 2019-07-31 11:57
*/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(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.port=8000
spring.security.user.name=admin
spring.security.user.password=admin
客户端
客户端使用较为简单引入
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
在application.properties
# 监控
spring.application.name=dtpki-service
spring.boot.admin.client.url=http://192.168.1.254:8000
#用户名密码是与服务端一致,否则不能正常监控出现401
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
#监控所有
management.endpoints.web.exposure.include=*
## 排除部分
management.endpoint.heapdump.enabled=false
management.endpoint.beans.enabled=false
management.endpoint.caches.enabled=false
management.endpoint.mappings.enabled=false
management.endpoints.jmx.exposure.include=*
management.endpoint.scheduledtasks.enabled=false
management.endpoint.shutdown.enabled=true
management.endpoint.threaddump.enabled=false
management.endpoint.loggers.enabled=false
management.endpoint.env.enabled=false
运行
同时启动客户端服务端
http://localhost:8000
打开浏览器输入用户名密码
暴露部分端点
management.endpoints.web.exposure.include=info,health,beans,env
暴露所有端点
management.endpoints.web.exposure.include=*
不暴露beans端点
management.endpoints.web.exposure.exclude=beans
Actuator 管理端口
management.server.port=8000
#暴露 有端
management.endpoints.web.exposure.include =*
#默认情况下 有端点都不启用,此时需要按需启用端点
management.endpoints.enabled-by-default=false
#启用端点 info
management.endpoint.info.enabled=true
#启用端点 beans
management.endpoint.beans.enabled=true
#启用端点 configprops
management.endpoint.configprops.enabled=true
#启用端点 env
management.endpont.env.enabled=true
#启用端点 health
management.endpoint.health.enabled=true
#启用端点 mappings
management.endpont.mappings.enabed=true
#启用端点 shutdown
management.endpoint.shutdown.enabled=true
结束
会出现的异常
java.lang.IllegalStateException: Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]
解决方式就是更换tomcat,使用jetty,或者undertow