Spring Boot Admin

Spring Boot Admin 是一個管理和監控 Spring Boot 應用程序的開源軟件,每一個應用都認爲是一個客戶端,經過 HTTP 或者使用 Eureka 註冊到 admin server 中進行展現,Spring Boot Admin UI 部分使用 Vue.js 將數據展現在前端。前端

1、Spring Boot Admin 分爲:web

   服務端是一個監控後臺用來彙總展現全部的監控信息spring

     客戶端就是具體的應用tomcat

1.server和client的模式app

(1)server端ide

  添加依賴spring-boot

<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.1.6</version>
</dependency>

  配置fetch

server.port=8000
spring.application.name=Admin Server

  啓動類ui

package com.example.management;

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

@SpringBootApplication
@EnableAdminServer
public class ManagementApplication {

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

(2)client端this

  添加依賴

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

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

  配置

server.port=8001
spring.application.name=Admin Client

spring.boot.admin.client.url=http://localhost:8000
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

  啓動類

package com.example.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ClientApplication {

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

注:

  使用時須要先啓動服務端,在啓動客戶端的時候打開 Actuator 的接口,並指向服務端的地址

2.基於springcloud的模式 

在server端加入@EnableDiscoveryClient註解,spring boot admin就會主動去拉取註冊中心的註冊服務列表,從而獲取他們的服務動態信息

註冊中心使用Eureka

(1)註冊中心Center

  添加依賴

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      <version>2.1.3.RELEASE</version>
</dependency>

  配置

server.port=8761
spring.application.name=Center

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.default-zone=http://localhost:8761/eureka/

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

  啓動類,添加@EnableEurekaServer註解,開啓Eureka Server

package com.example.center;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class CenterApplication {

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

  啓動,訪問http://localhost:8761/

尚未服務向註冊中心註冊服務

(2)Admin server

  依賴

<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>de.codecentric</groupId>
     <artifactId>spring-boot-admin-starter-server</artifactId>
     <version>2.1.6</version>
</dependency>

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     <version>2.1.3.RELEASE</version>
</dependency>

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

  配置

server.port=8000
spring.application.name=Admin Server

eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.health-check-url-path=/actuator/health
eureka.client.registry-fetch-interval-seconds=5
eureka.client.service-url.default-zone=${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

spring.thymeleaf.check-template-location=false

  啓動類,添加@EnableDiscoveryClient註解,開啓DiscoveryClient的功能

package com.example.management;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class ManagementApplication {

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

(2)Admin Client

  依賴

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

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>

  配置

server.port=8001
spring.application.name=Admin Client

eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.health-check-url-path=/actuator/health
eureka.client.registry-fetch-interval-seconds=5
eureka.client.service-url.default-zone=${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

  啓動類

package com.example.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

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

依次啓動服務Center,Admin Client,Admin Server

http://localhost:8761/

http://localhost:8000/

註冊到註冊中心的服務都會被監控

 

2、集成了登陸模塊

  登陸界面默認集成到了spring security模塊

  在Admin server裏

  添加依賴

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

  添加配置

spring.security.user.name=abc
spring.security.user.password=abc123

  添加配置類

package com.example.management.configure;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        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();
    }
}

從新啓動,http://localhost:8000,顯示登陸頁面,輸入配置的用戶名密碼

 

 

 3、監控郵件通知

  添加依賴

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

  添加配置

spring.mail.host=smtp.126.com
spring.mail.username=YYYYYYY@126.com
spring.mail.password=abcabc
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# 發送給誰
spring.boot.admin.notify.mail.to=XXXXXXX@qq.com
# 是誰發送出去的
spring.boot.admin.notify.mail.from=YYYYYYY@126.com
spring.boot.admin.notify.mail.enabled=true

  配置完成後,從新啓動,當服務上線下線的時候,指定的郵箱就能夠收到郵件了

 說明:

  若是是下圖,請添加配置

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

 

 監控的endpoint會都打開

相關文章
相關標籤/搜索