Spring Cloud 学习笔记 ——Spring Cloud Config 的 Client 端搭建

Config Client

一、基本搭建

1 .首先创建一个 Spring Boot 项目作为 module ,项目名为config-client
在这里插入图片描述
依赖需要添加 Web、Config Client 依赖
在这里插入图片描述

项目创建完成
在这里插入图片描述

2.项目创建成功后,再 resources 目录添加 bootstraps.properties 资源文件
在这里插入图片描述
配置 bootstraps.properties

# 下面三行配置分别对应 config-server 中的{application}、{profile}、{label}占位符
spring.application.name=client1
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8081
server.port=8082

前三行配置就是Spring Cloud 学习笔记 ——Spring Cloud Config 的 Server端搭建中提到的如下内容相对应:

访问 Git 上的配置文件接口名:

  • 1./{application}/{profile}/{label}
  • 2./{application}-{profile}.yml
  • 3./{application}-{profile}.properties
  • 4./{label}/{application}-{profile}.properties
  • 5./{label}/{application}-{profile}.yml
    application 表示配置文件名
    profile 表示配置文件的 profile,例如 test、dev、prod,与 SpringBoot 切换配置文件的 profile 是一样的
    label表示git 的分支,参数可选,默认就是 master

spring.cloud.config.uri是 config-server 的服务地址;这里假设我们已经有了 Git 上 client1-dev.properties 中的内容,写一个接口测试一下;

  • 3 编写接口
@RestController
@RefreshScope
public class HelloController {
    @Value("${javaboy}")
    String javaboy;
    @GetMapping("/hello")
    public String hello(){
        return javaboy;
    }
}
  • 4 ,启动 config-server 服务 和 client1 服务(config-client)
    在这里插入图片描述

  • 启动成功后访问:http://localhost:8082/hello接口
    -
    请求成功,访问的内容就是 git 上的 client1-dev.properties;其实是再 client1 项目启动时访问 Config Server 获取的,这就是 Config Client 的使用;
    如果有其他微服务也要连接 Config Server ,只需要添加 Config Client 依赖,像上面一样配置一下即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值