SpringBoot Admin 2.0

Spring Boot Admin 用于管理和監(jiān)控一個或者多個Spring Boot應(yīng)用, Spring Boot Admin 分為Server端和Client端绽乔,Client通過http向Server端注冊饮怯, 也可以結(jié)合Spring Cloud 的服務(wù)注冊組件Eureka 進(jìn)行注冊轻纪,本項(xiàng)目使用Eureka進(jìn)行注冊發(fā)現(xiàn)壶辜。

github地址:https://github.com/liangxiaobo/SpringBootAdminDemo

首先看項(xiàng)目結(jié)構(gòu)分為:

  • eureka-server 發(fā)現(xiàn)注冊服務(wù)
  • spring-boot-admin-server SpringBootAdmin服務(wù)管理端
  • spring-boot-admin-client SpringBootAdmin客戶端


    c1.png

Eureka Server

eureka-server項(xiàng)目的搭建過程請看 spring cloud 搭建集群Eureka Server 逞泄,這里就簡單說下

pom.xml 依賴

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

啟動類中添加注釋 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

application.yml

spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8761

成功啟動后訪問:http://localhost:8761

spring-boot-admin-server

admin-server中的認(rèn)證模塊一起說看完整的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.boot.admin.server</groupId>
    <artifactId>spring-boot-admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-admin-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.0.1</spring-boot-admin.version>
        <spring-cloud.version>Finchley.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>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
        <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-security</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>
            <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>

spring-boot-admin-server的啟動類:

@Configuration
@EnableAutoConfiguration
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class SpringBootAdminServerApplication {

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

spring-boot-admin-server也做為Eureka Client端橡羞;

增加一個安全配置類:

@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 {
        // @formatter:off
        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();
        // @formatter:on
    }

}

application.yml

server:
  port: 8766
spring:
  application:
    name: admin-server
  security:
    user:
      name: "admin"
      password: "admin"


eureka:
  instance:
      leaseRenewalIntervalInSeconds: 10
      health-check-url-path: /actuator/health
      prefer-ip-address: true
      metadata-map:
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

這其中 security.user.name和security.user.password是訪問admin-server的權(quán)限

security:
    user:
      name: "admin"
      password: "admin"

注意eureka的實(shí)例下要添加 eureka.instance.metadata-map.user.name和eureka.instance.metadata-map.user.name 否則在安全模式下無法注冊

eureka:
  instance:
      metadata-map:
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}

現(xiàn)在啟動訪問 spring-boot-admin-server http://localhost:8766

c2.png
c3.png

下面這張圖中間部分是因?yàn)槲矣没鸷鼮g覽器截圖整個網(wǎng)頁的原因


c4.png

spring-boot-admin-client

完成的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.boot.admin.client</groupId>
    <artifactId>spring-boot-admin-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-admin-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-webflux</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>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-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>

啟動類不用修改

@SpringBootApplication
public class SpringBootAdminClientApplication {

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

application.yml

spring:
  application:
    name: admin-client
  security:
    user:
      name: "client"
      password: "client"

management:
  endpoints:
    web:
      exposure:
        include: "*"
logging:
  file: /var/log/sample-boot-application.log
  pattern:
    file: clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    prefer-ip-address: true
    metadata-map:
      user.name:  ${spring.security.user.name}
      user.password:  ${spring.security.user.password}
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764

security.user.name眯停、 security.user.password 是SpringBootAdmin客戶端的認(rèn)證,同理 Eureka實(shí)例必須加上

eureka:
  instance:
    metadata-map:
      user.name:  ${spring.security.user.name}
      user.password:  ${spring.security.user.password}

現(xiàn)在啟動admin-client卿泽,在admin-server中發(fā)現(xiàn)多了一個


c7.png

如果啟動多個admin-client實(shí)例莺债,會看到在admin-server中admin-client下面有兩個實(shí)例


c8.png

配置郵件通知

當(dāng)客戶端觸發(fā)UP或OFFLINE或其它狀態(tài)時,會發(fā)郵件通知

首先在 spring-boot-admin-server的pom.xml中添加依賴

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

然后在application.yml中添加 配置JavaMailSender

###################
  # 郵件通知配置
  ##################
spring:
   mail:
      host: smtphm.qiye.163.com
      username: # 用戶名
      password: # 密碼
   boot:
     admin:
        notify:
          mail:
          from: # 發(fā)件人
          to: # 收件人
          enabled: true

配置完成又厉,依次啟動eureka-server九府、spring-boot-admin-server、spring-boot-admin-client
當(dāng)admin-client成功注冊時覆致,停掉admin-client侄旬,就會收到一個郵件通知


d1.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市煌妈,隨后出現(xiàn)的幾起案子儡羔,更是在濱河造成了極大的恐慌宣羊,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件汰蜘,死亡現(xiàn)場離奇詭異仇冯,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)族操,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門苛坚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人色难,你說我怎么就攤上這事泼舱。” “怎么了枷莉?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵娇昙,是天一觀的道長。 經(jīng)常有香客問我笤妙,道長冒掌,這世上最難降的妖魔是什么惠豺? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任缩麸,我火速辦了婚禮嵌灰,結(jié)果婚禮上菠齿,老公的妹妹穿的比我還像新娘列吼。我一直安慰自己或衡,他們只是感情好较沪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布逻翁。 她就那樣靜靜地躺著薄嫡,像睡著了一般氧急。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上毫深,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天吩坝,我揣著相機(jī)與錄音,去河邊找鬼哑蔫。 笑死钉寝,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的闸迷。 我是一名探鬼主播嵌纲,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼腥沽!你這毒婦竟也來了逮走?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤今阳,失蹤者是張志新(化名)和其女友劉穎师溅,沒想到半個月后茅信,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡墓臭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年蘸鲸,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窿锉。...
    茶點(diǎn)故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡酌摇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出嗡载,到底是詐尸還是另有隱情妙痹,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布鼻疮,位于F島的核電站,受9級特大地震影響琳轿,放射性物質(zhì)發(fā)生泄漏判沟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一崭篡、第九天 我趴在偏房一處隱蔽的房頂上張望挪哄。 院中可真熱鬧,春花似錦琉闪、人聲如沸迹炼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽斯入。三九已至,卻和暖如春蛀蜜,著一層夾襖步出監(jiān)牢的瞬間刻两,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工滴某, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留磅摹,地道東北人。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓霎奢,卻偏偏與公主長得像户誓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子幕侠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容