【问题标题】:Spring Boot application not reading properties value from Spring Cloud Config ServerSpring Boot 应用程序未从 Spring Cloud Config Server 读取属性值
【发布时间】:2021-03-22 11:47:23
【问题描述】:

我已经配置了一个 Spring Cloud Config Server,它从我们的 git 存储库中提取应用程序值。我可以通过浏览 spring cloud url [http://localhost:8012/XXXAPIConfigServer/default] 来确认它是否正确获取了值

现在在我的客户端微服务(users-ws)中,我添加了以下依赖项和配置

bootstrap.yml

spring:
  cloud:
    config:
      uri: http://localhost:8012
      name: XXXAPIConfigServer
  profiles:
    active: default

application.yml

jwt:
   secretKey: v++?c2HNyw^Eq#Ba=nFTYNRuSF1bKx
   prefix: Bearer
   expirationAfterDays: 1

当我运行客户端用户服务时,我得到的不是expirationAfterDays2,而是1

另外,我通过调用 jwtclass 来读取这些 jwt 值 JwtConfig

package com.company.abc.api.users.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

@Configuration
@ConfigurationProperties(prefix = "jwt")
public class JwtConfig {

   private String secretKey;
   private String prefix;
   private int expirationAfterDays;
   private String loginUrl;

   public JwtConfig() {}

   public String getSecretKey() {
      return secretKey;
   }

   public void setSecretKey(String secretKey) {
      this.secretKey = secretKey;
   }

   public String getPrefix() {
      return prefix;
   }

   public void setPrefix(String prefix) {
      this.prefix = prefix;
   }

   public int getExpirationAfterDays() {
      return expirationAfterDays;
   }

   public void setExpirationAfterDays(int expirationAfterDays) {
      this.expirationAfterDays = expirationAfterDays;
   }

   public String getLoginUrl() {
      return loginUrl;
   }

   public void setLoginUrl(String loginUrl) {
      this.loginUrl = loginUrl;
   }

   public String getAuthorizationHeader() {
      return HttpHeaders.AUTHORIZATION;
   }

}

这是来自客户端 api 用户服务的日志文件:(https://justpaste.it/8aeop)

【问题讨论】:

  • 看看你的 spring boot 启动日志,看看发生了什么。
  • 你用的是什么版本?
  • @spencergibb, spring-cloud-config-server => 3.0.0-M6。对于spring-cloud-start-config,我没有在pom.xml中添加版本所以我猜是最新版本2.2.6
  • 由于您不确定您的客户端版本,并且没有从配置服务加载配置的操作,您可以查看文档here,并通过设置spring.cloud.bootstrap.enabled=true手动启用引导或者在你的 POM 文件中包含新的spring-cloud-starter-bootstrap
  • 我会进一步深入研究配置。相同的配置应用于 Zuul API GW,它从配置服务器获取值。

标签: spring-boot spring-cloud spring-cloud-config


【解决方案1】:

我找到了导致问题的原因。我有与 Zuul API Gateway 相同的 bootstrap.yml 配置,我注意到我使用的是以下版本:

spring-boot-starter-parent => 2.3.6.RELEASE
sprint-cloud.version => Hoxton.SR9

我正在使用的用户 Web 服务中的位置(不工作)

spring-boot-starter-parent => 2.4.0
sprint-cloud.version => 2020.0.0-SNAPSHOT

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 2018-06-25
    • 2015-08-28
    • 2016-07-30
    • 2019-01-19
    • 2021-02-01
    • 2016-06-04
    • 2018-03-18
    相关资源
    最近更新 更多