SpringCloud Netflix (六):Config 配置中心


------------恢復內容開始------------

 

SpringCloud Config 配置中心

Config 配置中心

Spring Cloud Config為分布式系統中的外部化配置提供服務器端和客戶端支持。使用Config Server,您可以在中心位置管理所有環境中應用程序的外部屬性。客戶端和服務器上的概念都與Spring EnvironmentPropertySource抽象映射相同,因此它們非常適合Spring應用程序,但可以與以任何語言運行的任何應用程序一起使用。在應用程序從開發人員到測試人員再到生產人員的整個部署過程中,您可以管理這些環境之間的配置,並確保應用程序具有它們遷移時所需的一切。服務器存儲后端的默認實現使用git,因此它輕松支持帶標簽的配置環境版本,並且可以通過各種工具來訪問這些內容來管理內容。添加替代實現並將其插入Spring配置很容易。

 

Config配置中心實例

Config-Server

1.新建一個配置中心服務 springcloud-config-6001,並添加依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    <!--配置中心服務需要添加進注冊中心,所以要加eureka-client依賴-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
</dependencies>

2.創建配置文件 application.yml

server:
  port: 6001

spring:
  application:
    name: springcloud-config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/little_gofy/config-server.git #git倉庫的html地址
          #如果配置文件放在子目類下,則需在search-paths配置添加子目錄路徑
          search-paths:
          #如果倉庫為私有,則需在username和password配置添加用戶名和密碼
          username:
          password: 

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

3.在啟動類上添加注解@EnableConfigServer,允許配置服務

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

4.在git倉庫創建配置文件application.yml,隨便添加點配置

spring:
  profiles: dev
  application:
    name: config-dev
my:
  name: gofy-dev
    
---
spring:
  profiles: test
  application:
    name: config-test
my:
  name: gofy-test

開啟注冊中心服務和配置中心服務, 訪問 localhost:6001/application-dev.ymllocalhost:6001/application-test.yml, 返回git倉庫配置文件對應模式的配置.

HTTP服務具有以下形式的資源:

# application:配置文件名, profile:配置的模式, label:配置文件所在分支
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

如果報以下異常, 把 spring-cloud-config-server 降到 2.1.0.RELEASE 即可

NoClassDefFoundError: org/springframework/cloud/config/environment/PropertyValueDescriptor

Config-Client

1.創建一個客戶端 springcloud-config-client, 並添加依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
</dependencies>

2.創建 bootstrap.yml

spring:
  cloud:
    config:
      discovery:
        service-id: springcloud-config #指定配置中心,對應配置中心服務名
        enabled: true #開啟配置信息發現
      label: master #配置文件所在分支, 默認為master
      profile: dev #配置的模式, 多個用逗號分隔

eureka:
  client:
    service-url:
      defaultZone: http://server1:7001/eureka/
    register-with-eureka: false

再創建一個 application.yml

server:
  port: 80

spring:
  application:
    name: config-client

注意, 不要把bootstrap.yml的配置寫在application.yml里, 因為bootstrap.yml的相關配置會先於application.yml,而bootstrap.yml的加載也是先於application.yml。需要注意的是eureka.client.serviceUrl.defaultZone要配置在bootstrap.yml,不然客戶端是無法獲取配置中心參數的,會啟動失敗!

3.創建Controller調用配置中心的配置

@RestController
public class ConfigController {
    @Value("${my.name}") //注入配置中心里的配置文件的my.name值
    private String myName;

    @RequestMapping("/my")
    public String getMyName(){
        return myName;
    }
}

4.創建啟動類

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

啟動注冊中心、配置中心服務和客戶端, 訪問 localhost/my , 返回的是dev模式配置的值gofy-dev .

當我們把bootstrap.yml里的profile值改為test, 則返回的是test模式配置的值gofy-test .

如果報以下異常, 是因為配置中心服務還沒有注冊到注冊中心, 等一會再啟動客戶端就行了. 如果還報錯, 請檢查配置是否和上面的配置一致.

java.lang.IllegalStateException: No instances found of configserver (springcloud-config)

 

修改Config-Server本地倉庫位置

使用基於VCS的后端(git,svn),文件被檢出並克隆到本地文件系統。默認情況下,它們以config-repo-為前綴放在系統臨時目錄中。例如,在Linux上,它可能是/tmp/config-repo-,在Window上,它是/Temp/config-repo-。一些操作系統通常會清除臨時目錄。這可能導致意外行為,例如缺少屬性。

為避免此問題,請通過將spring.cloud.config.server.git.basedirspring.cloud.config.server.svn.basedir設置為不在系統臨時結構中的目錄來更改Config Server使用的目錄。

spring:
  cloud:
    config:
      server:
        git:
          basedir: E:/local-config-repo

 

我的個人博客站


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM