Spring-Boot之Admin服務監控-9


  一、Spring Boot Admin用於管理和監控一個或者多個Spring Boot程序。Spring Boot Admin分為Server端和Client 端,Client端可以通過向Http Server端注冊,也可以結合SpringCloud的服務注冊組件Eureka 進行注冊。SpringBoot Admin 提供了用AngularJs 寫的 Ul 界面,用於管理和監控。其中監控內容包括Spring Boot的監控組件Actuator的各個Http節點,也支持更高級的功能,包括Turbine、Jmx、Loglevel 等。

  二、直接使用Spring Boot Admin監控客戶端

  1、Admin Server

  (1)加入依賴

        <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.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

  (2)編寫啟動項

package com.cetc;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAdminServer public class AdminServerApplication {

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

}

  (3)編寫配置文件application.yaml

server:
  port: 8691
spring:
  application:
    name: admin-server
management:
  endpoints:
    web:
      exposure:
        include: ["*"]
  endpoint:
    health:
      show-details: always

  2、Admin Client,為了簡單使用,我這里還是通過feign的方式去接入Eureka的服務。

  (1)目錄結構和Spring-Cloud之Feign聲明式調用-4類似,這里全部寫出來,有興趣可以看下方源碼

  

  (2)加入依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--admin-client-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.0.4</version>
        </dependency>
        <!--admin-client-->

  (3)啟動項沒有什么特別的,這里直接編寫配置文件application.yaml

server:
  port: 8692
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: ["http://127.0.0.1:8691"] # 配置注冊的admin Server服務
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8670/eureka/ # 實際開發中建議使用域名的方式
management:
  endpoints:
    web:
      exposure:
        include: ["*"]
  endpoint:
    health:
      show-details: always
    logfile: external-file: logs/log.log # 這里的配置主要是在admin server中看到日志記錄
logging:
  path: logs
  file: logs/log.log # 日志配置

  3、測試。啟動Eureka-Server,Eureka-Client,Admin-Server,Admin-Client。端口分別為:8670、8673、8691、8692。

  (1)Eureka-Server注冊效果

  

   (2)Admin-Server效果

  

  三、讓Admin-Server通過Eureka注冊中心來監控所有服務(這才是重點),這里還加入了Security配置,主要目的是為了安全。

  1、Admin-Server

  (1)所需依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <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.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

  說明:spring-boot-admin-starter-server的版本選擇要慎重,不然會有很多坑。

  (2)編寫啟動項

package com.cetc;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableAdminServer @EnableEurekaClient public class AdminServerApplication {

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

  (3)編寫Security配置,這里也可以參考官網配置:https://codecentric.github.io/spring-boot-admin/2.0.4/#_securing_spring_boot_admin_server

package com.cetc.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
       .antMatchers("/assets/**").permitAll() .anyRequest().authenticated() .and()
//說明:這里和官網不同的是,因為默認端口為/login,所以我這里直接放開login.html就可以了不用配置loginProcessingUrl //在說明一點,這里是采用的本地頁面的。如果前后端分開,請配具體的登錄接口。 .formLogin() .loginPage("/login.html").permitAll() .and() //默認接口/logout,不用配置logoutUrl .logout() .logoutSuccessUrl("/login.html") .and() //這里必須加入httpBasic,因為Eureka-Server是基於最原始的方式進行驗證的。 .httpBasic(); } }

  (4)編寫配置文件

server:
  port: 8691
spring:
  application:
    name: admin-server
  security:
    user:
      name: admin
      password: admin # 加入登錄密碼
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8670/eureka/ # 實際開發中建議使用域名的方式
 instance: metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
management:
  endpoints:
    web:
      exposure:
        include: ["*"]
  endpoint:
    health:
      show-details: always # 顯示具體詳情

  說明:metadata-map,主要用於方向驗證使用。

  2、Admin-Client這里不需要修改任何配置,只需要注冊配置文件手動注冊的配置

server:
  port: 8692
spring:
  application:
    name: admin-client
# boot: # admin: # client: # url: ["http://127.0.0.1:8691"] # 配置注冊的admin Server服務
eureka: client: service-url: defaultZone: http://127.0.0.1:8670/eureka/ # 實際開發中建議使用域名的方式
management:
  endpoints:
    web:
      exposure:
        include: ["*"]
  endpoint:
    health:
      show-details: always
    logfile:
      external-file: logs/log.log # 這里的配置主要是在admin server中看到日志記錄
logging:
  path: logs
  file: logs/log.log # 日志配置

  3、測試。啟動Eureka-Server,Eureka-Client,Admin-Server,Admin-Client。端口分別為:8670、8673、8691、8692。

  (1)Eureka-Server效果

  

   (2)Admin-Server效果

  

  說明:為什么Eureka-Client是INSTANCES DOWN的狀態。這里服務其實是正常的,最主要的點在於Eureka-Client沒有配置actuator。所以訪問不到/acturtor/health接口。

  四、源碼地址:https://github.com/lilin409546297/spring-cloud/tree/master/admin


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM