如何使用带有Rabbit mq的Spring Cloud配置服务器和Spring Cloud总线更新配置?

问题描述 投票:1回答:1

我找不到任何最新的和/或有效的文档和/或有关如何进行设置的示例,因此我正在研究并尝试并出错。但是我还没能运行它。

Im具有config-server,具有以下依赖性:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

config-server具有以下bootstrap.yml

spring:
  application:
    name: config-server
  rabbitmq:
    host: ${RABBIT_MQ_HOST:localhost}
    port: ${RABBIT_MQ_PORT:5672}
    username: ${RABBIT_MQ_URSER_NAME:guest}
    password: ${RABBIT_MQ_URSER_PASSWORD:guest}

以及以下application.yml

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/MyGit/config-repository.git
          cloneOnStart: true

management:
  endpoints:
    web:
      exposure:
        include: "*"

server:
  port: 8888

我所有的客户都具有以下依赖性:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

例如,他们的bootstrap.yml看起来像这样:

spring:
  application:
    name: user-service
  cloud:
    config:
      uri: http://${CONFIG_HOST:localhost}:${CONFIG_PORT:8888}
  rabbitmq:
    host: ${RABBIT_MQ_HOST:localhost}
    port: ${RABBIT_MQ_PORT:5672}
    username: ${RABBIT_MQ_URSER_NAME:guest}
    password: ${RABBIT_MQ_URSER_PASSWORD:guest}

以及他们的application.yml这样:

management:
  endpoints:
    web:
      exposure:
        include: "*"

server:
  port: 8000

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://${EUREKA_HOST:localhost}:${EUREKA_PORT:8761}/eureka

例如,使用弹簧@ConfigurationProperties的Im:

@Component
@ConfigurationProperties("user")
@Getter
@Setter
public class UserConfiguration {
    private String role;
}

我正在使用dockerdocker-compose启动我的所有服务(包括rabbitmq:)>

version: "3"

