最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解用Spring Boot Admin來監(jiān)控我們的微服務(wù)

 更新時間:2020年08月17日 08:33:27   作者:后端老鳥  
這篇文章主要介紹了用Spring Boot Admin來監(jiān)控我們的微服務(wù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.概述

Spring Boot Admin是一個Web應用程序,用于管理和監(jiān)視Spring Boot應用程序。每個應用程序都被視為客戶端,并注冊到管理服務(wù)器。底層能力是由Spring Boot Actuator端點提供的。

在本文中,我們將介紹配置Spring Boot Admin服務(wù)器的步驟以及應用程序如何集成客戶端。

2.管理服務(wù)器配置

由于Spring Boot Admin Server可以作為servlet或webflux應用程序運行,根據(jù)需要,選擇一種并添加相應的Spring Boot Starter。在此示例中,我們使用Servlet Web Starter。

首先,創(chuàng)建一個簡單的Spring Boot Web應用程序,并添加以下Maven依賴項:

<dependency>
  <groupId>de.codecentric</groupId>
  <artifactId>spring-boot-admin-starter-server</artifactId>
  <version>2.2.3</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

之后,@ EnableAdminServer將可用,因此我們將其添加到主類中,如下例所示:

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

至此,服務(wù)端就配置完了。

3.設(shè)置客戶端

要在Spring Boot Admin Server服務(wù)器上注冊應用程序,可以包括Spring Boot Admin客戶端或使用Spring Cloud Discovery(例如Eureka,Consul等)。

下面的例子使用Spring Boot Admin客戶端進行注冊,為了保護端點,還需要添加spring-boot-starter-security,添加以下Maven依賴項:

<dependency>
  <groupId>de.codecentric</groupId>
  <artifactId>spring-boot-admin-starter-client</artifactId>
  <version>2.2.3</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

接下來,我們需要配置客戶端說明管理服務(wù)器的URL。為此,只需添加以下屬性:

spring.boot.admin.client.url=http://localhost:8080

從Spring Boot 2開始,默認情況下不公開運行狀況和信息以外的端點,對于生產(chǎn)環(huán)境,應該仔細選擇要公開的端點。

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

使執(zhí)行器端點可訪問:

@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().permitAll() 
      .and().csrf().disable();
  }
}

為了簡潔起見,暫時禁用安全性。

如果項目中已經(jīng)使用了Spring Cloud Discovery,則不需要Spring Boot Admin客戶端。只需將DiscoveryClient添加到Spring Boot Admin Server,其余的自動配置完成。

下面使用Eureka做例子,但也支持其他Spring Cloud Discovery方案。

將spring-cloud-starter-eureka添加到依賴中:

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

通過添加@EnableDiscoveryClient到配置中來啟用發(fā)現(xiàn)

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootAdminApplication.class, args);
  }

  @Configuration
  public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests().anyRequest().permitAll() 
        .and().csrf().disable();
    }
  }
}

配置Eureka客戶端:

eureka:  
 instance:
  leaseRenewalIntervalInSeconds: 10
  health-check-url-path: /actuator/health
  metadata-map:
   startup: ${random.int}  #需要在重啟后觸發(fā)信息和端點更新
 client:
  registryFetchIntervalSeconds: 5
  serviceUrl:
   defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

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

4.安全配置

Spring Boot Admin服務(wù)器可以訪問應用程序的敏感端點,因此建議為admin 服務(wù)和客戶端應用程序添加一些安全配置。
 由于有多種方法可以解決分布式Web應用程序中的身份驗證和授權(quán),因此Spring Boot Admin不會提供默認方法。默認情況下spring-boot-admin-server-ui提供登錄頁面和注銷按鈕。

服務(wù)器的Spring Security配置如下所示:

