日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Spring Boot Admin 2.0 上手

發(fā)布時(shí)間:2025/3/21 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot Admin 2.0 上手 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Spring Boot Admin 在 Spring Boot Actuator 的基礎(chǔ)上提供簡(jiǎn)潔的可視化 WEB UI,是用來(lái)管理 Spring Boot 應(yīng)用程序的一個(gè)簡(jiǎn)單的界面,提供如下功能:

  • 顯示 name/id 和版本號(hào)
  • 顯示在線狀態(tài)
  • Logging 日志級(jí)別管理
  • JMX beans 管理
  • Threads 會(huì)話和線程管理
  • Trace 應(yīng)用請(qǐng)求跟蹤
  • 應(yīng)用運(yùn)行參數(shù)信息,如:
    • Java 系統(tǒng)屬性
    • Java 環(huán)境變量屬性
    • 內(nèi)存信息
    • Spring 環(huán)境屬性

在本文中,我們將介紹配置 Spring Boot Admin(以下簡(jiǎn)稱 SBA)服務(wù)端的步驟以及如何將一個(gè) Spring Boot 應(yīng)用注冊(cè)為它的客戶端。

快速上手

首先我們需要?jiǎng)?chuàng)建一個(gè) SBA 的服務(wù)端。

服務(wù)端

創(chuàng)建一個(gè)簡(jiǎn)單的 Spring Boot Web 應(yīng)用程序并添加以下依賴

<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.0.1</version> </dependency> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>

然后我們就可以將?@EnableAdminServer?添加到我們的主類中

@EnableAdminServer @SpringBootApplication public class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);} }

配置服務(wù)端口等信息

