【问题标题】:spring config client - Could not resolve placeholder 'message' in value “${message}” error while starting clientspring config客户端-启动客户端时无法解析值“$ {message}”中的占位符“消息”错误
【发布时间】:2021-05-05 09:32:56
【问题描述】:

我正在实施spring config server。我的配置服务器启动正常,我可以从位于C:\\configprop 的 URL http://localhost:8888/client-config/development 获取数据,如下所示。

{
    "name": "client-config",
    "profiles": [
        "development"
    ],
    "label": null,
    "version": "0dec53953ad4f620031cdbb3a99a0fe9701fb9df",
    "state": null,
    "propertySources": [
        {
            "name": "C:\\\\configprop/file:C:\\Users\\{user}\\AppData\\Local\\Temp\\config-repo-5116664058083487059\\client-config-development.properties",
            "source": {
                "msg": "Hello world - this is from config server - Development Environment"
            }
        }
    ]
}

但是当我使用http://localhost:8080/message 获取config client 中的数据时,我收到Could not resolve placeholder 'message' in value "${message}" 错误。

客户端应用程序

主类

package com.example.demo;

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

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

控制器类

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class MessageRestController {

    @Value("${message}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return this.message;
    }
}

aplication.yml

spring:
  application:
    name: client-config
  cloud:
    config:
      uri: http://localhost:8888
  profiles:
    active: development
management:
  endpoints:
    web:
      exposure:
        include: refresh

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

client-config-development.properties

message = Hello world - this is from config server - Development Environment

【问题讨论】:

  • 您的 application.properties 文件中是否有“消息:这是我的消息”或类似内容?这就是它正在寻找的
  • message 属性在client-config-development.properties中定义
  • 该文件的外观如何?您是否正在使用该文件中的其他属性,这些属性确实有效?
  • @Stultuske 我已经添加了client-config-development.properties
  • 所以,那里没有其他属性。可能,它找不到您的属性文件。您指向的是固定路径:“c:\ ...”,尤其是因为您在服务器上工作,所以不应该这样做。

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


【解决方案1】:

嘿可以通过这个注入这个属性

第 1 步 像这样创建一个类,并在 ConfigurationProperties 中传递应用程序属性路径前缀

@ConfigurationProperties(prefix = "app.xyz")
@Getter
@Setter
@Configuration
public class LoginProperties {

    private String message;
    
}

在应用程序属性中

@SpringBootApplication
@EnableConfigurationProperties(LoginProperties.class)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

在您的 application.yml 属性文件中,您可能有这种类型的值

app:
    xyz:
      message: "Hello Java"

在您的 MessageRestController 类中

@Autowired private LoginProperties loginProperties;

那就试试

 @GetMapping("/message")
    public String getMessage() {
        return loginProperties.getMessage;
    }

    

或者你可以直接尝试这种方式但不确定

@GetMapping("/message")
    public String getMessage() {
        return `${message}`;
    }

【讨论】:

  • 我不能使用上面的配置,因为我需要在那里添加更多的属性,比如数据库配置
  • 即使你有 100 多个像我一样的财产也不会有问题
  • 但在这里我们只尝试引导 mssage,它不会影响任何或您的流程或代码
  • 同意,但对于数据库属性,我将再次面临同样的问题,因为我将在那里仅使用 @Value 注释
  • 如果它的工作最后被添加,你能试试这个方法吗
【解决方案2】:

根据 spring cloud 2020.0 的release notes,发生了重大变化。

您需要在application.yml中添加以下内容

spring.config.import="configserver:"

【讨论】:

  • 我试过spring.config.import=configserver:http://localhost:8888还是同样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 2020-07-24
  • 2020-06-27
  • 2016-06-21
  • 1970-01-01
  • 2019-04-08
相关资源
最近更新 更多