简体   繁体   中英

How to update configurations using spring cloud config server and spring cloud bus with rabbit mq?

I could not find any up to date and / or working documentation and / or example on how to set this up so what I did is researching and try and error. Yet I have not been able to get it running.

Im having a config-server with the following dependencies:

<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>

The config-server has the following 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}

And the following 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

All my clients are having the following dependencies:

<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>

Their bootstrap.yml for example looks like this:

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}

And their application.yml like this:

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

server:
  port: 8000

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

Im using springs @ConfigurationProperties , for example:

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

I'm starting all my services (including rabbitmq) using docker and docker-compose :

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"

When all my services are started, at first everything seems to be fine. All services are registered with eureka, all services are callable on localhost and they can talk to each other. But the config refresh is not working.

When I change something in my config repository and commit and push it it has no effect on my services. The configuration they have stays the same. When I manually call the bus refresh on my config-server:

http://localhost:8888/bus/refresh

with POST , like it is described in nearly every guide I could find so far, I get the response:

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

When I use GET instead, I get:

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

and it also has no effect on the confiuration of my services. And when I call the bus refresh on any of my services I get:

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

What do I need to do to get the auto configuration refresh working?

Manually calling the /actuator/refresh endpoints on the services works. They are then pulling the new config. So it seems like the rabbit mq simply is not working.

I found the solution. Acutally my setup and configuration files are totally fine, I just called the wrong endpoint. Instead of calling:

http://localhost:8888/bus/refresh

I have to call:

http://localhost:8888/actuator/bus-refresh

This works on any service I started. It will automatically send a message to rabbitmq which will then publish the refresh to all consumers. All configurations are then updating.

Though I still dont know why bus/refresh is not working. It is used in many examples / tutorials.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM