SpringCloud與微服務Ⅹ --- SpringCloud Config分佈式配置中心

一.SpringCloud Config是什麼java

分佈式系統面臨的問題 --- 配置問題mysql

微服務意味着要將單體應用中的業務拆分紅一個個子服務,每一個服務的粒度相對較小,所以系統中會出現大量的服務。因爲每一個服務都須要必要的配置信息才能運行,因此一套集中式的、動態的配置管理設施是必不可少的。SpringCloud提供了ConfigServer來解決這個問題,咱們每個微服務本身帶着一個application.yml,上百個配置文件的管理。git

SpringCloud Config是什麼github

SpringCloud Config能夠從github上獲取配置服務信息,爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不一樣微服務應用的全部環境提供了一個中心化的外部配置web

 

二.SpringCloud Config的工做原理spring

SpringCloud Config分爲服務端和客戶端兩部分。sql

服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來鏈接配置服務器併爲客戶端提供獲取配置信息,加密/解密信息等訪問接口。數據庫

客戶端則是經過指定的配置中心來管理應用資源,以及業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,而且能夠經過git客戶端工具來方便的管理和訪問配置內容。apache

 

三.SpringCloud Config能作什麼bootstrap

1.集中管理配置文件

2.不一樣環境不一樣配置,動態化的配置更新,分環境部署好比dev/test/prod/beta/release

3.運行期間動態調整配置,再也不須要每一個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置本身的信息。

4.當配置發生變更時,服務不須要重啓便可感知到配置的變化並應用新的配置

5.將配置信息以REST接口的形式暴露

因爲SpringCloud Config默認使用GIT來存儲配置文件(也支持SVN和本地文件),但最推薦的仍是git,並且使用http/https訪問的形式。

 

四.SpringCloud Config 服務端

1.在github上新建一個倉庫microservice-config

2.在本地拉取該項目,使用 git clone git@github.com:SillyBoy007/microservice-config.git

3.在該項目裏新建一個application.yml

4.編寫yml文件,並保存爲UTF-8格式

spring: profiles: active: - dev --- spring: profiles: dev #開發環境 application: name: microservice-config-wang-dev --- spring: profiles: test #測試環境 application: name: microservice-config-wang-test

5.上傳yml文件到github

查看github的Code發現上面已經有了該文件。那麼咱們該如何讀取github上的文件配置信息呢?

搭建ConfigServer

建立一個模塊microservice-config-3344。

pom文件

<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>

    <parent>
        <groupId>com.wang.springcloud</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>microservice-config-3344</artifactId>


    <dependencies>
        <!-- springCloud Config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- 避免Config的Git插件報錯:org/eclipse/jgit/api/TransportConfigCallback -->
        <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>4.10.0.201712302008-r</version>
        </dependency>
        <!-- 圖形化監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 熔斷 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- 熱部署插件 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

yml文件配置

server:
port: 3344

spring:
application:
name: microservice-config
cloud:
config:
server:
git:
uri: git@github.com:SillyBoy007/microservice-config.git #git倉庫地址
search-paths: microservice-config-learn #git項目的目錄

主啓動類註解

@SpringBootApplication @EnableConfigServer public class SpringCloudConfig3344 { public static void main(String[] args) { SpringApplication.run(SpringCloudConfig3344.class,args); } }

修改系統host文件,增長映射

127.0.0.1 config3344.com

測試經過microservice-config-3344是否能夠從github上獲取配置內容

啓動microservice-config-3344項目,分別訪問下列的地址

http://config3344.com:3344/application-dev.yml

http://config3344.com:3344/application-test.yml

http://config3344.com:3344/application-dasdd.yml

配置讀取規則

1./{application}-{profile}.yml

http://config3344.com:3344/application-dev.yml

2./{application}/{profile}[/{label}]

http://config3344.com:3344/applicaiton/test/master

3./label/{application}-{profile}.yml

http://config3344.com:3344/master/application-test.yml

 

五.SpringCloud Config 客戶端

1.在microservice-config項目下新建一個microservice-config-client.yml文件(以UTF-8格式保存)。

spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservice-config-client eureka: client: service-url: defaultZone: http://eureka-dev.com:7001/eureka/

--- server: port: 8202 spring: profiles: test application: name: microservice-config-client eureka: client: service-url: defaultZone: http://eureka-test.com:7001/eureka/

2.將上述的文件上傳到github上。

3.新建microservice-config-client-3355項目

pom文件:

<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>

    <parent>
        <groupId>com.wang.springcloud</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>microservice-config-client-3355</artifactId>

    <dependencies>
        <!-- SpringCloud Config客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

新建bootstrap.yml

application.yml是用戶級的資源配置項

bootstrap.yml是系統級的,優先級更高

SpringCloud會建立一個‘BootStrap Context’,做爲Spring應用的Application Context的父上下文。初始化的時候‘BootStrap Context’負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個從外部獲取的'Environment'。'BootStrap'屬性有高優先級,默認狀況下,它們不會被本地配置覆蓋。'BootStrap context'和'Application Context'有着不一樣的約定, 因此新增了一個'bootstrap.yml'文件,保證'BootStrap Context'和'Application Context'配置的分離。

