Spring Cloud整合 Spring Boot Admin

1.Spring Boot Admin 介绍

Spring Boot Admin通过spring-boot-starter-actuator提供的REST接口实现了图形化的监控界面,包括应用的配置信息、Beans信息、环境属性、线程信息、JVM状况等。

Spring Boot Admin分为服务端和客户端。客户端通过HTTP向服务端提供自身信息,服务端收集这些信息并以图形化界面的方式呈现。

Spring Boot Admin客户端简称为SBA客户端,Spring Boot Admin服务端简称为SBA服务端。

2.SBA服务端

因为后续我们要搭建多个监控模块,所以再搭建SBA服务端之前,我们先搭建一个聚合各监控模块的父模块。

点击IDEA的File -> New -> Module...,模板选择Maven,Module SDK选择1.8:

111.png

 

112.png

113.png

点击"Finish"完成项目创建,至此,项目结构如下图所示:

因为febs-monitor是一个纯聚合模块,所以可以把src下的内容删了,然后调整pom,代码如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>febs-cloud</artifactId>
        <groupId>cc.mrbird</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../febs-cloud/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>febs-monitor</artifactId>
    <packaging>pom</packaging>
    <name>FEBS-Monitor</name>
    <description>FEBS-Monitor监控模块</description>

</project>

接下来开始搭建SBA服务端。

在febs-monitor上右键选择New -> Module...,模板选择Spring initializr,Project SDK选择1.8:

115.png

116.png

117.png

118.png

 点击Finish,完成模块创建,至此,项目结构如下所示:

119.png

调整febs-monitor-admin模块的pom文件,代码如下所示: 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cc.mrbird</groupId>
        <artifactId>febs-monitor</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>febs-monitor-admin</artifactId>
    <name>Febs-Monitor-Admin</name>
    <description>Febs-Monitor-Admin基于Spring Boot Admin搭建的监控程序</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
            <version>2.1.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

在该pom中,我们将刚刚创建的febs-monitor模块作为父模块,并且继续引入了spring-boot-admin-server-ui依赖,该依赖用于图形化展示监控数据。

<modules>
    <module>febs-monitor-admin</module>
</modules>

因为febs-monitor-admin模式是febs-monitor的子模块,所以我们需要在febs-monitor的pom里添加:

在febs-monitor-admin模块的入口类上使用@EnableAdminServer注解标注,开启Spring Boot Admin服务端功能: 

@EnableAdminServer
@SpringBootApplication
public class FebsMonitorAdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(FebsMonitorAdminApplication.class, args);
    }
}

编写febs-monitor-admin的配置文件application.yml:

server:
  port: 8401

spring:
  application:
    name: FEBS-Monitor-Admin
  boot:
    admin:
      ui:
        title: ${spring.application.name}

Web Security配置类FebsSecurityConfigure

@EnableWebSecurity
public class FebsSecurityConfigure extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public FebsSecurityConfigure(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();
    }
}

上面配置了免认证路径,比如/assets/**静态资源和/login登录页面;配置了登录页面为/login,登出页面为/logout

配置好后,启动febs-monitor-admin模块,使用浏览器访问 http://localhost:8401

120.png

121.png

因为还没有搭建SBA客户端,所以监控信息都是空的,下面我们开始构建SBA客户端。

3.SBA客户端

被SBA服务端监控的微服务就是SBA客户端,我们希望febs-auth、febs-gateway、febs-register、febs-server-system和febs-server-test都被纳入监控,所以它们都是SBA客户端。要将这些微服务模块改造为SBA客户端所需的步骤是一样的,所以这里以febs-register为例子演示 

在febs-register模块的pom里添加SBA客户端依赖(其他依赖febs-common模块的微服务可以直接在febs-common的pom里引入):

 <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.1.6</version>
</dependency>

然后在febs-register模块的配置文件application.yml里添加如下配置:

spring:
  boot:
    admin:
      client:
        url: http://localhost:8401
        username: febs
        password: 123456

info:
  app:
    name: ${spring.application.name}
    description: "@project.description@"
    version: "@project.version@"

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS

这些配置的含义如下:

  • spring.boot.admin.client.url指定了SBA服务端地址;
  • spring.boot.admin.client.username对应SBA服务端的用户名;
  • spring.boot.admin.client.password对应SBA服务端的密码;
  • info.**配置了SBA客户端的名称,描述和版本信息;
  • management.endpoints.web.exposure.include='*'表示将SBA客户端的所有监控端点都暴露给SBA服务端;
  • management.endpoint.health.show-details表示总是展示详细的健康信息

Spring Boot Admin是通过spring-boot-starter-actuator提供的/actuator/**监控接口来实现的,而我搭建的微服务都是受Spring Cloud Security保护的,所以需要将/actuator/**资源纳入到免认证路径中 

修改febs-register模块的Web Security配置类FebsRegisterWebSecurityConfigure

@EnableWebSecurity
public class FebsRegisterWebSecurityConfigure extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**")
                .and()
                .authorizeRequests().antMatchers("/actuator/**").permitAll();
        super.configure(http);
    }
}

修改完毕,重启febs-register、febs-monitor-admin服务,重新访问SBA服务端的Web界面:

123.png

124.png

125.png

126.png

  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值