Spring Cloud Config Server上的属性不会注入Spring应用程序

问题描述 投票:2回答:5

我正在自学Spring Cloud Config Server,并且在将属性注入bean时遇到了一些麻烦。

所以我有一个简单的Spring Boot Application作为配置客户端,仅用于测试:

@SpringBootApplication
@ConfigurationProperties
public class DemoApplication {

    @Value("${greeting}")
    static private String greeting;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);  
        System.out.println("The greeting is: " + greeting);
    }
}

但系统打印:

The greeting is: null

检查env端点,我实际上发现"${greeting}"属性在环境中:

profiles: [ ],
configService:https://github.com/mstine/config-repo.git/demo.yml: {
greeting: "Webhook"
},
configService:https://github.com/mstine/config-repo.git/application.yml: {
eureka.instance.hostname: "localhost",
eureka.instance.leaseRenewalIntervalInSeconds: 10,
eureka.instance.metadataMap.instanceId: "${vcap.application.instance_id:${spring.application.name}:${server.port:8080}}",
eureka.client.serviceUrl.defaultZone: "${vcap.services.service-registry.credentials.uri:http://127.0.0.1:8761}/eureka/",
foo: "barnyardAnimal"
},

请注意,在configService中,有一个名为greeting的属性,其值为"Webhook"

我是Spring Framework的新手,所以我想知道我不是搞砸了什么?有人建议我也可以使用Environment访问外部属性,但我没有找到太多关于如何使用它的教程。任何想法?

=================================== UPDATE ============== ========================添加配置服务器的代码:

application.Java:

package io.spring.cloud.samples.fortuneteller.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class Application {

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

application.yml:

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/mstine/config-repo.git
spring spring-cloud-config
5个回答
0
投票

我认为你需要创建和启动你的服务器,我没有看到代码

@SpringBootApplication
@EnableConfigServer
public class Application {

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

启用服务器后,客户端需要连接到服务器并询问服务器将为其存储的配置参数。该服务器需要存储将要存储值的.yml。

客户端代码需要与服务器通信并使用bootstrap.yml询问特定值

---
spring:
  profiles:
    active: northamerica
  application:
    name: koitoerclient
  cloud:
     config:
       uri: http://localhost:8001
server:
  port: 8002

您可以在github here中找到配置和客户端和服务器代码的示例,非常简单,因此您可以了解整个配置。


0
投票

据我所知,你忘了添加

@EnableDiscoveryClient

在客户端应用程序的主类之上

我做了这个,在类似的情况下它对我有用


0
投票

您不能将属性作为静态字段注入。尝试将其注入某些@Component


0
投票

您需要将spring-cloud-dependencies添加到客户端应用程序的类路径中,以便在其自动启动器配置中使用云配置。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring.cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

0
投票

如果您使用的是本地存储库,则application.yml应如下所示:

server:
  port: 8888
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: file: --Path--
© www.soinside.com 2019 - 2024. All rights reserved.