spring:application:name: admin-server server:port: 18080 eureka:client:service-url:defaultZone: ${EUREKA_SERVICE_URL:http://localhost:7000}/eureka/

此時(shí),SBA Server 已經(jīng)好了,直接啟動(dòng)它。

客戶端

在建立了我們的服務(wù)端之后,我們可以將一個(gè) Spring Boot 應(yīng)用程序注冊(cè)為客戶端。
注冊(cè)客戶端有兩種方式,一種就是通過(guò)引入 SBA Client,另外一種是基于 Spring Cloud Discovery。

我們這里先介紹通過(guò)引入 SBA Client 的方式。

SBA Client

首先要引入以下依賴:

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

然后在配置文件 application.yml 中配置以下信息

spring:application:name: userboot:admin:client:url: http://localhost:18080 eureka:instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: /actuator/healthclient:registryFetchIntervalSeconds: 5service-url:defaultZone: ${EUREKA_SERVICE_URL:http://localhost:7000}/eureka/ management:endpoints:web:exposure:include: "*"endpoint:health:show-details: ALWAYS

現(xiàn)在客戶端也就配置好了,啟動(dòng)客戶端再次訪問(wèn)?http://localhost:18080?就能看到以下界面(啟動(dòng)了多個(gè)客戶端)

Wallboard:

Applications:

Journal:

Instance details:

Spring Cloud Discovery

如果我們的項(xiàng)目中使用了 Spring Cloud,那么我們其實(shí)并不用通過(guò) SBA Client 來(lái)向 SBA 注冊(cè),而是讓 SBA 通過(guò)注冊(cè)中心(Eureka、Consul 等)來(lái)發(fā)現(xiàn)服務(wù)。

這里以 Eureka 作為注冊(cè)中心來(lái)說(shuō)明。我們首先向 SBA 服務(wù)端以及客戶端中添加 Eureka Client 的依賴

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

然后照常在 application.yml 中配置 Eureka 的注冊(cè)信息,如下

eureka:instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: /actuator/healthclient:registryFetchIntervalSeconds: 5serviceUrl:defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/ management:endpoints:web:exposure:include: "*" endpoint:health:show-details: ALWAYS

這個(gè)配置就不多做解釋了,不懂的可以看之前關(guān)于 Eureka 的文章。

然后分別啟動(dòng) SBA 服務(wù)端和客戶端,就能看的和使用 SBA Client 一樣的效果了。

安全配置

SBA 服務(wù)端可以訪問(wèn)客戶端的敏感端點(diǎn),因此手冊(cè)上?建議我們應(yīng)該為服務(wù)端和客戶端添加一些安全配置。

首先我們先為服務(wù)端增加安全配置。

服務(wù)端的安全配置

向服務(wù)端添加 Spring Security 依賴

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

之后,我們需要新增一個(gè)安全配置類

@Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {private final String adminContextPath;public SecuritySecureConfig(AdminServerProperties adminServerProperties) {this.adminContextPath = adminServerProperties.getContextPath();}@Overrideprotected void configure(HttpSecurity http) throws Exception {// @formatter:offSavedRequestAwareAuthenticationSuccessHandler 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();// @formatter:on}}

然后通過(guò)配置文件設(shè)置賬號(hào)密碼

spring:security:user:name: "admin"password: "admin"

這樣,一個(gè)簡(jiǎn)單的安全配置就生效了。這時(shí)我們?cè)僭L問(wèn)?http://localhost:18080?就會(huì)發(fā)現(xiàn)需要認(rèn)證了。

如果這時(shí)你的客戶端是使用的 SBA Client 的方式,你會(huì)注意到客戶端這時(shí)已無(wú)法再注冊(cè)到服務(wù)端了(Spring Cloud Discovery 的話不受影響)。為了能將客戶端注冊(cè)到服務(wù)端,我們還必須在客戶端的配置文件中添加以下內(nèi)容:

spring.boot.admin.client:username: "admin"password: "admin"

下面我們來(lái)為客戶端增加安全配置。

客戶端的安全配置

可能你也想到了,因?yàn)?SBA 客戶端的注冊(cè)方式有兩種,所以在客戶端的安全配置上也是分為了兩種。

SBA Client

首先在客戶端的配置文件中新增以下內(nèi)容

spring:application:name: usersecurity:user:name: "client"password: "client"boot:admin:client:url: http://localhost:18080username: "admin"password: "admin"instance:metadata:user.name: ${spring.security.user.name}user.password: ${spring.security.user.password}

然后再在服務(wù)端的配置中增加以下內(nèi)容

eureka:instance:metadata-map:user.name: "client"user.password: "client"

OK,重啟 SBA Server 和 Clients 再試試看吧。

Spring Cloud Discovery

這種方式和使用 SBA Client 的配置文件有些類似,以免產(chǎn)生混淆,我這里直接貼完整的配置了。
(重點(diǎn)關(guān)注?eureka.instance.metadata-map)

SBA 客戶端:

spring:application:name: usersecurity:user:name: "client"password: "client" eureka:instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: /actuator/healthmetadata-map:user.name: ${spring.security.user.name}user.password: ${spring.security.user.password}client:registryFetchIntervalSeconds: 5service-url:defaultZone: ${EUREKA_SERVICE_URL:http://localhost:7000}/eureka/ management:endpoints:web:exposure:include: "*"endpoint:health:show-details: ALWAYS

SBA 服務(wù)端:

spring:application:name: admin-serversecurity:user:name: "admin"password: "admin" server:port: 18080 eureka:client:registryFetchIntervalSeconds: 5service-url:defaultZone: ${EUREKA_SERVICE_URL:http://localhost:7000}/eureka/instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: /actuator/healthmetadata-map:user.name: ${spring.security.user.name}user.password: ${spring.security.user.password}management:endpoints:web:exposure:include: "*"endpoint:health:show-details: ALWAYS

Eureka 的 metadataMap

在進(jìn)行安全配置的時(shí)候,我們會(huì)頻繁接觸 metadataMap,這里就簡(jiǎn)單介紹一下吧。

Eureka 中的 metadataMap 是專門用來(lái)存放一些自定義的數(shù)據(jù),當(dāng)注冊(cè)中心或者其他服務(wù)需要此服務(wù)的某些配置時(shí)可以在 metadataMap 里取。實(shí)際上,每個(gè) instance 都有各自的 metadataMap,map 中存放著需要用到的屬性。例如,上面配置中的?eureka.instance.metadata-map.user.name,當(dāng)這個(gè)服務(wù)成功注冊(cè)到 Eureka 上,Spring Boot Admin 就會(huì)取拿到這個(gè) instance,進(jìn)而拿到 metadataMap 里的屬性,然后放入請(qǐng)求頭,向此服務(wù)發(fā)送請(qǐng)求,訪問(wèn)此服務(wù)的 Actuator 開(kāi)放的端點(diǎn)。

關(guān)于 SBA 的更多認(rèn)證方式可以參見(jiàn)?joshiste/spring-boot-admin-samples

通知

當(dāng)客戶端(已注冊(cè)到服務(wù)端)發(fā)生某些事件的時(shí)候,我們可以接收到通知。目前有以下通知方式可供使用:

  • Email
  • PagerDuty
  • OpsGenie
  • Hipchat
  • Slack
  • Let’s Chat
  • Microsoft Teams
  • Telegram

這里我們主要來(lái)關(guān)注一下郵件通知。郵件通知會(huì)發(fā)送一個(gè)由?Thymeleaf?模板渲染的 HTML 文檔,就像下邊這樣

要使用郵件通知,我們首先需要添加?spring-boot-starter-mail?依賴

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

然后配置 JavaMailSender

spring:mail:host: smtp.example.comusername: smtp_userpassword: smtp_passwordboot:admin:notify:mail:to: admin@example.com

做完以上配置后,只要我們已注冊(cè)的客戶端的狀態(tài)從 UP 變?yōu)?OFFLINE 或其他狀態(tài),服務(wù)端就會(huì)自動(dòng)將電子郵件發(fā)送到上面配置的地址。其他的通知配置也類似。

示例代碼:https://github.com/zhaoyibo/spring-cloud-study/tree/master/admin

參考

Spring Boot Admin Reference Guide
A Guide to Spring Boot Admin

  • 本文作者:?Yibo
  • 本文鏈接:?https://windmt.com/2018/05/22/spring-boot-admin-guide/
  • 版權(quán)聲明:?本博客所有文章除特別聲明外,均采用?CC BY-NC-SA 4.0?許可協(xié)議。轉(zhuǎn)載請(qǐng)注明出處!

總結(jié)

以上是生活随笔為你收集整理的Spring Boot Admin 2.0 上手的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。