一起來學SpringCloud 配置中心Config

序言

​ Spring Cloud Config爲分佈式系統中的外部配置,提供了服務器和客戶端支持。使用Config Server,您可以在所有環境中管理應用程序的外部屬性。客戶端和服務器映射的概念與Spring Environment和PropertySource抽象相同,因此它們與Spring應用程序非常契合,但可以與任何以任何語言運行的應用程序一起使用。隨着應用程序通過從開發人員到測試和生產的部署流程,您可以管理這些環境之間的配置,並確定應用程序具有遷移時需要運行的一切。服務器存儲後端的默認實現使用git,因此它輕鬆支持標籤版本的配置環境,以及可用於管理內容的各種工具。可以輕鬆添加替代實現,並使用Spring配置將其插入 。

此文章僅限入門 SpringCloud版本爲 Greenwich

不過現在有阿里的Nacos 和 攜程的 Apollo 替代方案有很多。

使用

我們先創建一個配置中心服務端

首先加入依賴

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

然後是啓動類

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

在然後是yml

spring:
  application:
    name: spring-cloud-action-config
  profiles:
    active:
      - native
  cloud:
    config:
      name: config-server
      enabled: false

server:
  port: 8888

eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
    hostname: localhost
  client:
    service-url:
      #defaultZone 千萬別寫成 default-zone
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

這裏 首先說一下這個 - native 這個意思表明 我們使用的是本地配置中心,所以我們在該項目的resources 下創建的配置文件就可以讓 config client 使用。

我們就在下面創建一個spring-cloud-action-server-order.yml 的配置文件

fulinlin:
  name: my name is localfulinlin

啓動下訪問 http://localhost:8888/spring-cloud-action-server-order.yml 看看網頁是否顯示了yml的內容,如果顯示了則表示沒問題。

然後怎麼使用呢?

在使用config 的配置的依賴中加入

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

然後在resources 目錄下創建一個 bootstrap.yml ,這裏需要注意一下的是,Spring Cloud Config是不認 application.yml 的配置,這也是Cloud官方有說明的,不然配置的 uri屬性是無效的

spring:
  application:
    name: spring-cloud-action-server-order
  cloud:
    config:
#      profile: dev 
      uri: http://localhost:8888
      name: spring-cloud-action-server-order

這裏的 nameprofile 是做多環境使用的 比如我的配置文件叫做 spring-cloud-action-server-order-dev.yml 那麼就應該在profile 裏寫上 dev。

然後在使用 config 的服務裏寫個方法測試下唄

    @Value("${fulinlin.name}")
    String fulinlin;

    @GetMapping("/getConfig")
    public String getOrder() {
        return fulinlin;
    }

查看控制檯是否打印 my name is localfulinlin

集成git

yml裏稍做修改即可

spring:
  application:
    name: spring-cloud-action-config
  cloud:
    config:
      label: master
      server:
        git:
          uri: https://gitlab.com/fulinlin/spring-cloud-action-gitlab-config.git
          search-paths: respo
          username: 
          password: 

這個search-paths 就是倉庫的目錄 respo 就是倉庫目錄下的 文件夾

然後在使用config的時候

spring:
  application:
    name: spring-cloud-action-server-order
  cloud:
    config:
      name: spring-cloud-action-server-order
#      profile: dev       
      label: master
      uri: http://localhost:8888

這樣配置即可。

附:HTTP 請求地址和資源文件映射

  • http://ip:port/{application}/{profile}[/{label}]
  • http://ip:port/{application}-{profile}.yml
  • http://ip:port/{label}/{application}-{profile}.yml
  • http://ip:port/{application}-{profile}.properties
  • http://ip:port/{label}/{application}-{profile}.properties

此文章僅限入門,切記啊不會用找不到多去官網看文檔!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章