SpringBoot-集成SpringBootAdmin-進階版


上一篇我們已經學習了SpringBoot-集成BootAdmin

前言

我們知道項目的監控是尤為重要的,但是我們如果用jdk 自帶的jconsole 和jvisualvm 的話會非常繁瑣,且界面不是很友好。之前我們使用了spring boot 項目,但是都沒有對項目有一個很好的監控。在spring 家族中有 spring-boot-admin 可以很好的幫我們起到監控微服務項目的作用。

spring-boot-admin 是一個針對 Spring Boot 的 Actuator 接口進行 UI 美化封裝的監控工具,它可以在列表中瀏覽所有被監控 spring-boot 項目的基本信息、詳細的 Health 信息、內存信息、JVM 信息、垃圾回收信息、各種配置信息(比如數據源、緩存列表和命中率)等,還可以直接修改 logger 的 level。

spring-boot-admin 分為服務端和客戶端。服務端是一個單獨的微服務,用來查看監控的項目的運行情況,客戶端是我們一個個的微服務項目。所以要想讓我們的項目被服務端監控到,就需要將我們的服務注冊到服務端去

搭建admin-server

我們先來搭建spring-boot-admin 的服務端,上面說了服務端是一個單獨的項目。所以我們創建一個新的springboot 項目。創建好后,我們做一下修改

配置pom.xml

<?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 https://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>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.quellanan</groupId>
    <artifactId>springbootadmin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootadmin</name>
    <description>springbootadmin project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.version>
    </properties>

    <dependencies>

        <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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.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>

啟動類

在我們的啟動類上加入@EnableAdminServer 注解,如果不加的話,項目可以正常啟動,但是看不到任何東西。@EnableAdminServer 注解的作用就是啟動監控

@SpringBootApplication
@EnableAdminServer
public class SpringbootadminApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootadminApplication.class, args);
    }
}

配置 security

這樣配置好之后,就可以啟動項目啦,但是我們這里先不啟動,因為上一節我們學習了,spring-boot-security .這里我們將它用起來。
我們前面已經引入了 security ,接下來,我們在application中增加配置

spring.security.user.name=admin
spring.security.user.password=123456

表示這個用戶才能訪問。另外我們創建一個 SecurityConfig 類 繼承 WebSecurityConfigurerAdapter 重寫 configure(HttpSecurity http) 方法。代碼如下:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler
                = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl("/");
        http.authorizeRequests()
                .antMatchers("/assets/**").permitAll()
                .antMatchers("/login").permitAll()
                .anyRequest().authenticated().and()
                .formLogin().loginPage("/login")
                .successHandler(successHandler).and()
                .logout().logoutUrl("/logout").and()
                .httpBasic().and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        "/instances",
                        "/actuator/**"
                );
    }
}

現在我們啟動一下項目看看。啟動項目后輸入

會跳轉到 登錄界面,進入主頁現在是什么都沒有的

搭建admin-client

到此我們服務端的配置就已經可以了,現在我們來配置一下客戶端,我們隨便找一個Springboot 項目,或者自己創建一個新的項目都可以

pom.xml

我們先在pom 文件中加入admin-client 依賴,注意這里的版本需要和server 的版本一致

		<dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.2.1</version>
        </dependency>

application.properties

spring.boot.admin.client.url=http://localhost:8080
management.endpoints.web.exposure.include=*
spring.application.name=sdwlzlapp-file
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

注意點:

  • spring.boot.admin.client.url 指向我們服務端的項目接口路徑。
  • management.endpoints.web.exposure.include 表示將所有端口都暴露出來,可以被監控到。
  • spring.application.name 表示改項目在spring-boot-admin 上的的顯示名稱。
  • spring.boot.admin.client.username 和password 就是設置的用戶名和密碼了,這里需要注意的是,如果admin-server 中沒有集成 security 的話,不用配置用戶名和密碼也可以注冊進去,在服務端可以監控到,但如果admin-server 集成了security,就需要保證client 中配置的用戶名和server

演示

配置了上面這些,就就可以將項目注冊到admin-server 中啦,我們啟動一下項目

現在還有一個問題,如果我們項目本身就集成的安全框架,比如security ,沒有登錄的話不能訪問接口,那這樣的項目怎么被admin-server 監控到呢?比如就我們上節將的security 的demo ,我們注冊進來,雖然監控到了,但是是一個失敗的狀態

可以看到,不難發現問題,那就是監控的接口也被項目本身攔截了,所以才導致是失敗的狀態,那要怎么修改了呢,其實也好處理,將這幾個接口放開就可以了。我們在項目的SecurityConfig 類中configure(HttpSecurity http)加上

代碼如下:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/", "/hello").permitAll()
            .antMatchers( "/actuator/**").permitAll()
            .antMatchers( "/instances").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            //.loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll();
    }

這樣我們重啟項目,就發現可以監控成功了


免責聲明!

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



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