SpringBoot Admin監控spring cloud alibaba nacos微服務


版本說明

Spring-Boot版本:2.1.8RELEASE

Spring-Cloud版本:Greenwich.SR6

Spring-Cloud-Alibaba版本:2.1.0.RELEASE

Spring-Boot-Admin-starter-client:2.1.5

Spring-Boot-Admin-starter-server:2.1.5

這里springboot和springboot admin的大小版本請相同,如boot版本是2.1,admin版本也應該是2.1,否則項目很有可能會報錯無法啟動。

創建admin server端

pom依賴:

(1)這里web排除tomcat,引入jetty,是因為用tomcat有可能會在啟動時報錯,而jetty則不會。

(2)引入了security依賴,這樣會在admin必須要登陸才可以進入,保證了一定的安全性。

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

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

配置文件

注意:如果在properties中,星號不需要打引號,在yml中,需要打單引號

server.port=8888 #端口號
spring.application.name=gulimall-admin #服務名
spring.security.user.name=admin #賬號
spring.security.user.password=admin #密碼
spring.cloud.nacos.discovery.server-addr=192.168.1.43:8848 #注冊中心地址
management.endpoints.web.exposure.include=* # 選擇暴露所有可監控端點

主啟動類&&相關配置

@EnableDiscoveryClient
@EnableAdminServer //開啟spring boot admin相關功能
@SpringBootApplication
public class GulimallAdminApplication {

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

}

security配置:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;
    public SecurityConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        http.authorizeRequests()
                .antMatchers(new String[]{"/instances/**","/actuator/**"}).permitAll()
                .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();
    }
}

創建admin client端(眾多需要被監控的微服務)

其余雜七雜八的依賴這里省略。。。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.1.8.RELEASE</version>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

yml配置

spring:  
  boot:
    admin:
      client:
        url: http://localhost:8888 #admin server的地址
management:
  endpoints:
    web:
      exposure:
        include: '*' #暴露所有可監控的端點

需要注意的是:如果要監控被security或oauth2所保護的微服務,我們需要在security中配置(如果因為安全,我們可以配置ip白名單,只有指定ip,才可以訪問)

image-20201106205842586

admin搭建完成

啟動所有client服務和server服務。登陸,賬號密碼是我們在server端配置文件中配置的。

登陸成功后(頁面還是挺好看的,至少比以前的版本好看!),我們可以在wallboard中看到所有服務的健康狀況和實例數

image-20201106210550954

我這里有一個紅色是因為那個微服務boot版本是2.2,導致依賴沖突(我也懶得改了,影響不大)

同時我的admin server服務也入駐進nacos注冊中心。

服務詳細信息

點擊任意服務,進入服務的詳情頁,服務詳細信息都在下圖。

image-20201106211845183

日志級別:

image-20201106212310723

監控線程:

image-20201106212352852

web監控:

image-20201106212419795

image-20201106213742059

網關信息:

系統緩存:

image-20201106212823518

點擊clear后,redis中的緩存同時刪除


免責聲明!

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



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