簡體   English   中英

spring 配置客戶端 - 啟動客戶端時無法解析值“${message}”錯誤中的占位符“消息”

[英]spring config client - Could not resolve placeholder 'message' in value “${message}” error while starting client

我正在實施spring config server 我的配置服務器啟動正常,我可以從 URL C:\\configprop 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/messageconfig client中獲取數據時,我得到了Could not resolve placeholder 'message' in value "${message}"錯誤。

客戶端應用程序

主Class

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);
    }
}

controller class

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;
    }
}

應用程序.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>

客戶端配置 development.properties

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

嘿可以通過這個注入這個屬性

步驟 1 像這樣創建 Class 並在 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 class

@Autowired private LoginProperties loginProperties;

然后嘗試

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

    

或者你可以直接嘗試這種方式,但不確定

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

根據 spring 雲 2020.0 的發行說明,存在重大更改。

您需要將以下內容添加到 application.yml

spring.config.import="configserver:"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM