SpringCloud 配置中心

1、建立Config配置中心項目

1.添加依賴git

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

2.啓動類,須要添加@EnableConfigServerspring

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

/**
 * @author fusw
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterApplication {

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

}

3.配置文件bootstrap

eureka.instance.appname=base-iov-config
security.user.name=test
security.user.password=test
# GitLab 的配置方式,必須有 .git 後綴
# 配置默認 git 倉庫的地址
spring.cloud.config.server.git.uri=http://xxxx/config-repo.git
# git 倉庫地址下的相對地址,能夠配置多個,用「,」分割
spring.cloud.config.server.git.search-paths=*
#配置中心clone倉庫配置文件後存放的地址
spring.cloud.config.server.git.basedir=/data/deploy/config-repo
spring.cloud.config.server.git.force-pull=true
# git 倉庫的帳號
spring.cloud.config.server.git.username=test
# git 倉庫的密碼
spring.cloud.config.server.git.password=test



# 配置 其它git 倉庫的地址 spring.cloud.config.server.git.repos.x.uri, iot爲例
spring.cloud.config.server.git.repos.iot.uri=http://xxxx/iot/config-repo.git
spring.cloud.config.server.git.repos.iot.search-paths=*
spring.cloud.config.server.git.repos.iot.basedir=/data/deploy/config-repo
spring.cloud.config.server.git.repos.iot.force-pull=true
# git 倉庫的帳號
spring.cloud.config.server.git.repos.iot.username=test
# git 倉庫的密碼
spring.cloud.config.server.git.repos.iot.password=test
#前綴必定要配置,用來和默認倉庫區分
spring.cloud.config.server.git.repos.iot.pattern=Iot*

#註冊中心
eureka.client.serviceUrl.defaultZone=http://xxx/eureka/

2、git 存放配置的倉庫

建立一個git倉庫用來存放各項目配置,能夠在項目一級目錄根據項目建立文件夾(各項目文件夾的名稱能夠隨便起,Config配置中心只根據配置文件名找配置),而後各個項目文件夾存放不一樣環境的配置文件,
例如:
Iot-TestProject-dev.yml
Iot-TestProject-prd.yml
Iot-TestProject-test.ymlapp


最好不要放置test.yml相似的默認配置,若是在各個項目配置文件中沒有用spring.profiles.active指明使用的配置文件,那麼會加載默認的配置文件。code

3、各個SpringCloud的項目配置接入配置中心

在resource下的bootstrap.properties配置文件中,配置中心的相關配置以下server

# 使用默認倉庫的配置文件
spring.cloud.config.name=TestProject
# 使用iot廠庫的配置文件
#spring.cloud.config.name=Iot-TestProject
#使用哪一個環境的配置文件,和上面的配置配合,決定了使用哪個配置文件:TestProject -test.yml
spring.cloud.config.profile=test


# 對應 存放配置文件倉庫的git分支
spring.cloud.config.label=master

spring.cloud.config.username=test
spring.cloud.config.password=test

# 開啓 Config 服務發現支持
spring.cloud.config.discovery.enabled=true
# 指定 Server 端的 name,也就是 Server 端 spring.application.name 的值
spring.cloud.config.discovery.serviceId=base-iov-config
相關文章
相關標籤/搜索