SpringCloud配置中心(spring-cloud-config-server)及配置客户端(spring-cloud-starter-config)的踩坑及自我总结

夏侯阳
2023-12-01

搭建config-server访问github上存储的配置文件

阅读本文需要eureka-server和eureka-client的基础,这两个分别是服务注册中心和注册服务到服务注册中心

  1. 创建一个Maven工程,名字叫做exa-config-server,
    pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>clound-demo</artifactId>
        <groupId>com.clound.xf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>exa-config-client</artifactId>


    <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>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

其中各个依赖的版本我并没有写,是因为我采用的是父模块依赖。这几个依赖的版本都在最顶层的pom.xml中声明了,版本是:2.1.1.RELEASE

  1. 创建一个class文件,也就是springboot启动文件,命名ConfigServerApplication
package com.demo.xf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @Author: xiong Feng
 * @Date: 2019/4/11 10:32
 * @Description:
 */
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
    public static void main(String[] args){
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

@EnableConfigServer启用配置中心
@EnableEurekaClient作为一个服务注册到服务注册中心

创建application.p roperties,

server.port=8050
spring.application.name=exa-config-server
# 注册到服务注册中心
eureka.client.service-url.defaultZone=http://${hostname}:${port}/eureka/
# github的仓库地址 
spring.cloud.config.server.git.uri=https://github.com/crazyxFeng/SpringCloudConfig
# github的文件路径
spring.cloud.config.server.git.searchPaths=repo
# github的分支,默认是master
spring.cloud.config.label=master

至此,一个config-server就配置好了。

然后你在浏览器输入:http://127.0.0.1:8050/xf.name/dev
这里说下路径请求与资源对应关系
xf.name是属性,dev是开发环境

搭建config-client来访问数据

  1. 新建一个maven工程,名称exa-config-client
    pom.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>clound-demo</artifactId>
        <groupId>com.clound.xf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>exa-config-client</artifactId>


    <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>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>
  1. 新建class文件,也是springboot的启动类,命名ConfigClientApplication
package com.demo.xf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @Author: xiong Feng
 * @Date: 2019/4/11 11:20
 * @Description:
 */
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
    public static void main(String[] args){
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

  1. 新建一个controller命名ConfigClientController,用于访问github上的配置文件
package com.demo.xf.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xiong Feng
 * @Date: 2019/4/11 11:22
 * @Description:
 */
@RestController
public class ConfigClientController {

    @Value("${xf.env}")
    String env;

    @Value("${xf.name}")
    String name;
    @Value("${xf.age}")
    String age;

    @GetMapping(value = "/hi")
    public String hi(){
        return "我的名字是:"+name+",年龄是:"+age+",env:"+env;
    }
}

  1. 然后新建bootstrap.properties,注意这里是bootstrap而非application,是因为springboot在加载配置文件默认先加载bootstrap.properties,内容如下
spring.application.name=exa-config-client
server.port=8051
eureka.client.serviceUrl.defaultZone=http://${hostname}:${port}/eureka/

spring.cloud.config.label=master
# 指定github上配置文件的名称
spring.cloud.config.name=exa-config-server
# 指定开发环境
spring.cloud.config.profile=dev
# spring.cloud.config.discovery.enabled 是否从配置中心读取文件。
spring.cloud.config.discovery.enabled=true
# spring.cloud.config.discovery.serviceId 配置中心的serviceId,即服务名。
spring.cloud.config.discovery.serviceId=exa-config-server

上面需要注意的是:spring.cloud.config.name和spring.cloud.config.profile这两个属性,这两个属性合起来构成了github上配置文件的名称,所以我的github上配置文件的名称是exa-config-server-dev.properties,就这里这个配置,花了我一下午的时间。悲伤辣么大。。。。

  1. 然后我们访问浏览器输入url:http://xiongfeng:8051/hi,这个地址是注册到了服务注册中心的,所以可以这样访问。
    就可以看到如下结果:
    我的名字是:alex,年龄是:24,env:dev

总结我在搭建过程中踩的坑,

1.如果在client中访问时,报错如下,

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'xf.env' in value "${xf.env}"`
这种类似的错误,是你加载github上配置文件未成功

可从控制到看到是否加载成功,如下(是加载成功了的):

2019-04-11 16:31:59.166  INFO 11248 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://xiongfeng:8050/
2019-04-11 16:32:03.682  INFO 11248 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=exa-config-server, profiles=[dev], label=master, version=017238ed1980241f18211c783fdc6c3ee06df380, state=null
2019-04-11 16:32:03.682  INFO 11248 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/crazyxFeng/SpringCloudConfig/repo/exa-config-server-dev.properties'}]}

其中最后一栏中,最后部分的name就是成功加载的路径。
说下,我出现这种情况的解决办法,
(1)spring.cloud.config.name和spring.cloud.config.profile这两个属性值一定要和文件名对应,比如:
# 指定github上配置文件的名称
spring.cloud.config.name=exa-config-server
#指定开发环境
spring.cloud.config.profile=dev
那么github上文件名就只能为exa-config-server-dev.properties

(2)启动顺序,一定是config-server启动之后才启动config-client,如果注册到了eureka-server中,那么eureka-server是最先启动的,先于config-server和config-client

(3)在config-server的配置文件中,未指定该属性
#这个属性代表的是github中配置文件的路径
spring.cloud.config.server.git.searchPaths=repo
还有其他的情况,我就不一一列举了,

项目链接:
https://github.com/crazyxFeng/cloud-demo

 类似资料: