【问题标题】:Properties on Spring Cloud Config Server doesn't inject into Spring applicationSpring Cloud Config Server 上的属性不会注入 Spring 应用程序
【发布时间】:2016-07-30 11:51:39
【问题描述】:

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

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

@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 访问外部属性,但我没有找到太多关于如何使用它的教程。有什么想法吗?

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

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


【解决方案1】:

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

@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

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

【讨论】:

  • 您好,谢谢您的回答。配置服务器已启动并正在运行,但仍不显示该值。我在更新中附加了配置服务器 sn-p。谢谢。
【解决方案2】:

据我所知,您忘记添加

@EnableDiscoveryClient

在客户端应用的主类之上

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      您需要将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>
      

      【讨论】:

        【解决方案5】:

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

        server:
          port: 8888
        spring:
          profiles:
            active: native
          cloud:
            config:
              server:
                native:
                  searchLocations: file: --Path--
        

        【讨论】:

          猜你喜欢
          • 2019-09-12
          • 2021-03-22
          • 2021-10-09
          • 2019-07-14
          • 2019-01-19
          • 2015-03-09
          • 1970-01-01
          • 2015-10-04
          • 1970-01-01
          相关资源
          最近更新 更多