Spring Cloud(四) --- config

Spring Cloud Config

隨着線上項目變的日益龐大,每一個項目都散落着各類配置文件,若是採用分佈式的開發模式,須要的配置文件隨着服務增長而不斷增多。某一個基礎服務信息變動,都會引發一系列的更新和重啓,運維苦不堪言也容易出錯。就是在這種背景下,基本上BAT的沒加公司都研發了配置中心,這裏不列舉.html

Spring Cloud Config就是Spring Cloud團隊研發的配置中心,用來爲分佈式系統中的基礎設施和微服務應用提供集中化的外部配置支持,分爲服務端和客戶端. 服務端是一個獨立的微服務應用,用來鏈接配置倉庫並未客戶端提供獲取配置信息等的訪問接口; 客戶端則是微服務中的各個微服務應用,經過指定的配置中心來管理應用資源和業務相關的配置內容,並在啓動的時候從配置中心中加載配置信息.git

Spring Cloud Config 實現的配置中心默認採用 Git 來存儲配置信息,因此使用 Spring Cloud Config 構建的配置服務器,自然就支持對微服務應用配置信息的版本管理,而且能夠經過 Git 客戶端工具來方便的管理和訪問配置內容。固然它也提供了對其餘存儲方式的支持,好比:SVN 倉庫、本地化文件系統。這裏先使用github來演示案例(也可使用碼雲).github

案例步驟

  1. 首先須要github帳號,建立一個倉庫,如今私有倉庫也是免費的,因此就創建一個私有的倉庫,建立三個文件,以下:
    config-git

內容分別是:web

wz.hello=hello in dev

wz.hello=hello in prod

wz.hello=hello in test
  1. 建立config-server項目,步驟以下

引入依賴:算法

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

配置文件:spring

spring.application.name=config-server
# 帳號密碼不予展現
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
spring.cloud.config.server.git.uri=https://github.com/MissWangLove/cloud-config
# git倉庫地址下的相對地址,能夠配置多個,用,分割。本人的在根目錄
spring.cloud.config.server.git.search-paths=
server.port=12000

啓動類添加註解:docker

@SpringBootApplication
@EnableConfigServer
public class CloudConfigDemoApplication {

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

}

以後config server項目就搭建好了,啓動以後就能夠訪問了.http://localhost:12000/wz-config-client-prod.properties就能夠訪問內容了,固然還有另外一種方式是: http://localhost:12000/wz-config.client/prod效果是同樣的.json

倉庫中的配置文件會被轉換成web接口,訪問能夠參照如下的規則:bootstrap

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

以wz-config-client-prod.properties爲例子,它的application是wz-config-client,profile是prod。client會根據填寫的參數來選擇讀取對應的配置。windows

  1. 搭建客戶端,建立項目,剩下步驟以下

引入依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</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-config</artifactId>
</dependency>

application.properties的內容

server.port=13000
spring.application.name=config-client
management.endpoints.web.exposure.include=*
spring.output.ansi.enabled=ALWAYS

bootstrap.properties的內容

# 配置文件的application
spring.cloud.config.name=wz-config-client
# 配置文件的profile
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:12000/
spring.cloud.config.label=master

啓動類無需改變,接下來建立個controller

@RestController
@RefreshScope
public class HelloController {

    @Value("${wz.hello:null}")
    private String hello;

    @GetMapping("/hello")
    public String hello(){
        return hello;
    }
}

這下就能夠啓動訪問了,訪問hello就能夠經過客戶端訪問配置中心了.

上面bootstrapproperties的內容:

  • spring.application.name:對應{application}部分
  • spring.cloud.config.profile:對應{profile}部分
  • spring.cloud.config.label:對應git的分支。若是配置中心使用的是本地存儲,則該參數無用
  • spring.cloud.config.uri:配置中心的具體地址
  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便於擴展爲高可用配置集羣。

注意的是上面這些與spring-cloud相關的屬性必須配置在bootstrap.properties中,config部份內容才能被正確加載。由於config的相關配置會先於application.properties,而bootstrap.properties的加載也是先於application.properties。

還有就是那個actuator和@RefreshScope的做用是刷新,也就是說當github上的配置文件更新了以後,post請求http://localhost:13000/actuator/refresh就能夠起到動態刷新的做用,不用從新啓動項目能夠獲取到更新後的配置.

至於post請求創建本地下載了postman能夠模擬請求.

webhook

