根据前面的config原理图,我们需要建立Client端调用server端,最终实现client端获取远程git配置信息;
为了后面演示方便,我们提交三个配置文件到远程git库;
application.yml:
spring: profiles: active: - dev --- spring: profiles: dev port: 111 --- spring: profiles: test port: 222
crm-dev.yml
port: 7777
crm-test.yml
profile: 88888
然后我们新建一个module microservice-config-client-5001
加下依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
我们项目启动的时候,就要调用server config端,获取配置信息,所以这里要bootstrap.yml配置文件,优先级最高:
spring: application: name: application-dev cloud: config: name: crm uri: http://configserver.java1234.com:4001 # 配置configserver地址 profile: dev # 级别 label: master # 分支 git中 默认master fail-fast: true
application.yml:
server: port: 5001 context-path: /
再搞一个 ConfigClientController 类 测试显示端口:
package com.java1234.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientController { @Value("${port}") private String port; @GetMapping("/getPort") public String getPort() { return port; } public void setPort(String port) { this.port = port; } }
启动类:ConfigClientApplication_5001
package com.java1234; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClientApplication_5001 { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication_5001.class, args); } }
最后 本地hosts我们加给配置:
127.0.0.1 client-config.java1234.com
我们启动项目:然后页面访问:http://client-config.java1234.com:5001/getPort 即可获取远程端口配置信息;
下一篇:Config整合Eureka