Implementation

We will use RabbitMQ as the message broker. Ensure that you have installed and started up RabbitMQ before proceeding further.

Installation instructions for RabbitMQ are provided at https://www.rabbitmq.com/download.html.

The next step is to add connectivity to Spring Cloud Bus for Microservice A. Let's add the following dependency in the pom.xml file of Microservice A:

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

We can run Microservice A on different ports by providing the port as one of the startup VM arguments. The following screenshot shows how you can configure the server port as the VM argument in Eclipse. The value configured is -Dserver.port=8081:

We will run Microservice A on ports 8080 (default) and 8081. The following is an extract from the log when Microservice A is restarted:

o.s.integration.channel.DirectChannel : Channel 'microservice-a.springCloudBusInput' has 1 subscriber(s).
Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure
Bean with name 'refreshBusEndpoint' has been autodetected for JMX exposure
Created new connection: SimpleConnection@6d12ea7c [delegate=amqp://[email protected]:5672/, localPort= 61741]
Channel 'microservice-a.springCloudBusOutput' has 1 subscriber(s).
declaring queue for inbound: springCloudBus.anonymous.HK-dFv8oRwGrhD4BvuhkFQ, bound to: springCloudBus
Adding {message-handler:inbound.springCloudBus.default} as a subscriber to the 'bridge.springCloudBus' channel

All instances of Microservice A are registered with Spring Cloud Bus and listen to events on the Cloud Bus. The default configuration of RabbitMQ Connection is a result of the magic of autoconfiguration.

Let's update microservice-a.properties with a new message now:

    application.message=Message From Default Local
Git Repository Changed Again

Commit the file and fire a request to refresh the configuration on one of the instances, let's say port 8080, using the URL http://localhost:8080/bus/refresh:

    curl -X POST http://localhost:8080/bus/refresh

The following is an extract from the log of the second instance of Microservice A running on port 8081:

Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@510cb933: startup date [Mon Mar 27 21:39:37 IST 2017]; root of context hierarchy
Fetching config from server at: http://localhost:8888
Started application in 1.333 seconds (JVM running for 762.806)
Received remote refresh request. Keys refreshed [application.message]

You can see that even though the refresh URL is not called on port 8081, the updated message is picked up from the Config Server. This is because all instances of Microservice A are listening on the Spring Cloud Bus for change events. As soon as the refresh URL is called on one of the instances, it triggers a change event and all other instances pick up the changed configuration.

You will see the configuration change reflect in both instances of Microservice A at http://localhost:8080/message and http://localhost:8081/message. The following is the response from the service:

    {"message":"Message From Default Local 
Git Repository Changed Again"}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset