0% found this document useful (0 votes)
10 views17 pages

11 Spring Cloud Bus

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

11 Spring Cloud Bus

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Spring Cloud Bus

Applying dynamic changes to running services

Copyright Ken Krueger 2015


Module Outline
● The Problem: Dynamic Configuration Updates
● Spring Cloud Bus
● How Refresh Works

Copyright Ken Krueger 2015


Recall Spring Cloud Config
● Centralized server that serves-up configuration information
– Configuration itself can be backed by source control

● Clients connect over HTTP and retrieve their configuration settings


– Clients connect at startup time.

HTTP Client Application


ConfigServer
Config Server (Spring)
Config Server HTTP

HT
TP Client Application
(Spring)

Backing Files
(Git, Flat Files, etc.) Client Application
(Another Technology)

Copyright Ken Krueger 2015


Dynamic Configuration Changes
● But what if we have configuration changes after the client
applications are running?
● Traditional approach: “Bounce” all applications
– Repeating the startup process.

HTTP Client Application


ConfigServer
Config Server (Spring)
Config Server HTTP

HT
Client Application
TP
(Spring)
CHANGES
Backing Files
(Git, Flat Files, etc.) Client Application
(Another Technology)

Copyright Ken Krueger 2015


Potential Solution: Polling
● Applications could periodically poll the Config Server for changes
– After all, they send Eureka heartbeats!
● Probably best to push the changes from server to client instead.
– Config changes probably rare, no need to waste resources.

Any
changes? Client Application
ConfigServer
Config Server (Spring)
Config Server
Any
changes?
Any Client Application
changes? (Spring)
CHANGES
Backing Files
(Git, Flat Files, etc.) Client Application
(Another Technology)

Copyright Ken Krueger 2015


Spring Cloud Bus
● Push configuration changes to client applications via messaging
technology, like AMQP.

Spring Cloud Bus

You have
changes Client Application
ConfigServer
Config Server (Spring)
Config Server
You have
changes
Client Application
(Spring)
CHANGES
Backing Files
(Git, Flat Files, etc.) Client Application
(Another Technology)

Copyright Ken Krueger 2015


Module Outline
● The Problem: Dynamic Configuration Updates
● Spring Cloud Bus
● How Refresh Works

Copyright Ken Krueger 2015


Spring Cloud Bus
● Broadcasts configuration changes to clients.
– Eliminates need for client polling
● Based on Messaging technology
– Currently AMQP Only
– Clients become subscribers to configuration changes.

Copyright Ken Krueger 2015


Spring Cloud Bus Setup – Part 1
● Add dependency to the Spring Cloud Config Server:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

● Add the same dependency to each of your clients.


– Code works automatically
– Assumption: client code has spring cloud parent / dependency management section.

Copyright Ken Krueger 2015


Spring Cloud Bus Setup – Part 2
● Run an AMQP server, such as Rabbit MQ

● Rabbit MQ:
● Open Source
● Easy to Install and Run
● Pretty popular
● Spring Cloud Bus works automatically with Rabbit MQ
on localhost.

AMQP - Advanced Message Queueing Protocol

Copyright Ken Krueger 2015


Broadcasting Changes
1) Make changes to your config file(s)

Config Server does not poll for changes
2) POST /bus/refresh to your config server
3) Broker ensures message delivery to clients
4) Clients receive message and refresh themselves
Client Application
CHANGES (Spring)
Backing Files
(Git, Flat Files, etc.)
Client Application
(Spring)

POST http://server:port/bus/refresh Config


Server

Copyright Ken Krueger 2015


Module Outline
● The Problem: Dynamic Configuration Updates
● Spring Cloud Bus
● How Refresh Works

Copyright Ken Krueger 2015


How Refresh Works
● Spring Boot Applications can be Refreshed at Runtime
– Actuator provides /refresh endpoint (POST)
● org.springframework.boot / spring-boot-actuator dependency
– ONLY affects the following:
● Beans marked with @ConfigurationProperties
● Beans marked with @RefreshScope
● Logging level

Copyright Ken Krueger 2015


@ConfigurationProperties
● Introduced in Spring Boot
● Easy alternative to multiple @Value annotations
● Properties rebound on POST /refresh
@RestController
@ConfigurationProperties(prefix="wordConfig")
public class LuckyWordController {

String luckyWord;
String preamble;

@RequestMapping("/lucky-word")
public String showLuckyWord() { Notice: relaxed binding.
return preamble + ": " + luckyWord; LuckyWord, LUCKY_WORD
} also valid

// Getters and Setters ---


} wordConfig:
lucky-word: Irish
preamble: The lucky word is

Copyright Ken Krueger 2015


@RefreshScope
● Introduced in Spring Cloud
● Greater control, safe reloading of bean (not just property binding)
– Side-effect: makes bean lazy
● Reloaded (not just rebound) on POST /refresh
@RestController
@RefreshScope
public class LuckyWordController {

@Value("${wordConfig.lucky-word}") String luckyWord;


@Value("${wordConfig.preamble}") String preamble;

@RequestMapping("/lucky-word")
public String showLuckyWord() {
No more relaxed binding
return preamble + ": " + luckyWord; when using @Value
}
---
wordConfig:
// Getters and Setters NOT required
lucky-word: Irish
}
preamble: The lucky word is

Copyright Ken Krueger 2015


How @RefreshScope Works
● Spring creates a proxy for the actual bean
● Proxy is dependency injected into other beans
● Proxy contains logic to call methods on the target bean.
● On refresh:
– New bean is created
– “Target” reference is pointed to the newly created bean
– Older bean is dereferenced
● Result: users of original bean can safely finish their work.

ref Refere target Original Bean


nc es
Caller Proxy
ref.doWork() target.doWork() Replacement Bean

Copyright Ken Krueger 2015


Exercise

Setup Spring Cloud Bus with Rabbit MQ


Make Dynamic Configuration Changes

Instructions: Student Files, Lab 8

Copyright Ken Krueger 2015

You might also like