小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

SpringBoot整合Actuator

 碼農(nóng)9527 2021-09-08

 SpringBootActuator提供了很多生產(chǎn)級的特性,比如監(jiān)控和度量SpringBoot應(yīng)用程序。Actuator的這些特性可以通過眾多REST接口、遠(yuǎn)程shell和JMX獲得。Actuator也可以和一些外部的應(yīng)用監(jiān)控系統(tǒng)整合(Prometheus,Graphite,DataDog,Influx,Wavefront,NewRelic等),通過一個(gè)統(tǒng)一友好的界面,監(jiān)視和管理你的應(yīng)用程序。

SpringBoot整合Actuator

    開啟監(jiān)控

    引入maven依賴:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>1234復(fù)制代碼類型:[java]

    依賴引入之后可以直接啟動程序并在瀏覽器訪問:

    可以得到如下結(jié)果:

    (UP:代表應(yīng)用正常、DOWN:代表應(yīng)用不正常、UNKNOWN代表未知狀態(tài)。)

    從上面的相應(yīng)結(jié)果來看,我們得到的監(jiān)控信息非常有限,想要更多信息的話,需要在application.yml中進(jìn)行配置:

management:
  endpoint:
 health:
   show-details: always
# 自定義一些相關(guān)信息,暴露給調(diào)用者
info:
  app:
 name: family
 describe: java123456789復(fù)制代碼類型:[java]

    執(zhí)行代碼,在瀏覽器訪問:

    http://localhost:8888/actuator/info

    還可以開放監(jiān)控端點(diǎn)給服務(wù)調(diào)用者:

management:
  # 開放所有監(jiān)控端點(diǎn)
  endpoints:
 web:
   exposure:
  include: '*'123456復(fù)制代碼類型:[java]
management:
  # 開放訪問的服務(wù)端點(diǎn)
  endpoints:
 web:
   exposure:
  include: beans,env
management:
  # 不暴露對外開放的服務(wù)端點(diǎn)
  endpoints:
 web:
   exposure:
  include: mappings123456789101112復(fù)制代碼類型:[java]

    開啟端點(diǎn)和開放端點(diǎn)是不一樣的,絕大部分的監(jiān)控端點(diǎn)是默認(rèn)開啟的,少部分監(jiān)控端點(diǎn)默認(rèn)是不開啟的,對于默認(rèn)不啟用的監(jiān)控服務(wù)端點(diǎn),一定要先開啟:

management:
  endpoint:
 shutdown:
   enabled: true1234復(fù)制代碼類型:[java]

    shutdown可以替換。

    Actuator提供的接口

    Actuator提供了13個(gè)接口,可以分為三大類:配置接口、度量接口和其它接口,具體如下表所示。

HTTP 方法路徑描述
GET/autoconfig提供了一份自動配置報(bào)告,記錄哪些自動配置條件通過了,哪些沒通過
GET/configprops描述配置屬性(包含默認(rèn)值)如何注入Bean
GET/beans描述應(yīng)用程序上下文里全部的Bean,以及它們的關(guān)系
GET/dump獲取線程活動的快照
GET/env獲取全部環(huán)境屬性
GET/env/{name}根據(jù)名稱獲取特定的環(huán)境屬性值
GET/health報(bào)告應(yīng)用程序的健康指標(biāo),這些值由HealthIndicator的實(shí)現(xiàn)類提供
GET/info獲取應(yīng)用程序的定制信息,這些信息由info打頭的屬性提供
GET/mappings描述全部的URI路徑,以及它們和控制器(包含Actuator端點(diǎn))的映射關(guān)系
GET/metrics報(bào)告各種應(yīng)用程序度量信息,比如內(nèi)存用量和HTTP請求計(jì)數(shù)
GET/metrics/{name}報(bào)告指定名稱的應(yīng)用程序度量值
POST/shutdown關(guān)閉應(yīng)用程序,要求endpoints.shutdown.enabled設(shè)置為true
GET/trace提供基本的HTTP請求跟蹤信息(時(shí)間戳、HTTP頭等)

    服務(wù)保護(hù)緩存及跨域

    我們將服務(wù)端點(diǎn)開放時(shí),面向的對象是用戶而不是對外的所有人。所以要對角色進(jìn)行控制,下面來給大家用SpringSecurity來配置實(shí)現(xiàn)對Actuator服務(wù)端點(diǎn)的保護(hù)。

    首先引入依賴:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>1234復(fù)制代碼類型:[java]

    之后在SpringSecurity權(quán)限管理配置,在Configuration文件夾下創(chuàng)建

    ActuatorSecurity.java:

package com.example.demo.configuration;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configurationpublic class ActuatorSecurity extends WebSecurityConfigurerAdapter { @Override
 protected void configure(HttpSecurity http) throws Exception {
  http.httpBasic().and()
 .authorizeRequests() // 有ACTUATOR_ADMIN角色標(biāo)識的用戶才能訪問
 .antMatchers("/actuator/*").hasRole("ADMIN") // 必須登錄認(rèn)證才能訪問
 .antMatchers("/actuator/*").authenticated();
 }
}123456789101112131415161718復(fù)制代碼類型:[java]

    在application.yml中配置,添加一個(gè)用戶及其具有的角色

spring:
  security:
 user:
   name: family
   password: 123456
   roles:
  ADMIN1234567復(fù)制代碼類型:[java]

    在瀏覽器訪問:

    http://localhost:8888/actuator/health

    服務(wù)端點(diǎn)緩存

    不帶參數(shù)的端點(diǎn)請求SpringBoot會自動進(jìn)行緩存,通過下面的配置可以設(shè)置緩存時(shí)間。

management:
  endpoint:
 beans:
   cache:
  time-to-live: 200s12345復(fù)制代碼類型:[java]

    如果端點(diǎn)添加了SpringSecurity保護(hù),服務(wù)端點(diǎn)的響應(yīng)結(jié)果將不會被緩存。

    我們前面一直默認(rèn)使用“/actuator”作為服務(wù)訪問的根路徑,但是這會造成安全隱患,所以可以做一些個(gè)性化配置。

management:
  endpoints:
 web:
   base-path: /family
   path-mapping:
  health: healthcheck123456復(fù)制代碼類型:[java]

    配置修改之后訪問鏈接變成:

    http://localhost:8888/actuator/health

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多