SpringCloud Config服务端配置
用自己的GitHub账号在GitHub上新建一个名为microservicecloud-config
的新Repository:
获取Repository的http链接
本地硬盘目录上新建git仓库并clone:
在本地D:\44\mySpringCloud\microservicecloud-config里面新建一个application.yml:
spring:
profiles:
active:
- dev
---
spring:
profiles: dev # 开发环境
application:
name: microservicecloud-config-zhang-dev
---
spring:
profiles: test # 测试环境
application:
name: microservicecloud-config-zhang-test
将上一步的YML文件推送到github上:
- git add .
- git commit -m “init yml”
- git push origin master
新建Module模块microservicecloud-config-3344
它即为Cloud的配置中心模块:
引入依赖:
<dependencies>
<!-- Spring Cloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 图形化监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
uri: https://github.com/xxx/microservicecloud-config.git #github仓库名字
主启动类Config_3344_StartSpringCloudApp:
@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
}
}
修改hosts文件,增加映射:
127.0.0.1 config-3344.com
测试通过Config微服务是否可以从GitHub上获取配置内容:
启动微服务3344:
访问:http://config-3344.com:3344/application-dev.yml
访问:http://config-3344.com:3344/application-test.yml
访问:http://config-3344.com:3344/application-xxx.yml(不存在的配置)
配置读取规则
/{application}-{profile}.yml
- http://config-3344.com:3344/application-dev.yml
- http://config-3344.com:3344/application-test.yml
- http://config-3344.com:3344/application-xxx.yml(不存在的配置)
/{application}/{profile}[/{label}]
- http://config-3344.com:3344/application/dev/master
- http://config-3344.com:3344/application/test/master
- http://config-3344.com:3344/application/xxx/master
/{label}/{application}-{profile}.yml
- http://config-3344.com:3344/master/application-dev.yml
- http://config-3344.com:3344/master/application-test.yml