一朗兵、??? 背景說(shuō)明
Java服務(wù)級(jí)監(jiān)控用于對(duì)每個(gè)應(yīng)用占用的內(nèi)存、線程池的線程數(shù)量顶滩、restful調(diào)用數(shù)量和響應(yīng)時(shí)間余掖、JVM狀態(tài)、GC信息等進(jìn)行監(jiān)控礁鲁,并可將指標(biāo)信息同步至普羅米修斯中集中展示和報(bào)警盐欺。網(wǎng)上類似的文章較多,內(nèi)容長(zhǎng)且時(shí)間較舊仅醇,本文所寫內(nèi)容已經(jīng)過(guò)實(shí)踐驗(yàn)證冗美,可快速幫助你實(shí)現(xiàn)集成。
二析二、??? 監(jiān)控方案說(shuō)明
本監(jiān)控方案僅用于SpringBoot 2項(xiàng)目粉洼。通過(guò)在服務(wù)中引入actuator組件實(shí)現(xiàn)與普羅米修斯的集成。由于actuator有一定的安全隱患甲抖,本文也著重介紹了如何實(shí)現(xiàn)授權(quán)訪問(wèn)漆改。
三、??? 方案詳情
1准谚、引入spring actuator及spring security
Actuator用于收集微服務(wù)的監(jiān)控指標(biāo)包括內(nèi)存使用挫剑、GC、restful接口調(diào)用時(shí)長(zhǎng)等信息柱衔。為避免敏感信息泄露的風(fēng)險(xiǎn)樊破,還需要同時(shí)使用spring security框架以實(shí)現(xiàn)訪問(wèn)actuator端點(diǎn)時(shí)的授權(quán)控制∷纛恚“micrometer-registry-prometheus”用于將您的微服務(wù)暴露為“exportor”哲戚,可直接對(duì)接普羅米修斯。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.8.1</version> </dependency>
2艾岂、配置文件(application.yml)增加應(yīng)用名稱
spring: application: name: your service name
“name”節(jié)點(diǎn)的值需要根據(jù)當(dāng)前服務(wù)的名稱填寫顺少,建議規(guī)則如下:小于32字符長(zhǎng)度;全小寫王浴;單詞間使用“-”分隔脆炎。
3、配置文件中增加actuator配置
建議將下面配置放在二級(jí)配置文件(application-x氓辣,例:線上配置文件“application-prod”)中秒裕。配置信息的“include”節(jié)點(diǎn)建議僅暴露“prometheus”節(jié)點(diǎn)。
#name和password為您自定義的信息 spring: security: user: name: *** password: *** management: server: port: 1234 #給actuator一個(gè)自定義端口钞啸,建議與服務(wù)的端口進(jìn)行區(qū)分 metrics: tags: application: ${spring.application.name} endpoints: web: base-path: /${spring.application.name}/application-monitor #安全起見(jiàn)几蜻,此處使用自定義地址 exposure: include: prometheus #安全起見(jiàn)喇潘,僅暴露prometheus一個(gè)端點(diǎn)
4、配置spring security
為實(shí)現(xiàn)actuator端點(diǎn)訪問(wèn)授權(quán)梭稚,需要在啟動(dòng)文件(即包含“static void main”方法的文件)的同級(jí)任意目錄中建立一個(gè)名為“SecurityConfig .java”的java文件颖低,并將下列代碼復(fù)制到類中
@EnableWebSecurity @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .formLogin() .and() .httpBasic() .and() .authorizeRequests() //application-monitor為3小節(jié)本配置文件中自定義的actuator端點(diǎn)url,此處代碼表示當(dāng)訪問(wèn)actuator端點(diǎn)時(shí)哨毁,需要進(jìn)行登錄枫甲。用戶名和密碼參看3小節(jié)配置 .antMatchers("/**/application-monitor/**").authenticated() .anyRequest().permitAll(); //其它業(yè)務(wù)接口不用登錄 } }
5源武、驗(yàn)證結(jié)果
配置完成后啟動(dòng)服務(wù)扼褪。訪問(wèn)服務(wù)中的查詢類和命令類業(yè)務(wù)接口,應(yīng)與引入“actuator”和“spring security”前一致粱栖。訪問(wèn)“ip:1234/{your service name}/application-monitor/prometheus”话浇,應(yīng)顯示登錄界面,如下圖所示闹究。
輸入3小節(jié)中“spring.security.user”節(jié)點(diǎn)中的用戶名與密碼幔崖,顯示如下界面表示配置成功。需要注意:務(wù)必保證測(cè)試結(jié)果與上述說(shuō)明一致渣淤,以避免服務(wù)正常的restful接口無(wú)法被訪問(wèn)赏寇。
配置成功后,您的服務(wù)就變成了一個(gè)標(biāo)準(zhǔn)的“exportor”价认,可以實(shí)現(xiàn)與普羅米修斯的對(duì)接嗅定。
6、附錄
如果出現(xiàn)POST用踩、PUT和DELETE方法無(wú)法訪問(wèn)渠退,請(qǐng)?jiān)黾尤缦麓a配置。