🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
![](https://img.kancloud.cn/3a/98/3a98395d1e5c139c986ef08045fa6498_993x336.jpg) <br/> Config Server 与 Git 建立通信的步骤如下: [TOC] # 1. 在Git新建配置文件 我在Gitee上创建仓库 config-center,并新建了如下配置文件。 * *`cloud-config-center-3344-dev.yml`* ```yml app: name: cloud-config-client-3344 version: dev-1.0 ``` # 2. 构建配置中心模块 cloud-config-server-3344 模块将作为 config 配置中心,即它就是 Config Server 服务端。 <br/> 步骤如下: **1. 构建配置中心模块:cloud-config-server-3344** **2. 在当前模块的`pom.xml`中添加 config-server 依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- eureka-client 不是必须的 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ... </dependencies> ``` **3. 当前模块的`resources/application.yml`** ```yml server: port: 3344 spring: application: name: cloud-config-center cloud: config: server: git: uri: https://gitee.com/flymini/config-center.git #config-center仓库的HTTPS地址 search-paths: - config-center #仓库名 username: xxxx@163.com #邮箱 password: xxxxx$0 #密码 #default-label: master #默认读取master分支 label: master #读取仓库的master分支 ``` **4. 在当前模块的启动类上添加注解`@EnableConfigServer`** ```java @SpringBootApplication @EnableConfigServer public class ConfigCenterMain3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterMain3344.class, args); } } ``` **5. 测试当前模块是否与Gitee建立通信** (1)启动当前模块。 (2)访问配置文件:http://localhost:3344/cloud-config-center-3344-dev.yml ,响应内容如下。 ```yml app: name: cloud-config-client-3344 version: dev-1.0 ``` (3)访问一个不存在的配置文件:http://localhost:3344/cloud-config-center-3344-not.yml ,响应内容如下。 ```yml {} ``` <br/> 通过测试,说明当前模块已经与Gitee成功建立了通信。 <br/> 读取规则:官网提供了下面几种读取规则。 ``` (1)/{application}/{profile}[/{label}] http://localhost:3344/cloud-config-center-3344/dev/master (2)/{application}-{profile}.yml http://localhost:3344/cloud-config-center-3344-dev.yml (3)/{label)/{application}-{profile}.yml http://localhost:3344/master/cloud-config-center-3344-dev.yml #即使文件是 application.yml, 使用 .properties也可以访问到 (4)/{application}-{profile}.properties http://localhost:3344/cloud-config-center-3344-dev.properties (5)/{label)/{application}-{profile}.properties http://localhost:3344/master/cloud-config-center-3344-dev.properties ```