services:

  rabbitmq:
    image: bitnami/rabbitmq:latest
    container_name: rabbitmq
    environment:
      RABBITMQ_USERNAME: admin
      RABBITMQ_PASSWORD: admin
    ports:
      - "4369:4369"
      - "5672:5672"
      - "15672:15672"
      - "25672:25672"

  config-server:
    build:
      context: config-server
      dockerfile: Dockerfile
    container_name: config-server
    depends_on:
      - rabbitmq
    environment:
      RABBIT_MQ_HOST: "rabbitmq"
      RABBIT_MQ_PORT: "5672"
      RABBIT_MQ_URSER_NAME: "admin"
      RABBIT_MQ_URSER_PASSWORD: "admin"
    ports:
      - "8888:8888"

  eureka-server:
    build:
      context: eureka-server
      dockerfile: Dockerfile
    container_name: eureka-server
    depends_on:
      - rabbitmq
      - config-server
    environment:
      RABBIT_MQ_HOST: "rabbitmq"
      RABBIT_MQ_PORT: "5672"
      RABBIT_MQ_URSER_NAME: "admin"
      RABBIT_MQ_URSER_PASSWORD: "admin"
      CONFIG_HOST: "config-server"
      CONFIG_PORT: "8888"
    ports:
      - "8761:8761"

  zuul-gateway:
    build:
      context: zuul-gateway
      dockerfile: Dockerfile
    container_name: zuul-gateway
    depends_on:
      - rabbitmq
      - config-server
      - eureka-server
    environment:
      RABBIT_MQ_HOST: "rabbitmq"
      RABBIT_MQ_PORT: "5672"
      RABBIT_MQ_URSER_NAME: "admin"
      RABBIT_MQ_URSER_PASSWORD: "admin"
      CONFIG_HOST: "config-server"
      CONFIG_PORT: "8888"
      EUREKA_HOST: "eureka-server"
      EUREKA_PORT: "8761"
    ports:
      - "8765:8765"

  user-service:
    build:
      context: user-service
      dockerfile: Dockerfile
    container_name: user-service
    depends_on:
      - rabbitmq
      - config-server
      - eureka-server
    environment:
      SPRING_PROFILES_ACTIVE: "development"
      RABBIT_MQ_HOST: "rabbitmq"
      RABBIT_MQ_PORT: "5672"
      RABBIT_MQ_URSER_NAME: "admin"
      RABBIT_MQ_URSER_PASSWORD: "admin"
      CONFIG_HOST: "config-server"
      CONFIG_PORT: "8888"
      EUREKA_HOST: "eureka-server"
      EUREKA_PORT: "8761"
    ports:
      - "8000:8000"

  user-service-2:
    build:
      context: user-service
      dockerfile: Dockerfile
    container_name: user-service-2
    depends_on:
      - rabbitmq
      - config-server
      - eureka-server
    environment:
      SPRING_PROFILES_ACTIVE: "development"
      RABBIT_MQ_HOST: "rabbitmq"
      RABBIT_MQ_PORT: "5672"
      RABBIT_MQ_URSER_NAME: "admin"
      RABBIT_MQ_URSER_PASSWORD: "admin"
      CONFIG_HOST: "config-server"
      CONFIG_PORT: "8888"
      EUREKA_HOST: "eureka-server"
      EUREKA_PORT: "8761"
    ports:
      - "8001:8000"

  user-service-3:
    build:
      context: user-service
      dockerfile: Dockerfile
    container_name: user-service-3
    depends_on:
      - rabbitmq
      - config-server
      - eureka-server
    environment:
      SPRING_PROFILES_ACTIVE: "development"
      RABBIT_MQ_HOST: "rabbitmq"
      RABBIT_MQ_PORT: "5672"
      RABBIT_MQ_URSER_NAME: "admin"
      RABBIT_MQ_URSER_PASSWORD: "admin"
      CONFIG_HOST: "config-server"
      CONFIG_PORT: "8888"
      EUREKA_HOST: "eureka-server"
      EUREKA_PORT: "8761"
    ports:
      - "8002:8000"

当我开始所有服务时,一切似乎都很好。所有服务都已在eureka上注册,所有服务均可在localhost上调用,并且它们可以相互通信。但是配置刷新无法正常工作。

当我在配置存储库中更改某些内容并提交并推送时,它对我的​​服务没有影响。他们拥有的配置保持不变。当我手动在配置服务器上调用总线刷新时:

http://localhost:8888/bus/refresh

使用POST,就像到目前为止我所能找到的几乎每本指南中所描述的,我都会得到响应:

{
    "timestamp": "2020-03-30T09:33:57.818+0000",
    "status": 405,
    "error": "Method Not Allowed",
    "message": "Request method 'POST' not supported",
    "path": "/bus/refresh"
}

当我改用GET时,我得到:

{
    "name": "bus",
    "profiles": [
        "refresh"
    ],
    "label": null,
    "version": "03759e798f3516da6a18fc8b61a265d37ddeff4e",
    "state": null,
    "propertySources": []
}

并且它对我的服务配置也没有影响。当我在任何服务上调用总线刷新时,都会得到:

{
    "timestamp": "2020-03-30T09:35:39.643+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/bus/refresh"
}

我需要怎么做才能使自动配置刷新正常工作?

手动调用服务上的/actuator/refresh端点有效。然后,他们将提取新的配置。因此,似乎兔子mq根本无法正常工作。

我找不到任何最新的和/或有效的文档和/或有关如何进行设置的示例,因此我正在研究并尝试并出错。但是我一直无法运行它。 Im ...

spring docker docker-compose rabbitmq spring-cloud
1个回答
0
投票

我找到了解决方案。最终,我的设置和配置文件完全没问题,我只是称错了端点。而不是调用:

© www.soinside.com 2019 - 2024. All rights reserved.