iT邦幫忙

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

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

Day 15 - 組態控制中心 Config Server

建立統一管理的 Config Server

建立一個 config-server 專案

使用到的組件如下

Cloud Config
Config Server

主要依賴是 spring-cloud-config-server

dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

application.properties

spring.cloud.config.server.git.uri=D:/springcloud/config-repo
server.port=8888
  • spring.cloud.config.server.git.uri
    設定應用起動時從哪裡讀取設定檔,可以從Github,也可以從本地Git檔案

    D:/springcloud/config-repo資料夾放的檔案
    application.yml
server.port: ${PORT:${SERVER_PORT:0}}
info.id: ${spring.application.name}
debug: true
spring.sleuth.log.json.enabled: true
logging.pattern.console: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([trace=%X{X-Trace-Id:-},span=%X{X-Span-Id:-}]){yellow} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex"

reservation-service.properties

server.port=${PORT:8000}
message=HELLO world!
spring.cloud.stream.bindings.input=reservations

起動程式
ConfigServerApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

記得加上@EnableConfigServer就可以啟動ConfigServer的功能了

起動 Config-Server 後可以訪問 http://localhost:8888/reservation-service/master 就可以取得設定相關資料

{
  "name": "reservation-service",
  "profiles": [
    "master"
  ],
  "label": null,
  "version": "b017cbcb47700df4ffd7e824614532dd18128040",
  "propertySources": [
    {
      "name": "D:/springcloud/config-repo/reservation-service.properties",
      "source": {
        "server.port": "${PORT:8000}",
        "spring.cloud.stream.bindings.input": "reservations",
        "message": "HELLO world!"
      }
    },
    {
      "name": "D:/springcloud/config-repo/application.yml",
      "source": {
        "server.port": "${PORT:${SERVER_PORT:0}}",
        "info.id": "${spring.application.name}",
        "debug": true,
        "spring.sleuth.log.json.enabled": true,
        "logging.pattern.console": "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([trace=%X{X-Trace-Id:-},span=%X{X-Span-Id:-}]){yellow} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex"
      }
    }
  ]
}

上一篇
Day 14 - 介紹 Spring Cloud
下一篇
Day 16 - 組態客戶端 Config Client
系列文
30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構35
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言