首页 > 解决方案 > Spring Cloud Client 未从 Spring Cloud 服务器获取配置

问题描述

我是 Spring Cloud 的新手,我正在尝试使用存储在 github 上的属性文件连接服务器和客户端。

我的服务器 application.yml 文件配置如下:

---
server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/JonEasy/pluralsight-spring-cloudconfig-wa-tolls.git
          #username: uname
          #password: pass
          search-paths:
            - 'station*'
          repos:
            perf:
              pattern:
                - '*/perf'
              uri: https://github.com/JonEasy/pluralsight-spring-cloudconfig-wa-tolls-perf.git
              search-paths:
                - 'station*'

github 存储库在此处链接主要属性替代属性

我的客户端应用程序具有以下设置

spring.application.name=s1rates
spring.profiles.active=default
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.enabled= true

休息控制器是:

@Controller
public class RateController {

    @Value("${rate}")
    String rate;

    @Value("${lanecount}")
    String lanecount;

    @Value("${tollstart}")
    String tollstart;


    @RequestMapping("/rate")
    public String getRate(Model m) {

        m.addAttribute("rateamount", rate);
        m.addAttribute("lanes", lanecount);
        m.addAttribute("tollstart", tollstart);

        //name of the view
        return "rateview";

    }
}

所有 ${variables} van 都可以在 git 存储库中的属性文件中找到。

服务器运行良好,但客户端给我以下错误

创建名为“rateController”的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析值“${rate}”中的占位符“rate”

另请注意,我的 spring 应用程序名称“s1rates”与我的主存储库中的属性文件相匹配,位于 station1/s1rates.properties 下。

有什么提示吗?

标签: javaspringclient-serverspring-cloudspring-framework-beans

解决方案


我遇到了同样的问题,发现这篇文章很有帮助。

但是,我将在这里巩固我的发现。

确保您的 spring 云客户端具有正确的 pom.xml 文件。

<parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>2020.0.0</version>
</parent>

并添加依赖

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

休息多个帖子中给出的所有属性,通常都很好。

例如在客户端 bootstrap.properties 文件中

spring.application.name=s1rates
spring.profiles.active=default
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.enabled= true

推荐阅读