900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Spring Cloud整合 Spring Boot Admin

Spring Cloud整合 Spring Boot Admin

时间:2021-10-25 22:57:43

相关推荐

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:

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

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

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /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:

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

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

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /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@SpringBootApplicationpublic class FebsMonitorAdminApplication {public static void main(String[] args) {SpringApplication.run(FebsMonitorAdminApplication.class, args);}}

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

server:port: 8401spring:application:name: FEBS-Monitor-Adminboot:admin:ui:title: ${spring.application.name}

Web Security配置类FebsSecurityConfigure

@EnableWebSecuritypublic class FebsSecurityConfigure extends WebSecurityConfigurerAdapter {private final String adminContextPath;public FebsSecurityConfigure(AdminServerProperties adminServerProperties) {this.adminContextPath = adminServerProperties.getContextPath();}@Overrideprotected 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:

因为还没有搭建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:8401username: febspassword: 123456info: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

@EnableWebSecuritypublic class FebsRegisterWebSecurityConfigure extends WebSecurityConfigurerAdapter {@Overrideprotected 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界面:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。