Spring Boot Admin 詳解(Spring Boot 2.0,基於 Eureka 的實現)

原文:http://www.javashuo.com/article/p-kshwdali-gu.htmlgit

 

Spring Boot Admin 用於監控基於 Spring Boot 的應用,它是在 Spring Boot Actuator 的基礎上提供簡潔的可視化 WEB UI。github

(一)簡介
Spring Boot Admin 提供了不少功能,如顯示 name、id 和 version,顯示在線狀態,Loggers 的日誌級別管理,Threads 線程管理,Environment 管理等。web

(二)Spring Boot Admin 是由服務端和客戶端組成
在 Spring Boot 項目中,Spring Boot Admin 做爲 Server 端,其餘的要被監控的應用做爲 Client 端,基於這種的配置以下步驟:spring

2.1 Server
2.1.1 添加相關依賴
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.1.2 啓動類添加註解,開啓監控
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
2.1.3 配置文件
server:
port: 8788
2.2 Client
2.2.1 添加相關依賴
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.0</version>
</dependency>
2.2.2 配置文件
spring.boot.admin.client.url: "http://localhost:8788"
management.endpoints.web.exposure.include: "*"
以上的配置,就能夠實現 Spring Boot 項目中 Spring Boot Admin 監控其餘應用了,可是這不是咱們的重點,詳細信息請參考官網文檔:http://codecentric.github.io/spring-boot-admin/2.0.0/,咱們的重點是在 Spring Cloud 中使用 Spring Boot Admin 監控 Spring Cloud 的服務,下面咱們就詳細的講解在 Spring Cloud 中搭建 Spring Boot Admin安全

(三)在 Spring Cloud 中基於 Eureka 的 Spring Boot Admin 的搭建
3.1 啓動以前的項目 eureka server,端口8761
3.2 新建 module(springboot-admin),建立步驟參考上篇
3.2.1 添加相關依賴,pom文件:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
因爲項目中使用的是 spring boot 2.0 版本,因此這裏要使用 spring boot admin 的最新版本(還未正式發佈),本人測試使用 spring boot admin 2.0.0 版本會有問題。springboot

3.2.2 啓動類添加註解
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableEurekaClient
public class SpringBootAdminApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}

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

@Profile("secure")
@Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;

public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler 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
}
}
}
SecurityPermitAllConfig和SecuritySecureConfig的配置是 Spring Boot Admin 官方給的配置,是對 url 進行安全認證等配置,照着配置便可。@EnableEurekaClient 註解是把 Spring Boot Admin 註冊到 Eureka 裏,這樣 Spring Boot Admin 就能夠發現註冊到 Eureka 裏的其餘服務實例,@EnableAdminServer 註解是開啓監控功能。app

3.2.3 配置文件
官方有給出示例,主要是配置 eureka 的地址,這裏要強調說明的一點,因爲 Spring Boot 2.0 的 Actuator 只暴露了 /health、/info 兩個端口(爲了安全考慮),因此要配置 management.endpoints.web.exposure.include 的屬性,下面的配置文件中暴力了一點,配置暴露了全部的端點,因爲 Spring Boot Admin 有 web UI 管理界面,配置了登陸的用戶名密碼以下,使用了 security.user 的屬性,其餘的詳細配置,能夠查看官方文檔。ide

spring:
application:
name: spring-boot-admin
profiles:
active:
- secure
server:
port: 8788

# tag::configuration-eureka[]
eureka: #<1>
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

management:
endpoints:
web:
exposure:
include: "*" #<2>
endpoint:
health:
show-details: ALWAYS
# end::configuration-eureka[]

---
spring:
profiles: insecure

---
spring:
profiles: secure
security:
user:
name: "user"
password: "password"
eureka:
instance:
metadata-map:
user.name: "user" #These two are needed so that the server
user.password: "password" #can access the protected client endpoints
3.2.4 啓動 spring boot admin 服務,界面以下:spring-boot


用戶名密碼即上面的配置,user/password登陸成功後測試

 

此時因爲 Eureka 裏只有 Spring Boot Admin 自身已註冊,因此其監控列表裏只有本身,下面咱們啓動其餘的服務,讓其註冊到 Eureka 裏。

3.2.5 啓動 spring-demo-service 服務
(以前文章裏現有的服務,能夠查找此前的文章,或者查看文末的源碼下載),在啓動前,還有一處要配置,就是在 3.2.3 裏說的,Actuator 在 spring boot 2.0 版本後,只暴露了兩個端點,因此此時啓動,監控不到所需的信息,下面修改配置文件以下:

server:
port: 8281

eureka:
client:
serviceUrl:
# 向每一個註冊中心註冊
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
spring:
application:
name: spring-demo-service

management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
此時啓動 spring-demo-service,發現監控列表裏 spring-demo-service 已經有了

 

點擊 SPRING-DEMO-SERVICE 進入,頁面以下,能夠看到有咱們以前介紹的一些功能。

 

 

3.2.6 其餘的配置
若是想要顯示版本信息,配置文件中加入 info.version=1.0.0 能夠配置版本信息

源碼下載:https://github.com/shmilyah/spring-cloud-componets 

相關文章
相關標籤/搜索