WebHook是當某個事件發生時,經過發送http post請求的方式來通知信息接收方。Webhook來監測你在Github.com上的各類事件,最多見的莫過於push事件。若是你設置了一個監測push事件的Webhook,那麼每當你的這個項目有了任何提交,這個Webhook都會被觸發,這時Github就會發送一個HTTP POST請求到你配置好的地址。

如此一來,你就能夠經過這種方式去自動完成一些重複性工做,好比,你能夠用Webhook來自動觸發一些持續集成(CI)工具的運做,好比Travis CI;又或者是經過 Webhook 去部署你的線上服務器。下圖就是github上面的webhook配置。

github

  • Payload URL :觸發後回調的URL
  • Content type :數據格式,兩種通常使用json
  • Secret :用做給POST的body加密的字符串。採用HMAC算法
  • events :觸發的事件列表。
events事件類型 描述
push 倉庫有push時觸發。默認事件
create 當有分支或標籤被建立時觸發
delete 當有分支或標籤被刪除時觸發

svn也有相似的hook機制,每次提交後會觸發post-commit腳本,咱們能夠在這裏寫一些post請求

這樣咱們就能夠利用hook的機制去觸發客戶端的更新,可是當客戶端愈來愈多的時候hook支持的已經不夠優雅,另外每次增長客戶端都須要改動hook也是不現實的。後面會有消息總線bus也會這個功能

Config的配置中心服務化和高可用

前面都是config server和config client的案例,那如何註冊到註冊中心呢,須要什麼改動能實現服務化和高可用呢?下面案例

Spring Cloud Config就到這了.配置中心還有不少,好比Apollo等.

  1. 修改config-server項目

新增eureka依賴

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

添加註解

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class CloudConfigDemoApplication {

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

}

添加配置文件

spring.application.name=config-server
spring.cloud.config.server.git.username=****
spring.cloud.config.server.git.password=****
spring.cloud.config.server.git.uri=https://github.com/MissWangLove/cloud-config
# git倉庫地址下的相對地址,能夠配置多個,用,分割。本人的在根目錄
spring.cloud.config.server.git.search-paths=
server.port=12000
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
  1. 修改config-client項目

添加依賴

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

添加配置:

server.port=13000
spring.application.name=config-client
management.endpoints.web.exposure.include=*
spring.output.ansi.enabled=ALWAYS
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

添加註解:

@SpringBootApplication
@EnableDiscoveryClient
public class CloudConfigClientDemoApplication {

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

}

上面就實現了配置中心服務化(註冊到eureka上)

  1. 再次建立config-server項目,與前一個項目的惟一區別就是端口號不一樣

配置文件的修改:

spring.application.name=config-server
spring.cloud.config.server.git.username=*****
spring.cloud.config.server.git.password=*****
spring.cloud.config.server.git.uri=https://github.com/MissWangLove/cloud-config
# git倉庫地址下的相對地址,能夠配置多個,用,分割。本人的在根目錄
spring.cloud.config.server.git.search-paths=
server.port=12001
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

以後啓動eureka-server,config-server-demo,config-server-demo1,config-client-demo;就能夠看到eureka的註冊中心會有3個服務,兩個config-server,一個config-client,訪問http://localhost:12000/wz-config-client/prod和http://localhost:12001/wz-config-client/prod就能夠看到相同的效果,這樣當一個掛掉,另外一個還能夠正常運行.

前面說了github上的配置文件若是更新的話,須要post發送refresh才能刷新,還有一種就是經過消息總線(bus),這的內容看: http://www.ityouknow.com/springcloud/2017/05/26/springcloud-config-eureka-bus.html,由於這個實現須要消息中間件kafka或者RabbitMQ,須要一個虛擬機或者阿里雲服務器,本人的到期了,等弄好了再進行測試.

配合Bus進行修改和測試

本人在windows上安裝了個docker,以後建立了個rabbitmq容器,這個過程仍是很簡單了,自行百度就好.

  1. 先修改server端吧

添加依賴:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

添加配置文件:

# rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

啓動類添加註解:

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
@RefreshScope
public class CloudConfigDemoApplication {

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

}

上賣弄server端就修改完成,以後在github上修改配置文件,這邊會自動刷新

  1. 修改客戶端

添加依賴:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

添加配置文件:

# rabbitmq
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

添加註解:

@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
public class CloudConfigClientDemoApplication {

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

}

本人在修改的時候還在controller上加了@RefreshScope註解,可是仍是不能起到自動刷新的做用,也就是在github上進行修改以後,必須在客戶端執行curl -X POST http://localhost:13000/actuator/bus-refresh/才能夠起到刷新的效果.

相關文章
相關標籤/搜索