【问题标题】:Spring Cloud Client not fetching configuraiton from spring cloud serverSpring Cloud Client 未从 Spring Cloud 服务器获取配置
【发布时间】:2021-01-12 19:15:30
【问题描述】:

我是 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 repos 链接在这里 Main PropertiesAlternative Properties

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

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 时出错:注入 autowired 依赖失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析占位符 值“${rate}”中的“率”

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

有什么提示吗?

【问题讨论】:

标签: java spring client-server spring-cloud spring-framework-beans


【解决方案1】:

我遇到了同样的问题,发现 this 的帖子很有帮助。

不过,我将在这里巩固我的发现。

确保您的 Spring Cloud 客户端具有正确的 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

【讨论】:

    猜你喜欢
    • 2017-09-07
    • 2020-04-16
    • 2017-07-25
    • 2023-03-24
    • 1970-01-01
    • 2016-09-12
    • 2020-09-27
    • 2021-03-21
    • 2020-06-03
    相关资源
    最近更新 更多