spring: cloud: config: name: microservice-config-client #須要從github上讀取的資源名稱,沒有yml後綴名 profile: dev label: master uri: http://config3344.com:3344 #本微服務啓動後先去找3344號服務,經過SpringCloud Config獲取GitHub的服務地址 

新建application.yml

spring: application: name: microservice-config-client

host域名映射

127.0.0.1 client-config.com

主啓動類

package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClient3355 { public static void main(String[] args) { SpringApplication.run(ConfigClient3355.class,args); } }

新建RestController類測試

package com.wang.springcloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientRestController { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @RequestMapping public String getConfig(){ return "applicationName:"+applicationName+"\t eurekaServers:"+eurekaServers+"\t port:"+port; } }

啓動測試:

1.先啓動microservice-config-3344項目,再啓動microservice-config-client-3355項目。

2.訪問http://client-config.com:8201/http://client-config.com:8201/config

3.修改bootstrap.yml,將環境profile切換爲test

4.訪問http://client-config.com:8202/ , http://client-config.com:8202/config

 

六.SpringCloud Config 實際應用

修改microservice-config項目

1.新建microservice-config-eureka-client.yml

spring: profiles: active: - dev --- server: port: 7001 spring: profiles: dev application: name: microservice-config-eureka-client ​ eureka: instance: hostname: eureka7001.com #eureka實例的主機名 client: register-with-eureka: false #不把本身註冊到eureka上 fetch-registry: false #不從eureka上來獲取服務的註冊信息 service-url: defaultZone: http://eureka7001.com:7001/eureka/
--- ​ server: port: 7001 ​ spring: profiles: test application: name: microservice-config-eureka-client eureka: instance: hostname: eureka7001.com #eureka實例的主機名 client: register-with-eureka: false #不把本身註冊到eureka上 fetch-registry: false #不從eureka上來獲取服務的註冊信息 service-url: defaultZone: http://eureka7001.com:7001/eureka/

2.新建microservice-config-dept-client.yml

spring: profiles: active: - dev --- server: port: 8001 spring: profiles: dev application: name: microservice-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource #數據源類型 driver-class-name: org.gjt.mm.mysql.Driver #數據庫驅動 url: jdbc:mysql://localhost:3306/cloudDB01 #數據庫url
 username: root password: 123456 dbcp2: min-idle: 5 #數據庫鏈接池的最小維持鏈接數 initial-size: 5 #初始化鏈接數 max-total: 5 #最大鏈接數 max-wait-millis: 200 #等待鏈接獲取的最大超時時間 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路徑 type-aliases-package: com.wang.springcloud.entities #全部entity別名類所在包 mapper-locations: classpath:mybatis/mapper/**/*.xml #mapper映射文件 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka --- server: port: 8002 spring: profiles: test application: name: microservice-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource #數據源類型 driver-class-name: org.gjt.mm.mysql.Driver #數據庫驅動 url: jdbc:mysql://localhost:3306/cloudDB02 #數據庫url username: root password: 123456 dbcp2: min-idle: 5 #數據庫鏈接池的最小維持鏈接數 initial-size: 5 #初始化鏈接數 max-total: 5 #最大鏈接數 max-wait-millis: 200 #等待鏈接獲取的最大超時時間 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路徑 type-aliases-package: com.wang.springcloud.entities #全部entity別名類所在包 mapper-locations: classpath:mybatis/mapper/**/*.xml #mapper映射文件 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka

3.上傳上述兩個新文件到github

4.新建microservice-config-eureka-server-7001項目

pom文件

<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>

    <parent>
        <groupId>com.wang.springcloud</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-config-eureka-server-7001</artifactId>
    
    <dependencies>
        <!-- SpringCloudConfig配置 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- 熱部署插件 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

bootstrap.yml

spring: cloud: config: name: microservice-config-eureka-server profile: dev label: master uri: http://config3344.com:3344 #本微服務啓動後先去找3344號服務,經過SpringCloud Config獲取GitHub的服務地址

application.yml

spring: application: name: microservice-config-eureka-server

主啓動類

package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class ConfigEurekaServer7001 { public static void main(String[] args) { SpringApplication.run(ConfigEurekaServer7001.class,args); } }

4.新建microservice-config-dept-client-8001項目

pom文件

<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>

    <parent>
        <groupId>com.wang.springcloud</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>microservice-config-dept-client-8001</artifactId>

    <dependencies>
        <!-- SpringCloudConfig配置 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wang.springcloud</groupId>
            <artifactId>microservice-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

bootstrap.yml

spring: cloud: config: name: microservice-config-dept-client #須要從github上讀取的資源名稱,注意沒有yml後綴名 #profile配置是什麼就取什麼配置dev or test profile: dev #配置環境 #profile: test label: master uri: http://config3344.com:3344 #SpringCloudConfig獲取的服務地址

application.yml

spring: application: name: microservice-config-dept-client

4.拷貝microservice-provider-dept-8001項目的mybatis資源文件以及mapper、service業務代碼

5.啓動測試

先啓動microservice-config-3344,而後啓動microservice-config-eureka-client,最後啓動microservice-config-dept-client-8001,訪問http://localhost:7001/,能夠看到8001服務已經註冊到eureka中了,訪問http://localhost:8001/dept/get/1 , 而後切換test環境,訪問http://localhost:8002/dept/get/1,能夠看到數據的來源也切換了。

相關文章
相關標籤/搜索