Spring Cloud——Config Client

Spring Cloud——Config Client

(本文只作为学习记录)
  • 首先要运行前文提到的Config Server配置中心

  • 新建Spring Boot项目,导入spring-boot-starter-webspring-cloud-starter-config依赖

  • 新建 bootstrap.yml文件用于替换application.properties文件,代码如下:

注意!!! 对于下述示例代码,要保证在你的git仓库中存在 config-client.yml文件
spring:
  application:
    name: config-client

  cloud:
    config:
      uri: http://localhost:9004/
      profile: default
      label: master

server:
  port: 9006

spring.application.name对应于配置文件名
cloud.config.uri对应于你的配置中心
cloud.config.profile对应于配置文件环境(如default,dev,test)
cloud.config.label对应于配置文件所在的分支
server.port对应于该项目对应的端口号

  • 新建web包,在该包下新建InfoController.java,添加以下代码:
@RestController
public class InfoController {
    //保证你的配置文件中含有该字段,可参考:https://github.com/sjh-github/SpringCloudConfig/blob/master/config-client.yml
    @Value("${info.from}")
    private String info;

    @GetMapping(value = "/info")
    public String getInfo() {
        return info;
    }
}

源代码请参考 ConfigClient

猜你喜欢

转载自blog.csdn.net/m0_37770300/article/details/81280984