Spring Cloud 09 -- Client 通过 Config Server 读取配置文件

一、 在远程仓库添加配置文件

文件 client01-dev.yml

二、改造 client01,添加依赖

给 client01 做一些配置,使其可以通过 config server 读取远程配置文件

        
        
            org.springframework.cloud
            spring-cloud-starter-config
        

三、修改配置文件

新建一个 bootstrap.yml 文件,添加如下内容

spring:
  # 为了获取 Config Server 的配置文件
  cloud:
    config:
      # spring.cloud.config.label 指明远程仓库的分支
      label: master
      # spring.cloud.config.profile
        #  dev 开发环境配置文件
        #  test 测试环境
        #  pro 正式环境
      profile: dev
      # spring.cloud.config.uri 指明配置服务中心的网址
      uri: http://localhost:8181/

我的 application.yml 如下

server:
  port: 8763

spring:
  application:
    name: client01

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  # 为了在服务注册中心里显示实际的 IP 地址,需添加下面的配置
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true

# 为了打开 feign 的 hystrix 功能
feign:
  hystrix:
    enabled: true

所以访问 Config Server 会使用的路径是 http://localhost:8181/client01/dev/master

注意:一定要新建一个 bootstrap.yml ,因为 bootstrap.yml 优先于 application.yml ,在里面配置 Config Server 的路径,否则会一直使用默认的路径,默认的路径端口是8888,启动会报错。

四、在控制器中添加一个接口,返回 client01-dev.yml 的 myValue

代码如下

    // 获取 Config Server 的配置文件中的 myValue 的值
    // 注意:如果远程仓库中没有这个配置参数时,服务是启动不了的
    @Value("${myValue}")
    private String myValue;
    /**
     * 该接口是为了获取 config server 的配置文件信息
     */
    @RequestMapping(value = "/config",method = RequestMethod.GET)
    public String config(){
        return myValue;
    }

完整代码如下

import com.dhsg.sc.client01.service.IUseOtherApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private IUseOtherApiService userOtherApiService;

    // 该注解可以读取配置文件中的值赋予下面的变量
    @Value("${server.port}")
    private String port;

    // 获取 Config Server 的配置文件中的 myValue 的值
    // 注意:如果远程仓库中没有这个配置参数时,服务是启动不了的
    @Value("${myValue}")
    private String myValue;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(@RequestParam(value = "name", defaultValue = "dhsg") String name) {
        return "Hello " + name + " ,I am from port:" + port;
    }

    /**
     * 该接口是通过 feign 组件,访问 client02 的接口
     *
     * @return client02 的端口和服务名
     */
    @RequestMapping(value = "/getclient02name", method = RequestMethod.GET)
    public String getclient02name() {
        return userOtherApiService.getClient02Name();
    }

    /**
     * 该接口是为了获取 config server 的配置文件信息
     */
    @RequestMapping(value = "/config", method = RequestMethod.GET)
    public String config() {
        return myValue;
    }
}

五、访问 http://localhost:8763/config

结果

你可能感兴趣的:(Spring Cloud 09 -- Client 通过 Config Server 读取配置文件)