spring cloud confg-client的操作

Config Server服务端可以从后端存储中(上图为Git)拉取配置参数、属性,然后对外提供获取相应环境(dev开发、stage预发布、prod生产)配置的Restful服务。此时我们的Config Client客户端就可以通过Config Server提供的服务获取相关配置
 

接上一篇在gitee仓库的创建的3个配置文件,端口为9091-1,含有非数字,现在都改为6开头的数字:

server:
port: 6002

spring:

application:

name: ms-config-server-pre

一.新建项目:ms-config-client

1.pom文件:

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2. 配置文件: 启动的时候内置的bootstrap.yml配置文件,然后获取Config Server的配置,去连接Config Server,然后再去加载application.yml文件。

application.yml:

server:
  port: 8010

bootstrap.yml:注意这里的application.name要配成要访问远程仓库gitee中的需要访问的配置文件中的应用名,如仓库存在ms-config-client-dev.properties,那么这里的application.name改为xxx-dev.properties前面的应用名字,即ms-config-client。如果随便起一个名字,在仓库中找不到对应的配置文件,那么将只会取到application-*.yml这种默认的配置文件

spring:
  cloud:
    config:
      uri: http://localhost:8009
      profile: dev
      label: master   # 当configserver的后端存储是Git时,默认就是master
  application:
    name: ms-config-client

3.controller:

package com.ljf.weifuwu.springcloud.config.client.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("${city}")
   private String profileType;
    @Value("${spring.application.name}")
    private String name;
      @GetMapping("/msconfig-client")
      private String getProfileType() {
          System.out.println("ms-client获取的参数:"+profileType+" 远程name:"+name);
            return this.profileType+" >>"+name;
       }
}

 

4.启动类:

package com.ljf.weifuwu.springcloud.config.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class ConfigClientApp
{
    public static void main( String[] args ) {
        SpringApplication.run(ConfigClientApp.class, args);
        System.out.println("config client 启动成功了!!!");
    }
}

 5.分别启动:ms-config-server(8009)、ms-config-client(8010)

6.查看gitee中application-dev的配置文件:

6.访问:http://localhost:8010/msconfig-client   ,居然加载不到,郁闷了一整晚

7.在gitee仓库中,将application-dev.yml改为application-dev.properties文件格式

启动一下ms-config-client服务,再次访问:成功获取到

8.为了规范性,在仓库中新建和自己项目相关的应用:本地仓库新建一个“ms-config-client-dev.properties”文件,然后提交到远程仓库中

重启ms-config-client服务开始访问 :

 可能是仓库中有两个xxx-dev的文件,将application-dev.propertes,改为application-devtest.propertes:

重启ms-config-client服务再次访问:

接着修改ms-config-client-dev.propertes文件中内容,改为yml的层级结构:

 

 重启ms-config-client服务开始访问 :ok,这次成功了

结论是:spring config client 访问仓库的配置文件必须是properties后缀的文件且内容为yml格式的层次结构,且不能存在多个xxx-profile,profile相同的配置文件。这次为什么????????????????????????????????????

 二.常见的问题

1、本地配置和远程配置冲突问题
上面我们在Controller中使用@Value("${type}")注解注入了type参数,这个type参数是远程仓库中的参数,那么此时如果我们在application.yml配置文件中,加一个本地的参数type会怎样呢:

2、spring.application.name与远端仓库文件的关系
如果应用没有设置spring.application.name应用名,或仓库没有找到对应的应用名的配置文件,则只会取到application-*.yml这种默认的配置文件,所以要确保工程的spring.application.name已配置,并且在远端仓库中托管有这种应用名前缀的配置文件
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值