@Configuration(proxyBeanMethods = false)
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

 private final AdminServerProperties adminServer;

 public SecuritySecureConfig(AdminServerProperties adminServer) {
  this.adminServer = adminServer;
 }

 @Override
 protected void configure(HttpSecurity http) throws Exception {
  SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  successHandler.setTargetUrlParameter("redirectTo");
  successHandler.setDefaultTargetUrl(this.adminServer.path("/"));

  http.authorizeRequests(
    (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll() 
 // 授予對所有靜態(tài)資產(chǎn)和登錄頁面的公共訪問權(quán)限  
     .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() //其他所有請求都必須經(jīng)過驗證
  ).formLogin(
    (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() //配置登錄和注銷
  ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()) //啟用HTTP基本支持,這是Spring Boot Admin Client注冊所必需的
    .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) //使用Cookies啟用CSRF保護
      .ignoringRequestMatchers(
        new AntPathRequestMatcher(this.adminServer.path("/instances"),
          HttpMethod.POST.toString()), 
        new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
          HttpMethod.DELETE.toString()), //禁用Spring Boot Admin Client用于(注銷)注冊的端點的CSRF-Protection
        new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) 
      )) //對執(zhí)行器端點禁用CSRF-Protection。
    .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
 }

 
 @Override
 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  auth.inMemoryAuthentication().withUser("user").password("{noop}password").roles("USER");
 }

}

添加之后,客戶端無法再向服務(wù)器注冊。為了向服務(wù)器注冊客戶端,必須在客戶端的屬性文件中添加更多配置:

spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin

當使用HTTP Basic身份驗證保護執(zhí)行器端點時,Spring Boot Admin Server需要憑據(jù)才能訪問它們。可以在注冊應用程序時在元數(shù)據(jù)中提交憑據(jù)。在BasicAuthHttpHeaderProvider隨后使用該元數(shù)據(jù)添加Authorization頭信息來訪問應用程序的執(zhí)行端點。也可以提供自己的屬性HttpHeadersProvider來更改行為(例如添加一些解密)或添加額外的請求頭信息。

使用Spring Boot Admin客戶端提交憑據(jù):

spring.boot.admin.client:
  url: http://localhost:8080
  instance:
   metadata:
    user.name: ${spring.security.user.name}
    user.password: ${spring.security.user.password}

使用Eureka提交憑據(jù):

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

5.日志文件查看器

默認情況下,日志文件無法通過執(zhí)行器端點訪問,因此在Spring Boot Admin中不可見。為了啟用日志文件執(zhí)行器端點,需要通過設(shè)置logging.file.path或?qū)pring Boot配置為寫入日志文件 logging.file.name。

Spring Boot Admin將檢測所有看起來像URL的內(nèi)容,并將其呈現(xiàn)為超鏈接。
 還支持ANSI顏色轉(zhuǎn)義。因為Spring Boot的默認格式不使用顏色,可以設(shè)置一個自定義日志格式支持顏色。

logging.file.name=/var/log/sample-boot-application.log 
logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx

6. 通知事項

郵件通知

郵件通知將作為使用Thymeleaf模板呈現(xiàn)的HTML電子郵件進行傳遞。要啟用郵件通知,請配置JavaMailSender使用spring-boot-starter-mail并設(shè)置收件人。

將spring-boot-starter-mail添加到依賴項中:

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

配置一個JavaMailSender

spring.mail.username=smtp_user
spring.mail.password=smtp_password
spring.boot.admin.notify.mail.to=admin@example.com

無論何時注冊客戶端將其狀態(tài)從“ UP”更改為“ OFFLINE”,都會將電子郵件發(fā)送到上面配置的地址。

自定義通知程序

可以通過添加實現(xiàn)Notifier接口的Spring Bean來添加自己的通知程序,最好通過擴展 AbstractEventNotifier或AbstractStatusChangeNotifier來實現(xiàn)。

public class CustomNotifier extends AbstractEventNotifier {

 private static final Logger LOGGER = LoggerFactory.getLogger(LoggingNotifier.class);

 public CustomNotifier(InstanceRepository repository) {
  super(repository);
 }

