0%

SpringCloud Config-server

pom.xml

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server:
port: 7006

spring:
application:
name: config-server
cloud:
config:
server:
git:
# 配置git仓库的地址
uri: https://github.com/Delena1988/spring-cloud-config-repo
# git仓库地址下的相对地址,可以配置多个,用,分割。
search-paths: /**
# git仓库的账号(私有库必填)
username:
# git仓库的密码(私有库必填)
password:
# 配置git仓库的分支
label: master

ConfigServerApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.linjian.configserver;

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

//开启配置服务器
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

}

Github创建repository spring-cloud-config-repo

创建配置文件

spring-cloud-config-dev.yml

spring-cloud-config-test.yml

spring-cloud-config-pro.yml

测试:

  1. 启动Config-server

  2. 访问 http://localhost:7006/master/spring-cloud-config-dev.yml

  3. 访问 http://localhost:7006/master/spring-cloud-config-test.yml

  4. 访问 http://localhost:7006/master/spring-cloud-config-pro.yml