spring boot 新增admin監控
阿新 • • 發佈:2018-12-25
一、Spring Boot Admin簡介
spring boot admin github開源地址:https://github.com/codecentric/spring-boot-admin
它主要的作用是在Spring Boot Actuator的基礎上提供簡潔的WEB UI展示。
二、專案使用:
1、搭建一個maven web專案
2、pom依賴配置
- <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>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-client</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>de.codecentric</groupId>
- <artifactId>spring-boot-admin-server-ui-login</artifactId>
- </dependency>
在pom.xml中新增上以上配置
admin服務端:spring-boot-admin-server、spring-boot-admin-server-ui
admin客戶端:spring-boot-admin-starter-client (加上該項能監控服務端自身的執行狀態,其他專案只需要引入client就可以引入監控)
安全:spring-boot-starter-security
登入驗證:spring-boot-admin-server-ui-login (也可以自行新增簡單的登入介面)
3、application.yml
- info:
- app:
- name: imard
- version: v1.0.0
- logging:
- file: "d:/logs/imard/boot.log"
- management:
- context-path: "/actuator"
- spring:
- application:
- name: "@[email protected]"
- boot:
- admin:
- url: http://www.test.com:8080
- profiles:
- active:
- - secure
- ---
- spring:
- profiles: insecure
- management:
- security:
- enabled: false
- security:
- basic:
- enabled: false
- ---
- spring:
- profiles: secure
- boot:
- admin:
- username: "${security.user.name}"
- password: "${security.user.password}"
- client:
- metadata:
- user.name: "${security.user.name}"
- user.password: "${security.user.password}"
- security:
- user:
- name: user
- password: pass
info配置app的基本資訊
www.test.com 在本機hosts中做了對映
4、Application.java
- @Configuration
- @EnableAutoConfiguration
- @EnableAdminServer
- publicclass Application extends SpringBootServletInitializer {
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- return application.sources(Application.class);
- }
- publicstaticvoid main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
@EnableAdminServer 新增上該註解啟動監控
5、SecurityConfig
- @Profile("secure")
- @Configuration
- publicclass SecurityConfig extends WebSecurityConfigurerAdapter {
- @Override
- protectedvoid configure(HttpSecurity http) throws Exception {
- http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
- http.logout().logoutUrl("/logout");
- http.csrf().disable();
- http.authorizeRequests()
- .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**").permitAll();
- http.authorizeRequests().antMatchers("/api/**").permitAll().antMatchers("/**")
- .authenticated();
- // Enable so that the clients can authenticate via HTTP basic for registering
- http.httpBasic();
- }
- }
6、監管管理
配置完1~5個步驟以後,使用application啟動監控程式。
通過http://www.test.com:8080/login.html監控登入介面進行安全驗證後,如下圖: