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、付费专栏及课程。

余额充值