springcloud之Config初識篇—服務端連接遠程倉庫

配置中心在使用上和註冊中心有些相似之處,也是分爲客戶端和服務端,服務端用來連接我們的遠程倉庫(如git、svn)獲取倉庫中的配置文件,客戶端連接服務端,從服務端獲取配置文件信息。

服務端:

1、添加maven依賴

<!--啓動項目使用-->
        <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>
        <!--指定向eurekaServer上註冊自己,也可以不註冊-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2、添加yml配置

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        #使用git作爲遠程倉庫
        git:
          #這裏是你遠程倉庫配置文件的地址
          uri: https://github.com/***/***
          username:
          password:
          timeout: 15
      #啓用config配置
      discovery:
        enabled: true
  
server:
  port: 8100

eureka:
  instance:
    hostname: config-server
    instance-id: config-server
  client:
    service-url:
      defaultZone: http://eurekaServer:8700/eurekaServer/eureka

3、啓動類上添加註解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {

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

}

git倉庫:

configServer獲取文件信息:

PS:config對獲取配置文件有格式要求文件名要以 {preName}-{suffixName}.yml 這樣的格式才能獲取到。

PS:服務端每次請求都會獲取到遠程倉庫最新的信息。服務端啓動的時候不會獲取遠程倉庫的信息,只在我們調用服務端的時候服務端纔會去遠程倉庫獲取信息。

全路徑:http://host:port/枝幹/文件名稱

枝幹:默認是master,如果請求中不加枝幹就是master,添加的話指定好即可。

如http://xxxx:xx/dev/ {preName}-{suffixName}.yml

獲取的就是遠程倉庫中dev分支上的{preName}-{suffixName}.yml的信息。

獲取文件類型:.properties   .json  .yml

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