javascript
SpringCloud(8)微服务监控Spring Boot Admin
1.簡介
Spring Boot Admin 是一個管理和監控Spring Boot 應用程序的開源軟件。Spring Boot Admin 分為 Server 端和 Client 端,Spring Boot Admin UI部分使用AngularJs將數據展示在前端。
2.工程架構
- Eureka Server:服務注冊中心,端口為8761。
- Admin Server:用于對微服務系統進行統一的監控和管理。
- Admin Clinet:客戶端集成Admin。
3.構建Admin Server
新建Spring Boot工程,取名為 admin-server 其完整依賴為:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>admin-server</artifactId><version>0.0.1-SNAPSHOT</version><name>admin-server</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><spring-cloud.version>Dalston.RELEASE</spring-cloud.version></properties><dependencies><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-server-ui</artifactId><version>1.5.1</version></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-server</artifactId><version>1.5.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><!-- 管理界面與JMX-Beans交互 --><dependency><groupId>org.jolokia</groupId><artifactId>jolokia-core</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>配置application.yml,設置 management.security.enabled=false 關閉安全驗證,設置Spring Boot Admin默認開啟的節點.
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/ server:port: 5000 spring:application:name: admin-serverboot:admin:routes:endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream management:security:enabled: false logging:file: "logs/boot-admin-sample.log"在 resources 目錄下建一個 logback-spring.xml文件
<?xml version="1.0" encoding="UTF-8"?> <configuration><include resource="org/springframework/boot/logging/logback/base.xml"/><jmxConfigurator/> </configuration>注解 @EnableAdminServer 開啟Admin Server的功能.
@EnableEurekaClient @EnableAdminServer @SpringBootApplication public class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}}這樣Spring Boot Admin工程創建完畢!
4.構建Admin Client
新建Spring Boot工程,取名為 admin-client,其完整依賴為:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>admin-client</artifactId><version>0.0.1-SNAPSHOT</version><name>admin-server</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><spring-cloud.version>Dalston.RELEASE</spring-cloud.version></properties><dependencies><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-eureka</artifactId></dependency><dependency><groupId>org.jolokia</groupId><artifactId>jolokia-core</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>配置 application.yml 文件,設置日志輸出路徑,并關閉 Actuator 模塊的安全驗證。
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/ server:port: 8762 spring:application:name: admin-client management:security:enabled: false logging:file: "logs/boot-admin-client.log"在程序的啟動類上加上 @EnableEurekaClient 注解,開啟EurekaClient功能.
@SpringBootApplication @EnableEurekaClient public class AdminClientApplication {public static void main(String[] args) {SpringApplication.run(AdminClientApplication.class, args);}}5.啟動程序
依次啟動 eureka-server、admin-server 和 admin-client 工程,在瀏覽器訪問 admin-server 的主頁 http://localhost:5000/,瀏覽器顯示界面如圖:
"JOURNAL"選項為服務注冊、下線、剔除的時間線。
6.添加安全登錄界面
Spring Boot Admin 提供了登錄界面的組件,并且和 Spring Boot Security 相結合,需要用戶登錄才能訪問。
引入依賴
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-server-ui-login</artifactId><version>1.5.0</version> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId> </dependency>在工程的application.yml中做以下配置,創建一個 security 的 user 用戶,它的用戶名為 admin ,密碼為 123456,。通過 eureka.instance.metadate-map 配置帶上該 security 的 user 用戶信息。
security:user:name: adminpassword: 123456 eureka:instance:metadata-map:user.name: adminuser.password: 123456然后,在程序中配置 Spring Boot Security,寫 SecurityConfig 的配置類,給靜態資源加上 permitAll() 方法,除上述以外的資源訪問需要權限認證,另外這些資源不支持 CSFR(跨站請求偽造),所以禁用掉 CSFR,最后需要開啟 Http 的額基本認證,即 httpBasic() 方法。
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {// Page with login form is served as /login.html and does a POST on /loginhttp.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();// The UI does a POST on /logout on logouthttp.logout().logoutUrl("/logout");// The ui currently doesn't support csrfhttp.csrf().disable();// Requests for the login page and the static assets are allowedhttp.authorizeRequests().antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**").permitAll();// ... and any other request needs to be authorizedhttp.authorizeRequests().antMatchers("/**").authenticated();// Enable so that the clients can authenticate via HTTP basic for registeringhttp.httpBasic();}}重新啟動 admin-server 工程,在瀏覽器中訪問 http://localhost:5000/,輸入用戶名admin,密碼為123456,登錄即可。
參考方志朋《深入理解Spring Cloud與微服務構建》
轉載于:https://www.cnblogs.com/yueshutong/p/10272494.html
總結
以上是生活随笔為你收集整理的SpringCloud(8)微服务监控Spring Boot Admin的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 隐写
- 下一篇: 专家:香港拥有人民币资产配置独特优势