【问题标题】:can't receive values from spring cloud config server无法从 Spring Cloud 配置服务器接收值
【发布时间】:2019-11-26 05:12:23
【问题描述】:

使用带有 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 repo 包含一个文件: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 吗?

【问题讨论】:

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


    【解决方案1】:

    你的文件是在 spring 上下文中加载的吗?你能在日志中看到类似下面的文件吗:

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

    【讨论】:

    • 我可以访问 localhost:8888/dataCollector/default。你的意思是在我的配置服务器中?还是在客户端? (数据收集器)?
    • 我的意思是当你启动你的客户端时在客户端是加载的配置文件。因为除非加载配置文件,否则@Value 无法将字段绑定到属性,因为该属性在 spring 上下文中不可用。
    • 客户端日志中看不到文件
    • 我想问题在于从配置服务器获取文件。您可以将配置文件本地移动到客户端的资源目录中并包含在spring上下文中,然后看看它是否有效?如果是,则问题与配置服务器有关。
    • 另外,尝试将所有配置属性移动到 bootstrap.properties 而不是 application.properties spring.cloud.config.server.git.uri=github.com/eLoomina/configFilesspring.cloud.config.server.git。用户名=xxxxxx spring.cloud.config.server.git.password=xxxxxx
    猜你喜欢
    • 2018-08-24
    • 2017-09-07
    • 1970-01-01
    • 2021-11-25
    • 2017-07-25
    • 2023-03-24
    • 1970-01-01
    • 2018-05-08
    • 2020-07-23
    相关资源
    最近更新 更多