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

被折叠的 条评论
为什么被折叠?



