2

我正在自学 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但我没有找到太多关于如何使用它的教程。任何想法?

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

应用程序.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);
    }
}

应用程序.yml:

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/mstine/config-repo.git
4

5 回答 5

1

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

server:
  port: 8888
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: file: --Path--
于 2019-03-22T18:53:31.110 回答
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 中找到客户端和服务器代码的配置和示例,非常简单,因此您可以理解整个配置。

于 2016-04-09T05:17:16.913 回答
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>
于 2019-02-18T12:11:42.727 回答
0

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

于 2018-12-07T11:11:14.417 回答
0

据我所知,您忘记添加

@EnableDiscoveryClient

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

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

于 2016-07-18T10:50:50.797 回答