0062-Config Client搭建

本文详细介绍了如何搭建微服务架构下的Config Client,通过连接Config Server获取远程配置。包括了必要的pom依赖、bootstrap.yml配置及主启动类的设置,并演示了如何通过Controller读取配置。

1. 前言

上一节Config Server已经获取到远程的配置文件,Client端需要连上Server端,从Server端获取配置,真实环境中所有的微服务都是Client端。

2. Client端搭建

2.1 pom依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>
2.2 bootstrap.yml配置文件

bootstrap.yml优先级比application.yml高

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:9800 #服务端地址
      fail-fast: true
      profile: dev    # 启用的profile
      label: release1.0 # 启用的分支
2.3 主启动类
 */
@SpringBootApplication
@RestController
public class ConfigClient9900Application {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClient9900Application.class, args);
    }

    // 配置文件注入
    @Value("${foo}")
    private String foo;
    @Value("${label:master}")
    private String label;

    @GetMapping("/getconfig")
    public String getConfig() {
        return "Current label is " + label + ", and the foo content is " + foo + ".";
    }
}

3. 测试

打开浏览器访问
http://localhost:9900/getconfig出现下面字符串则表示读取成功

Current label is release1.0, and the foo content is dev foo version.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值