1. 程式人生 > >springcloud之config配置中心-Finchley.SR2版

springcloud之config配置中心-Finchley.SR2版

本篇和大家分享的是springcloud-config配置中心搭建,寫到這裡突然想起自己曾今開源過基於Redis釋出訂閱編寫的一個配置中心,剛看了git星數有點少哈哈,這裡順勢發個連線歡迎大俠們點贊:https://github.com/shenniubuxing3/IConfCenter


  • springcloud版本說明
  • config-server配置中心
  • config-client配置客戶端
  • eureka註冊中心實現配置高可用

springcloud版本說明

由於市面上其版本比較多,版本不一可能造成了讀者嘗試時版本問題,所以這裡指明當前作者寫文章時使用的cloud版本
springboot版本:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

springcloud版本:

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

config-server配置中心

config配置中心主要是用來獲取要釋出的配置檔案資訊,並開放介面被其他呼叫者使用,先上maven配置:

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

通常在程式入口處添加註解 @EnableConfigServer 然後我們還需要知道開放那些配置檔案作為配置資訊來源,因此需要在application.yml檔案中配置如下資訊:

spring:
  application:
    name: config-server  #springcloud-config預設採用application作為name
  cloud:
    config:
      server:
        native:
          search-locations: file:///D:/my_study/study_java/springcloud_3/config-server/src/main/resources/config
  profiles:
    active: native
server:
  port: 4020

這裡我使用本地的配置檔案目錄 src/main/resources/config 來提供配置檔案,如果在windows上其實不用寫file:///,不過官網還特別標註了windows上file後面要多一個 '/' 這裡需要大家注意;這裡我config資料夾下有兩個配置檔案,如下:
image
此刻我們最簡單的配置服務就搭建好了,啟動程式並訪問如下地址:http://10.0.75.1:4020/config-server/conf1,conf0

值得注意的時候這裡用 ',' 分割了下,在瀏覽器中得到如下兩個配置檔案合併後的資訊:
image
可以去掉其中任何一個conf1或者conf0,得到的就是對應配置檔案的資訊,這裡通過瀏覽器訪問的路徑規則是:
http://xx.xx.xx/{application}/{profiles}/{label} label預設null


config-client配置客戶端

同樣先來看pom的config-client對應的配置,這裡多了個web依賴因為我打算在api介面資訊看配置效果

        <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入口處增加註解 @EnableDiscoveryClient 下面就是配置檔案中的資訊了,要注意的是這個版本config-client的config相關配置要放在名稱為 bootstrap.properties 的檔案中(這是預設的配置檔名),如下bootstrap.yml資訊:

spring:
  cloud:
    config:
      name: config-server    #application
      profile: conf1,conf0   #profile 後來者覆蓋,沒有合併
      label:                 #label
      uri: http://10.0.75.1:4020

需要注意的是uri配置的是剛才上面我們訪問的config-server地址,其他的幾個配置對應剛才說的url規則
application.yml配置:

spring:
  application:
    name: config-client
server:
  port: 5020

再來定義個api介面:

@RestController
public class ConfigController {

    @Value("${shenniu.author}")
    private String author;

    @Value("${shenniu.des}")
    private String des;

    @GetMapping("/getPort")
    public String getPort() {
        return "作者:" + author +
                "描述:" + des;
    }
}

此時執行config-client,通過開放的api介面返回對映的配置資訊如下:
image


eureka註冊中心實現配置高可用

高可用通俗來講就是部署多個服務,當某個掛掉的時候其他的頂上去,這裡使用Eureka註冊中心(後面可能會分享關於zk和consul);先建立個eureka-server專案並執行起來:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

在Application入口處新增 @EnableEurekaServer 註解啟動eureka服務,這裡我分別啟動1020,1021,1022三個eureka服務埠,使其自身是高可用的,相關配置:

spring:
  application:
    name: eureka
server:
  port: 1020
eureka:
  instance:
    appname: ${spring.application.name}
  client:
#    register-with-eureka: false  #開啟自動註冊到eureka中心,高可用
#    fetch-registry: false
    service-url:
      defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/
  server:
    eviction-interval-timer-in-ms: 30000  #檢測失效資訊的時間
    enable-self-preservation: false  #關閉自我保護
    use-read-only-response-cache: false

下面需要分別改造下config-server和config-client的配置,可以遵循如下資訊:
config-server
pom增加:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

application.yml增加:

eureka:
  client:
    service-url:
      defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/
  instance:
    appname: ${spring.application.name}
    prefer-ip-address: true

config-client
pom增加:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

bootstrap.yml改造:

spring:
  cloud:
    config:
      name: config-server    #application
      profile: conf1,conf0   #profile 後來者覆蓋,沒有合併
      label:                 #label
#      uri: http://10.0.75.1:4020
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/
  instance:
    appname: ${spring.application.name}
    prefer-ip-address: true

如果可以吧config-server多開幾個埠,都註冊到eureka中心,成功後如下資訊:
image
同樣訪問api介面時得到如下獲取配置成功資訊
image