 @Override
 protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
  return Mono.fromRunnable(() -> {
   if (event instanceof InstanceStatusChangedEvent) {
    LOGGER.info("Instance {} ({}) is {}", instance.getRegistration().getName(), event.getInstance(),
      ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
   }
   else {
    LOGGER.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(),
      event.getType());
   }
  });
 }

}

其他的一些配置參數(shù)和屬性可以通過官方文檔來了解。

到此這篇關(guān)于詳解用Spring Boot Admin來監(jiān)控我們的微服務(wù)的文章就介紹到這了,更多相關(guān)Spring Boot Admin監(jiān)控微服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot項目為何引入大量的starter?如何自定義starter?

    SpringBoot項目為何引入大量的starter?如何自定義starter?

    這篇文章主要介紹了SpringBoot項目為何引入大量的starter?如何自定義starter?文章基于這兩個問題展開全文,需要的小伙伴可以參考一下
    2022-04-04
  • 在IDEA中如何設(shè)置最多顯示文件標簽個數(shù)

    在IDEA中如何設(shè)置最多顯示文件標簽個數(shù)

    在使用IDEA進行編程時,可能會同時打開多個文件,當文件過多時,文件標簽會占據(jù)大部分的IDEA界面,影響我們的編程效率,因此,我們可以通過設(shè)置IDEA的文件標簽顯示個數(shù),來優(yōu)化我們的編程環(huán)境,具體的設(shè)置方法如下
    2024-10-10
  • Java實現(xiàn)郵件發(fā)送的過程及代碼詳解

    Java實現(xiàn)郵件發(fā)送的過程及代碼詳解

    這篇文章主要介紹了Java實現(xiàn)郵件發(fā)送的過程及代碼詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • java 中迭代器的使用方法詳解

    java 中迭代器的使用方法詳解

    這篇文章主要介紹了java 中迭代器的使用方法詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • JAVA Static關(guān)鍵字的用法

    JAVA Static關(guān)鍵字的用法

    這篇文章主要介紹了JAVA Static關(guān)鍵字的用法,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • Java實現(xiàn)簡單的RPC框架的示例代碼

    Java實現(xiàn)簡單的RPC框架的示例代碼

    本篇文章主要介紹了Java實現(xiàn)簡單的RPC框架的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • java開發(fā)微信分享到朋友圈功能

    java開發(fā)微信分享到朋友圈功能

    這篇文章主要為大家詳細介紹了java開發(fā)微信發(fā)送給朋友和分享到朋友圈功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • java開發(fā)Activiti進階篇流程實例詳解

    java開發(fā)Activiti進階篇流程實例詳解

    這篇文章主要為大家介紹了java開發(fā)Activiti進階篇流程實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解一)

    使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解一)

    JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,采用完全獨立于語言的文本格式,是理想的數(shù)據(jù)交換格式。接下來通過本文給大家介紹使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法,需要的朋友參考下吧
    2016-03-03
  • Spring整合多數(shù)據(jù)源實現(xiàn)動態(tài)切換的實例講解

    Spring整合多數(shù)據(jù)源實現(xiàn)動態(tài)切換的實例講解

    下面小編就為大家?guī)硪黄猄pring整合多數(shù)據(jù)源實現(xiàn)動態(tài)切換的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07

最新評論

大宁县| 永安市| 桓台县| 阳信县| 襄垣县| 绥宁县| 内丘县| 渝中区| 江达县| 枣强县| 灵武市| 清水河县| 全南县| 嫩江县| 新源县| 林芝县| 桃园县| 南江县| 元谋县| 全州县| 吴旗县| 沾益县| 乐都县| 临湘市| 山阴县| 蓬安县| 鄂伦春自治旗| 家居| 温宿县| 正阳县| 霸州市| 威远县| 兴城市| 都匀市| 柳河县| 遂宁市| 杭锦旗| 德州市| 花莲市| 阿城市| 柞水县|