SpringCloud-Config配置中心搭建保姆級教程

一、分佈式配置中⼼

在使⽤微服務架構開發的項⽬中,每個服務都有⾃⼰的配置⽂件(application.yml),如果將每個服務的配置⽂件直接寫在對應的服務中,存在以下問題:
1. 服務開發完成之後,需要打包部署,配置⽂件也會打包在jar⽂件中,不便於項⽬部署之後的配置修改(在源碼中修改——重新打包——重新上傳——重新運⾏)
2. 微服務架構中服務很多,配置⽂件也很多,分散在不同服務中不便於配置的管理
3. 如果想要對服務進⾏集羣部署,需要打包多個jar⽂件,上傳,運⾏

1、分佈式配置中⼼介紹

2、分佈式配置中⼼搭建

步驟:
1、創建⼀個Git遠程倉庫,⽤來存放配置⽂件
2、搭建分佈式配置中⼼服務器(Spring Cloud Config)Config server
  2.1、連接到配置⽂件的Git倉庫
  2.2、註冊到eureka
3、修改每個服務,刪除application.yml中的所有配置,連接到分佈式配置中⼼

1、創建Git遠程倉庫存儲配置文件

1、創建遠程倉庫:https://gitee.com/qfytao/fmmall-config.git
2、在本地D盤創建 fmmall-config⽬錄,作爲本地存放配置⽂件的⽬錄,在⽬錄中創建files⽬錄
3、使⽤idea打開 fmmall-config ⽬錄
4、項⽬中服務的配置⽂件拷⻉粘貼到files⽬錄,以服務的名稱給配置⽂件命名

5、將本地倉庫push到創建的git遠程倉庫

2、搭建分佈式配置中⼼服務器

1、創建SpringBoot應⽤,添加依賴

2、配置application.yml

server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: 'https://gitee.com/qfytao/fmmall-config.git'
          search-paths: files
          username: 366274379@qq.com
          password: admin123
eureka:
  client:
    service-url:
      defaultZone: 'http://zhangsan:123456@localhost:8761/eureka'

3、在啓動類添加註解

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

4、運⾏測試

訪問 http://localhost:8888/api-order-submit/master

3、配置服務,通過分佈式配置中⼼加載配置⽂件

1、添加依賴

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2、配置服務的application.yml
spring:
  cloud:
    config:
      uri: 'http://localhost:8888'
      name: api-order-submit
      label: master
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章