1config GitHub模式 · SpringCloud微服务实战 · 看云
导航
本节代码地址
1. 新建模块fw-cloud-config-native-server
1.1 maven 配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</dependencies>
1.2 新建启动类
需要加入@EnableConfigServer
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class FwConfigNativeServerApplication {
public static void main(String[] args) {
SpringApplication.run(FwConfigNativeServerApplication.class, args);
}
}
1.3 配置信息-Git 模式
server:
port: 8778
spring:
application:
name: fw-config-server
cloud:
config:
server:
git:
uri: https://github.com/xuyisu/fw-sping-cloud.git #设置自己的git地址
username: 账号
password: 密码
search-paths: fw-cloud-config-repo #仓库里面如果没有见文件夹,不需要加此配置
eureka:#已服务发现的方式可以加,否则不用加
client:
service-url:
defaultZone: http://localhost:8761/eureka
1.4 在仓库中添加配置信息
1.4.1 fw-test.yml 默认
version: default-1.0
1.4.2 fw-test-dev.yml 开发
version: dev-1.0
1.4.3 fw-test-prod.yml 生产
version: prod-1.0
1.4.4 fw-test-uat.yml uat
version: uat-1.0
1.4.5 fw-register-eureka-client.yml 项目演示
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
logging:
level:
com.yisu: debug
version: eureka-2.0
1.5 项目启动
浏览器或Postman 输入地址localhost:8778/fw-test/default
返回结果如下
{
"name": "fw-test",
"profiles": [
"default"
],
"label": null,
"version": "05bdb20957cdb159ccc8b163010025f66ea0f0c6",
"state": null,
"propertySources": [
{
"name": "https://github.com/xuyisu/fw-sping-cloud.git/fw-cloud-config-repo/fw-test.yml",
"source": {
"version": "default-1.0"
}
}
]
}
可以看到返回的JSON中应用名是fw-test,环境是默认的,因为请求的是default,分支名label是null,默认是master,version 就是Git CommitId,propertySources里面就是配置的属性
1.6 测试读取配置(其它几个类同)
浏览器或Postman 输入localhost:8778/fw-register-eureka-client.yml
1.7 访问配置信息的URL与配置文件的映射关系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
- {application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。
- {profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-uat.yml、application-prod.yml。
- {label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。
1.8 服务端模式
不管服务端使用什么模式,对客户端均不受影响
