Spring Cloud Config Server 配置

本文將示例一個(gè)Spring Cloud Server的配置,以及Client的連接配置,Server端的配置文件從本地文件以及Git倉(cāng)庫(kù)讀取,當(dāng)配置發(fā)生更改時(shí)將通過(guò)Spring Cloud Bus推送事件給Client進(jìn)行配置環(huán)境的刷新,消息隊(duì)列使用的是Kafka,構(gòu)建工具使用Gradle,官方文檔:spring-cloud-config、spring-cloud。先說(shuō)明下Spring的版本:

  • Spring Boot: 2.1.5.RELEASE
  • Spring Cloud: Greenwich.SR1

Config Server

Gradle配置

spring-cloud-starter-bus-kafka是kafka的實(shí)現(xiàn),如果用RabbitMq則換成org.springframework.cloud:spring-cloud-starter-bus-amqp,spring-cloud-config-monitor的項(xiàng)目很簡(jiǎn)單,提供了新的改動(dòng)提交到Github、Gitlab等時(shí)webhook回調(diào)的rest。

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

repositories {
    maven { url 'https://maven.aliyun.com/repository/public' }
    maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
    maven { url 'https://maven.aliyun.com/repository/spring' }
    maven { url 'https://maven.aliyun.com/repository/spring-plugin' }
    mavenCentral()
}

ext {
    set('springCloudVersion', "Greenwich.SR1")
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    implementation 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
    implementation 'org.springframework.cloud:spring-cloud-config-monitor'
    testImplementation 'junit:junit:4.12'
    testCompile('org.springframework.boot:spring-boot-starter-test')
    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
    annotationProcessor("org.projectlombok:lombok")
    compileOnly("org.projectlombok:lombok")
}

項(xiàng)目配置

git倉(cāng)庫(kù)等信息放到bootstrap.yml配置文件中,但是放到application.yml中也可以正常啟動(dòng)的,等有空調(diào)式這個(gè)項(xiàng)目的源碼的時(shí)候再探究區(qū)別,這里先按官方建議的寫(xiě)。需要注意的是,本地配置中,windows系統(tǒng)需要有三個(gè)///,linux系統(tǒng)需要兩個(gè)//。
bootstrap.yml

spring:
  application:
    name: 'Spring Cloud Server'
  cloud:
    config:
      server:
        native:
          search-locations: file:///D:/IDEA/localconfig
          order: 1
        git:
          uri: https://github.com/你的github配置項(xiàng)目地址
          username: 用戶名
          password: 密碼
          skip-ssl-validation: true
          order: 2
          #refresh-rate: 300

application.yml

server:
  port: 8888
  address: 127.0.0.1
spring:
  kafka:
    bootstrap-servers: kafka連接地址
    consumer:
      group-id: SpringCloud-Bus
  profiles:
    active:
      - native
      - git
management:
  endpoints:
    web:
      exposure:
        include: '*'

這里Server端的配置就完成了,只需要在啟動(dòng)類(lèi)加上注解@SpringBootApplication@EnableConfigServer就可以啟動(dòng)配置中心的服務(wù)端。稍微說(shuō)下配置文件名??梢允褂脼g覽器獲取配置中心當(dāng)前的配置,也就是模擬client端獲取配置的結(jié)果。瀏覽器上GET /host:8888/{applicationName}/{profile}/{label},就能獲取到配置文件,相應(yīng)的配置文件命名也是{applicationName}-{profile}-{label}.yml,lable用于標(biāo)記版本(沒(méi)用過(guò))。默認(rèn)不帶profile用default代替。

Config Client

Gradle配置

和Server端的類(lèi)似,去掉monitor和config server,添加client的依賴包。

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

repositories {
    maven { url 'https://maven.aliyun.com/repository/public' }
    maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
    maven { url 'https://maven.aliyun.com/repository/spring' }
    maven { url 'https://maven.aliyun.com/repository/spring-plugin' }
    mavenCentral()
}

ext {
    set('springCloudVersion', "Greenwich.SR1")
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
    testImplementation 'junit:junit:4.12'
    testCompile('org.springframework.boot:spring-boot-starter-test')
    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
    annotationProcessor("org.projectlombok:lombok")
    compileOnly("org.projectlombok:lombok")
}

項(xiàng)目配置

以下配置會(huì)去讀取applicationName是common,prifile為prod的配置文件,例如配置目錄中存在三個(gè)文件:common.yml, common-prod.yml, common-test.yml,則會(huì)返回common-prod.yml,common.yml的內(nèi)容,前者覆蓋后者。
bootstrap.yml

spring:
  cloud:
    config:
      name: common
      profile: prod
      fail-fast: true
      uri: http://127.0.0.1:8888

application.yml

spring:
  kafka:
    bootstrap-servers: kafka連接地址
server:
  address: 127.0.0.1
  port: 8080

management:
  endpoints:
    web:
      exposure:
        include: '*'

啟動(dòng)Spring Boot入口程序,Spring就會(huì)從Config Server獲取配置文件。

自動(dòng)刷新

依賴了兩個(gè)項(xiàng)目Spring Cloud Bus,以及Monitor項(xiàng)目。Spring Cloud Bus允許調(diào)用rest POST config_server_host/actuator/bus-refresh 發(fā)送一個(gè)RefreshRemoteApplicationEvent事件,其他項(xiàng)目接受到事件后會(huì)重新獲取Config Server的配置并刷新需要刷新的組件,不需要我們一個(gè)個(gè)子項(xiàng)目去調(diào)用/refresh的方法,這在微服務(wù)的系統(tǒng)中非常的方便也避免了出紕漏。Spring Cloud Bus解決了項(xiàng)目?jī)?nèi)部自動(dòng)通知的問(wèn)題,而Monitor則解決了外部系統(tǒng)自動(dòng)通知的問(wèn)題,下面以Github為例說(shuō)明:

Github

path=*意思是當(dāng)該倉(cāng)庫(kù)有push時(shí),對(duì)所有的服務(wù)發(fā)送RefreshRemoteApplicationEvent事件,如果需要可以規(guī)定某幾個(gè)service,通過(guò)逗號(hào)分隔。
Webhook配置完成后,當(dāng)有配置提交到倉(cāng)庫(kù)時(shí),GitHub會(huì)調(diào)用rest,Config Server會(huì)發(fā)送RefreshRemoteApplicationEvent事件,Client會(huì)從配置中心重新獲取配置并刷新相關(guān)組件,一切都是全自動(dòng)的,不需要人為的操作或者重啟程序。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容