springboot2.x中的服務監控
阿新 • • 發佈:2018-12-25
想給服務新增一個監控,看看網上各位前輩的,基本都是基於springboot1.x的,springboot升級到2.0以後和1.x還是有很多不一樣的,那麼2.0以後怎麼使用admin監控呢?
先看下圖的managment.security.enable,現在已經是過時API了,那麼我們必須要更新知識庫了。
總體思路
和之前的思路一樣,分為服務端和客戶端。
服務端配置pom
客戶端新增監控url配置
server端
1、建立專案,引入依賴,我的完整pom如下
<dependencies>
<!--表示為web工程-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--暴露各種指標-->
<dependency>
<groupId>org.springframework.boot</groupId >
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency >
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
2、配置yml
server:
port: 8888
eureka:
instance:
hostname: localhost
prefer-ip-address: true
lease-renewal-interval-in-seconds: 10
client:
registry-fetch-interval-seconds: 5
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
spring:
application:
name: spring-boot-admin-server
3、啟用服務監控
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class ActuatorApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorApplication.class, args);
}
}
client端
在需要監控的專案中新增以下配置
1、新增pom依賴
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
2、新增yml配置
spring:
application:
name: boot-service
boot:
admin:
client:
url: http://localhost:8888
檢視監控
先啟動剛才建立的服務監控專案,然後再分別啟動需要監控的專案,然後訪問http://localhost:8888(根據你的實際情況),訪問結果如下
一眼就看到一個不正常的服務,我們點進去看一下出了什麼問題,因為我的服務確實在正常執行,可以正常訪問
結果就很明顯了,一看network error,明顯是網路不通,可是服務在正常執行,那麼基本就是許可權的問題了,因為我的eboot-admin添加了shiro的許可權攔截,因此上面的/actuator/**都被攔截了,我們在shiro中將該路徑放行
filterChainDefinitionMap.put("/actuator/**", "anon");
再次訪問,結果如下
服務出現問題,applications會有如下提示,一看offline就······
至此,springboot2.0的服務監控已經搞定。同時actuator也可以配合security做許可權控制,但是我們監控的這些服務大部分可能都是在內網環境使用,後期我會加上帶許可權的監控,所有原始碼已上傳到gitee,【戳我獲取原始碼】,後期新增功能和程式碼會隨時上傳,需要的小夥伴隨時獲取哦
當然,如果能幫我點個star就更好了\^_\^