1. 程式人生 > 程式設計 >SpringCloud配置中心Config過程解析

SpringCloud配置中心Config過程解析

1.什麼是配置中心

統一管理配置,怏速切換各個環境的配置

相關產品:

百度的 discont

  https://github.com/knightliao/disconf

阿里的diamand

  https://github.com/takeseem/diamond

springcloud的configs-server:

  http://cloud.spring.io/spring-cloud-config/

2.新增依賴

<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>

3.啟動類添加註解@EnableConfigServer

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {

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

}

4.修改application.yml配置

server:
 port: 9100
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/

spring:
 application:
  name: config-server
 cloud:
  config:
   server:
    git:
     #倉庫地址,去掉git
     uri: https://gitee.com/YTHeng/config_cloud
     #git伺服器登入的使用者名稱和密碼,我這邊使用的是碼雲
     username: [email protected]
     password: 12345678.
     #超時時間
     timeout: 5
     #分支
     default-label: master

5.在碼雲伺服器新建倉庫和檔案

SpringCloud配置中心Config過程解析

6.訪問地址

http://localhost:9100/master/product-service-dev.yml

路徑訪問方式

/{name}-{profiles}. properties
/{name}-{profiles}.yml
/{name}-{profiles}.json
/{label}/{name]-{profiles].yml

name:伺服器名稱

profile:環境名稱,開發、測試、生產

Lable:倉庫分支、預設 master分支

另附:

SpringCloud配置中心Config過程解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。