Spring Boot Admin 是管理和監控你的SpringBoot應用程序的社區項目,做者codecentric。應用程序經過Spring Boot Admin Client進行註冊,HTTP方式
或者使用springcloud服務發現(例如:Eureka,Consul)
。Spring Boot Admin UI只是Spring Boot Actuator endpoints上的一個AngularJs應用程序,提供圖形化界面的展現。git
SpringBoot | 2.0.1.RELEASE |
SpringBootAdmin | 2.0.0-SNAPSHOT |
SpringBoot2.0
版本以後配置文件會有部分調整,本文不適用於2.0
以前的版本
1. pom.xml
中引入Maven依賴github
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
PS:
spring-boot-admin-starter-server
包含spring-boot-admin-server
和spring-boot-admin-server-ui
兩個依賴
2. 開啓@EnableAdminServer
註解支持web
@SpringBootApplication @EnableAdminServer public class AdminServerSpringStrap { public static void main(String[] args) { SpringApplication.run(AdminServerSpringStrap.class, args); } }
PS:
若是使用SpringBootAutoConfiguration方式自動引入註解配置須要添加@AutoConfigureBefore({AdminServerAutoConfiguration.class})
,不然@EnableAdminServer
註解無效
至此Admin服務端已經配置完畢。spring
1. pom.xml
中引入Maven依賴安全
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency>
2. application.yml
配置app
--- spring: profiles: client application: name: client boot: admin: client: url: "http://localhost:8080/" // Admin服務端地址,根據實際狀況更改 server: port: 8081 management: endpoints: web: exposure: include: "*" // SpringBootActuator監控暴露全部接口
PS:
SpringBoot2.0
以後大部分endpoints不會被暴露,上面咱們暴露了全部endpoints,如應用到生產環境,考慮到安全問題,對於Actuator
的Endpoints
請根據須要進行配置
至此Admin客戶端已經配置完畢。ide
SpringSecurity
進行權限控制pom.xml添加Maven依賴spring-boot
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
添加到須要進行權限控制的項目中
1. Admin客戶端application.yml
配置fetch
--- spring: profiles: client application: name: client boot: admin: client: url: "http://localhost:8080" instance: metadata: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password} security: user: name: client password: client server: port: 8081 management: endpoints: web: exposure: include: "*"
PS:
security
下的屬性配置httpBasic
的用戶名密碼
instance.metadata
下的屬性配置向Admin服務端
傳輸本客戶端的用戶名和密碼,這樣Admin服務端請求的時候會攜帶已傳遞的用戶名密碼,若是此處的密碼和security
下的不一致,則服務端將沒有權限訪問客戶端的監控
配置完成後,啓動客戶端,服務端請求客戶端監控接口的時候會帶上客戶端傳遞的憑證,至此完畢。ui
1. Admin服務端增長SpringSecurity
的權限配置
啓動器添加
@EnableWebSecurity
註解
@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 } }
2. Admin服務端application.yml
配置
--- spring: profiles: admin application: name: admin security: user: name: admin password: admin server: port: 8080 management: endpoints: web: exposure: include: "*"
3. Admin客戶端application.yml
配置
--- spring: profiles: client application: name: client security: user: name: client password: client boot: admin: client: url: "http://localhost:8080" instance: metadata: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password} username: admin password: admin server: port: 8081 management: endpoints: web: exposure: include: "*"
PS:
客戶端增長了spring.boot.admin.client.[username,password]
配置,此配置是傳遞Admin服務端的憑證信息
,不然不能註冊到Admin服務端。其餘配置同上一節僅Admin客戶端進行權限控制
一致。
若是須要Admin服務端本身註冊本身,則只要將Admin服務端的application.yml
與客戶端一致便可。
至此SpringSecurity
權限控制配置完畢。
此文只說明Spring-Boot-Admin
集成Eureka
的配置,至於Eureka的集成請關注個人後續文章。
1. Maven依賴說明
Spring-Boot-Admin的相關依賴請參考上述說明
2. Admin服務端配置
spring: application: name: server-admin security: user: name: admin password: admin server: port: 9888 eureka: client: service-url: defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/ registry-fetch-interval-seconds: 5 instance: metadata-map: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password} lease-renewal-interval-in-seconds: 10 health-check-url-path: /actuator/health management: endpoints: web: exposure: include: "*"
配置基本同SpringBoot
項目一致,不一樣的是將Admin服務端註冊到Eureka註冊中心
,此配置包含權限控制,根據須要進行刪減。
3. Admin客戶端配置
spring: application: name: service-hi security: user: name: client password: client server: port: 8762 eureka: client: service-url: defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/ registry-fetch-interval-seconds: 5 instance: metadata-map: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password} lease-renewal-interval-in-seconds: 10 health-check-url-path: /actuator/health management: endpoints: web: exposure: include: "*"
配置基本同SpringBoot
項目一致,不一樣的是將Admin客戶端註冊到Eureka註冊中心
,此配置包含權限控制,根據須要進行刪減。若是客戶端和服務端都註冊到同一個註冊中心
,則不須要在客戶端指定服務端的地址,而且不須要在客戶端配置服務端的憑證信息。
至此Spring-Boot-Admin集成到Eureka完畢。
admin項目Maven倉庫
<repository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots</name> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>false</enabled> </releases> </repository>