spring 配置客户端 - 启动客户端时无法解析值“${message}"中的占位符“message"错误 [英] spring config client - Could not resolve placeholder 'message' in value “${message}” error while starting client

查看:27
本文介绍了spring 配置客户端 - 启动客户端时无法解析值“${message}"中的占位符“message"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing spring config server. My config server starts fine and I can get the data from URL http://localhost:8888/client-config/development located at C:\\configprop as shown below.

{
    "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/messageconfig client 中获取数据时,我收到 无法解析占位符消息"值${message}" 错误.

but when I get the data in the config client using http://localhost:8080/message I am getting Could not resolve placeholder 'message' in value "${message}" error.

客户申请

主类

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

推荐答案

根据发布说明 spring cloud 2020.0,有重大变化.

According to the release notes for spring cloud 2020.0, there were breaking changes.

你需要在application.yml中加入以下内容

You need to add the following to application.yml

spring.config.import="configserver:"

这篇关于spring 配置客户端 - 启动客户端时无法解析值“${message}"中的占位符“message"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