springboot-admin2.0监控项目运行情况

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

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值