无法从Spring Cloud配置服务器接收值

时间:2019-07-17 11:20:10

标签: java spring spring-boot github spring-cloud-config

在带有Github存储库的spring-cloud-config服务器上使用@Value($ {fanout.exchange})注释无法初始化。 我得到了:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'queue' defined in class path resource [com/eloomina/dataCollector/rabbitMQ/RabbitConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.amqp.core.Queue]: Factory method 'queue' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.rabbitConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'fanout.exchange' in value "${fanout.exchange}"

两个类的pom.xml都具有spring-cloud-config。 configServer带有@EnableConfigServer注释。我在github存储库中的文件名为dataCollector.properties

RabbitConfiguration类:

@Configuration
@RefreshScope
public class RabbitConfiguration {
    @Value("${fanout.exchange}")
    private String rawDataExchange;
    @Value("${queue.name}")
    private String queueName;
    @Bean
    Queue queue() {
        return new Queue(queueName, true);
    }
    @Bean
    FanoutExchange exchange() {
        return new FanoutExchange(rawDataExchange);
    }
    @Bean
    Binding binding(Queue queue, FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
}

我的配置服务器application.properties:

server.port=8888
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
spring.application.name=configServer
spring.cloud.config.server.git.uri=https://github.com/eLoomina/configFiles
spring.cloud.config.server.git.username=xxxxxx
spring.cloud.config.server.git.password=xxxxxx

我的客户端bootstrap.properties:

spring.application.name=dataCollector 
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

github存储库包含一个文件:dataCollector.properties:

s

pring.devtools.restart.enabled = false

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka


server.port=8081
spring.application.name=dataCollector

##MONGO:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=accounts



management.server.port: 9001

## RABBITMQ
queue.name: rawDataQueue
fanout.exchange: rawdata-exchange
spring.rabbitmq.host: localhost
spring.rabbitmq.port: 5672
spring.rabbitmq.username: guest
spring.rabbitmq.password: guest

##LOGGING:
logging.file=dataCollector.log

为什么dataCollector服务无法从配置服务器获取fanout.exchange?

1 个答案:

答案 0 :(得分:0)

您的文件是否在spring上下文中加载?您可以在日志中看到如下所示的文件:

  

INFO 1234 --- [main] b.c.PropertySourceBootstrapConfiguration:找到的属性源:CompositePropertySource {name ='demoService',   propertySources = [MapPropertySource {name ='configClient'},   MapPropertySource   {name ='https://github.com/demo-configuration/application.properties'}]}