新建项目,创建config-server
和之前的创建项目步骤一样,不懂得看之前实战,因为搭建的是服务器不是客户端,需要选择ConfigServer
1.注意依赖,这两个一定要有
<!--配置中心-->
<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>
2.启动类添加注解
@EnableConfigServer
3.配置文件
#端口号
server:
port: 9100
#服务名称
spring:
application:
name: config-server
#指定注册中心
eureka:
client:
sericeUrl:
defaultZone: http://localhost:8761/eureka/
4.启动后报错

意思就是还缺少git服务,配置中心肯定要去读取某个数据源才可以
使用git服务器结合Config搭建分布式配置中心
1.默认使用git存储配置中心
使用git服务器,可以自己搭建gitlab服务器或者使用github、开源中国git、阿里云git
博主这里使用的是码云gitee
1)创建一个gittee仓库
2)复制HTTPS仓库地址
2.配置文件添加配置
#git配置
cloud:
config:
server:
git:
uri: 复制HTTPS的仓库路径
username: git用户名
password: git密码
3.将商品服务配置添加到git仓库中
完成后之后提交
4.启动config-server服务
注意访问规则,官方文档也有说明:
将创建好的配置文件名复制,也就是我上面的product-service.yml
页面访问:http://localhost:9100/product-service.yml
问题:报错404,后台错误日志是用户身份验证不正确。将私有仓库改为公开,这个问题博主还没有具体找到原因,等知道后再修改文章
正常访问到的就是git仓库里的配置文件信息了
重点:访问格式案例
配置文件名称是product-service 这里的-是不需要有的,因为访问格式采用一种后缀的形式,如果使用_则找不到配置文件
/{name}-{profiles}.properties
/{name}-{profiles}.yml 当前使用的这种访问格式 localhost:9100/master/product-service-t.yml这样也是可以访问到的 改成对应格式都可以访问到 通过一个后缀就可以实现各种配置格式
/{name}-{profiles}.json
/{label}/{name}-{profiles}.yml
name 服务器名称
profile 环境名称,开发、测试、生产
lable 仓库分支,默认master分支
git可以采用多分支的方式去测试,这里不做演示了
超时问题
配置中心作为一个微服务也需要配置超时时间,如果不配置的话配置中心挂掉请求一直得不到返回
#git服务器配置
cloud:
config:
server:
git:
uri: https://gitee.com/walk-under-the-pavilion/config_cloud.git
username: [email protected]
password: 3539643q..
timeout: 5 #超时时间 秒
default-label: master #默认访问分支 master