简介

Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul)发现。 UI是的AngularJs应用程序,展示Spring Boot Admin Client的Actuator端点上的一些监控

常见功能

  • 显示健康状况
  • 显示详细信息,例如
  • JVM和内存指标
  • micrometer.io指标
  • 数据源指标
  • 缓存指标
  • 显示构建信息编号
  • 关注并下载日志文件
  • 查看jvm系统和环境属性
  • 查看Spring Boot配置属性
  • 支持Spring Cloud的postable / env-和/ refresh-endpoint
  • 轻松的日志级管理
  • 与JMX-beans交互
  • 查看线程转储
  • 查看http跟踪
  • 查看auditevents
  • 查看http-endpoints
  • 查看计划任务
  • 查看和删除活动会话(使用spring-session)
  • 查看Flyway / Liquibase数据库迁移
  • 下载heapdump
  • 状态变更通知(通过电子邮件,Slack,Hipchat,…)
  • 状态更改的事件日志(非持久性)

SpringBootAdmin直连配置

创建SpringBootAdminServer工程

添加依赖

<properties>
    <java.version>1.8</java.version>
    <spring-boot-admin.version>2.1.1</spring-boot-admin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-dependencies</artifactId>
            <version>${spring-boot-admin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

添加配置项 application.properties

spring.application.name=admin-server
server.port=8769

添加启动类注解

@SpringBootApplication
@EnableAdminServer

创建SpringBootAdminClient工程

添加依赖

<properties>
    <java.version>1.8</java.version>
    <spring-boot-admin.version>2.1.1</spring-boot-admin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-dependencies</artifactId>
            <version>${spring-boot-admin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

添加配置项 application.properties

spring.application.name=admin-client
server.port=8768
spring.boot.admin.client.url=http://localhost:8769
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

启动工程

同时启动两个工程,访问地址 http://localhost:8769/

SpringBootAdmin使用

点击Wallboard查看服务详情

SpringBootAdmin使用

SpringBootAdmin结合Eureka使用

创建eurekaServer工程

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

添加配置项,application.properties

eureka.client.service-url.defualtZone=http://localhost:8761/eureka/
server.port=8761
spring.application.name=eurekaServer
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

添加启动类注解

@EnableEurekaClient
@EnableEurekaServer
@SpringBootApplication

创建adminServer工程

在上面工程的基础上增加eureka的配置

添加eureka依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

添加配置项,application.properties

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
eureka.client.service-url.defultZone=http://localhost:8761/eureka/
eureka.instance.health-check-url-path=/actuator/health

创建adminClient工程

添加依赖

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.RC1</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>

添加配置项,application.properties

server.port=8080
spring.application.name=admin-client
eureka.client.service-url.defultZone=http://localhost:8761/eureka/
eureka.instance.health-check-url-path=/actuator/health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

启动工程

同时启动三个工程,访问地址 http://localhost:8769/

集成spring security

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

添加配置项,application.properties

spring.security.user.name=admin
spring.security.user.password=admin

eureka.instance.metadata-map.user.name=${spring.security.user.name}
eureka.instance.metadata-map.user.password=${spring.security.user.password}

添加配置类

@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 {
//        super.configure(http);
        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:8769/
会被重定向到登录界面,登录的用户名和密码为配置文件中配置的,分别为admin和admin,界面显示如下:

SpringBootAdmin使用

集成邮箱报警功能

在spring boot admin中,也可以集成邮箱报警功能,比如服务不健康了、下线了,都可以给指定邮箱发送邮件。集成非常简单,只需要改造下admin-server即可:

在admin-server工程中添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

添加配置项

spring.mail.host=smtp.163.com
spring.mail.username=ops
spring.mail.password=ops
spring.boot.admin.notify.mail.to=[email protected]

做完以上配置后,当我们已注册的客户端的状态从 UP 变为 OFFLINE 或其他状态,服务端就会自动将电子邮件发送到上面配置的地址。

参考

相关文章:

  • 2022-12-23
  • 2021-07-11
  • 2022-02-15
  • 2023-02-10
  • 2021-10-06
  • 2021-10-03
  • 2021-07-03
猜你喜欢
  • 2022-01-06
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-01-14
相关资源
相似解决方案