【问题标题】:Spring Cloud Config Client connecting to RabbitMQSpring Cloud Config Client 连接到 RabbitMQ
【发布时间】:2015-08-05 10:54:34
【问题描述】:

我一直在尝试设置 Spring Cloud Config Server/Client。我一直在关注几个不同的例子(12)。我已经正确设置了客户端和服务器,并且可以成功查询 localhost:8888/localhost:8080 以查看 JSON 格式的值。

我的问题是 Spring Boot 是否会自动检测 Spring Cloud Config Server 提供的这些属性。现在我只是尝试在启动时连接到 RabbitMQ 实例,但尽管没有任何错误,但没有成功。它不会连接到 Rabbit 或创建队列/交换。

当我在本地有一个具有以下属性的 application.properties 文件但我希望通过 Spring Cloud Config 从 GitHub 存储库获取这些设置时,它可以工作。

spring.rabbitmq.host=178.61.47.---
spring.rabbitmq.port=5672
spring.rabbitmq.username=mqtt
spring.rabbitmq.password=mqtt

我已经浏览了 GitHub 上的问题/问题,但看不到任何与此相关的内容。

客户端类的代码如下:

@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class ConfigRabbitApplication {

    final static String queueName = "arduino-weather-queue";

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Bean
    Queue queue() {
        return new Queue(queueName, true);
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("arduino-weather");
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("arduino-iot-exchange", true, false);
    }

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

}

【问题讨论】:

  • 你有重现问题的项目吗?
  • 嗨,Spencer,我在这里将 ConfigClientConfigServer 的代码上传到了 GitHub
  • 您是否尝试从 RabbitMQ 读取配置?

标签: spring spring-boot spring-cloud


【解决方案1】:

不,spring boot 客户端不知道您想从 config-server 获取配置。这可能是在类路径中的特定类时加载的,这就是为什么你必须添加 org.springframework.cloud:spring-cloud-starter-config 依赖项。这里有很好的描述:http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage

如果 config-server 不在 localhost:8888 上,您还必须添加:

spring.cloud.config.uri: http://myconfigserver.com

到您的 bootstrap.yml 文件(它与 application.yml 相同,只是之前加载)。

【讨论】:

  • 我已经添加了 spring-cloud-starter-config 依赖项,并且能够使用 @Value 注释来显示 GitHub 上文件的属性。我想要做的是用 GitHub 上文件中的值替换我通常在本地托管的属性(在 application.properties 中)。
  • 您可以在哪个网址下查看此属性?关于使用哪个配置文件/应用程序名称很重要。它应该在 bootstrap.yml 文件中配置,如下所示:cloud.spring.io/spring-cloud-config/…
  • 可以查看localhost:8888/rabbitconfig/master下的属性。 Rabbitconfig 既是客户端中 bootstrap.yml 中给出的名称,也是 GitHub 上属性文件的名称。但是,我无法在启动时在应用程序中使用这些属性,因为我希望在运行 RabbitMQ 时它没有连接。
猜你喜欢
  • 2019-07-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 2023-01-20
  • 2020-10-05
  • 2017-08-17
  • 2017-03-29
  • 2018-05-12
相关资源
最近更新 更多