iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 10
0
Software Development

30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構系列 第 10

Day 10 - 上線前增加 SpringBoot Admin 來做監控

因為很多時候,你根本不會知道你服務在哪台實體機上執行,也不知道現在用到哪一版
所以在微服務系統架構中...監控管理是非常重要的一環

其實在 SpringBoot中有內建 提供非常多的資訊跟一些簡單的服務管理功能
後來有一個團隊做了不錯看的 UI 出來, 就是 SpringBoot Admin 也跟 Spring Cloud 做了不錯的整合, 就算不用 SpringCloud 單獨使用也非常好用.

Server 端

首先我再建立個新專案叫 admin
專案的 build.gradle 如下

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile 'de.codecentric:spring-boot-admin-server:1.5.5'
    compile 'de.codecentric:spring-boot-admin-server-ui:1.5.5'
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

我們加上了 spring-boot-admin-server 這是監控端, spring-boot-admin-server-ui 就是 UI 啦

然後我們啟動程式加上

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

@EnableAdminServer
@SpringBootApplication
public class AdminApplication {

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

加上了 EnableAdminServer 就可以了
啟動之後 打開 http://localhost:8080
首頁

然後當你用 Chrome 打開頁面的時候, 會跳出下面通知接收的詢問
通知接收
這邊我們先按允許

Client 端

客戶端更簡單, 只需要引入 spring-boot-admin-starter-client 套件

dependencies {
    compile 'de.codecentric:spring-boot-admin-starter-client:1.5.5'
}

application.yml 我們改一下 port 然後加上 client 的配置讓他知道 server 在哪
還有我們給應用程式一個名稱 bookservice 之後比較好辨識

spring:
  application:
    name: bookservice

server:
  port: 8081

spring.boot.admin.url: http://localhost:8080
management.security.enabled: false

接下來只要啟動程式就可以了, 你可以看到我們
畫面中我們的 bookservice 是啟動狀態, 同時狀態變動的時候 springboot admin 也會透過 chrome 的機制來通知我們
接收註冊

是不是感覺安心很多了呢? 至少不會發生服務掛掉一天一夜沒人發現這種鳥事 XD

Client 端 安全機制

但是 Client 端的 port 跟 api 是一樣的, 所以外面的人也可以透過這個 port 來監控你的應用

所以我們再做一點手腳來保護

簡單一點的呢....就直接加上

compile('org.springframework.boot:spring-boot-starter-security')

預設呢....就是所有 API 全部都需要授權(包含監控資訊)

所以稍微加上自己的配置 WebSecurityConfig.java, 作用在我們自己寫的 api 開頭 url 開放(之後會再用我們自訂的管理方式), 其他都需要授權

package com.example.book;

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

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable();
        http
                .authorizeRequests()
                .antMatchers("/api/**")
                .permitAll();
        http
                .authorizeRequests()
                .antMatchers("/**")
                .authenticated();
        http.httpBasic();
    }
}

然後配置檔 application.yml 就改成 這樣

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_DELAY=-1
    initialize: true
    sql-script-encoding: UTF-8
  application:
    name: bookservice
  boot:
    admin:
      url: http://localhost:8080
      client:
        metadata:
          user.name: ${security.user.name}
          user.password: ${security.user.password}

server:
  port: 8081

management.security.enabled: true

security:
  user:
    name: admin
    password: admin123

management.security.enabled 一定要開啟, 不開啟等於沒有生效喔
security 這邊就是 basicAuth
admin.client 這邊就是讓 server 端可以透過 basicAuth 正常存取 Client 的資訊


上一篇
Day 09 - yaml 配置規劃
下一篇
Day 11 - 如何客製 Admin 的通知(使用 Line )
系列文
30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構35
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言