簡介
springboot-admin實在Spring Boot Actuator的基礎上提供簡潔的視覺化WEB UI,是用來管理SpringBoot應用程式的一個簡單的介面,功能如下:
- 顯示 name/id 和版本號
- 顯示線上狀態
- Logging日誌級別管理
- JMX beans管理
- Threads會話和執行緒管理
- Trace應用請求跟蹤
- 應用執行引數資訊,如:
- Java 系統屬性
- Java 環境變數屬性
- 記憶體資訊
- Spring 環境屬性
應用
1.新建springboot-admin服務端
引入服務端pom依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.0.1</version>
</dependency>
<!-- spring-security安全認證 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
複製程式碼
application.yml配置
server:
port: 8080
#admin登入的使用者名稱和密碼
spring:
application:
name: admin-server
#security使用者配置
security:
user:
name: admin
password: admin
boot:
admin:
context-path: /admin
discovery: true
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
複製程式碼
新建SecuritySecureConfig配置:
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
/**
* @Description: TODO(Springboot-Admin登入配置)
* @Author: 愛飄de小子
*/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter{
/**
* -private
* 定義Admin上下文路徑
*/
private final String adminContextPath;
/**
* 由於解決分散式Web應用程式中的身份驗證和授權有多種方法,因此Spring Boot Admin不提供預設方法。預設情況下,spring-boot-admin-server-ui提供登入頁面和登出按鈕。
* Spring Security配置
* @param adminServerProperties
*/
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
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();
}
}
複製程式碼
啟動類新增@EnableAdminServer註解:
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 愛飄de小子
*/
@EnableAutoConfiguration
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
複製程式碼
啟動springboot專案即可訪問: ip:port/admin
2.配置客戶端(被監控的springboot專案)
pom檔案新增依賴:
<!-- springboot admin 客戶端 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
複製程式碼
application.yml配置:
spring:
boot:
admin:
client:
url: http://localhost:8080/
username: admin
password: admin
instance:
prefer-ip: true #使用IP註冊
#專案健康監控
management:
endpoints:
web:
exposure:
include: "*"
base-path: /admin #服務端配置的context-path
endpoint:
health:
show-details: always
#專案資訊
info:
app:
#自定義顯示資訊
name: "xxx"
description: "xxx"
version: "xx"
environment: "xx"
spring-boot-version: "2.0.1.RELEASE"
spring-admin-version: "2.0.1"
version: "1.0"
複製程式碼