On 25 November Trifork hosted a webinar in which I gave a short overview of Spring Cloud and its support for the Netflix OSS stack, focusing on Spring Cloud Config and the support for Netflix’s Eureka, Ribbon and Hystrix.
We’ve been investigating this stack over the last couple of months and are using parts of it in production already: we have found that a lot of the common concerns that you need to tackle as you’re moving into a distributed systems architecture are nicely covered and, in many cases, even abstracted by the Spring Cloud platform. In a typical Spring fashion, this allows you as an application developer to focus more on your business logic while letting the framework handle the concerns related to things like accessing shared configuration, working with service registries, handling failing downstream services, etc.
This blog provides you with background info to accompany the webinar, which has been recorded and can be found on our YouTube channel.
The code has been published on GitHub, as well as the accompanying config repository, in case you’d like to code along with the video.
In the demo I basically showcase an application with the following architecture:
There’s two services with a web application fronting them. All three applications act as clients to both the config server as well as Eureka. The calls that the conference web app makes to the downstream services are guarded by Hystrix circuitbreakers. There’s also a Hystrix dashboard app that can visualize the state of the circuit breakers present in the web app.
All applications can be run from the command line using Spring Boot’s maven plugin: just type ‘mvn spring-boot:run’. Of course you can also import the projects into your IDE and run them from there, or use one of the other methods available to run Spring Boot-based applications.
After cloning the Git repositories, make sure to first update the config server’s configuration file to point to your local copy of the Git config repository. The code will be in the state that matches the end of the webinar: there are ‘start’ tags in both repositories if you’d like to reset the code to the state matching the start of the webinar instead.
Steps taken in the webinar
The steps shown in the webinar, which you can make for yourself if you reset the code to the ‘start’ tag, are the following:
- Add the spring-cloud-starter-config dependency to the talk-service and inject the ‘greeting’ configuration property into its controller. I also enabled a ‘dev’ profile for the talk-service. The greeting key needs to be added to the talk-service.properties in the config repo and the config server needs to be running; after that, you can start the talk-service and it should work.
- Add the spring-cloud-starter-eureka dependency to the talk-service and annotate its main class with @EnableDiscoveryClient to have it register itself with Eureka (which needs to be running for that, obviously). Restart the talk-service, and it should show up in the Eureka dashboard. Start the review-service and conference-web apps as well, and they too will show up: you can now access the conference-web app at http://localhost:8080/.
- Modify the TalkService in the conference-web app to use a HystrixCommand with a fallback method returning cached talks. You should now be able to stop the talk-service and still be able to use the web app. If you start the Hystrix dashboard and connect to the conference-web app (simply access http://localhost:9000/hystrix/monitor?stream=localhost%3A8080%2Fhystrix.stream) then you should see the circuit breakers with their status and thread pools.
Where to go from here
This sample is really just the beginning. Some suggestions on how you can expand the sample with other Spring Cloud solutions (check the reference docs for more details):
- Add a dependency to the Spring Cloud Zuul starter and annotate your conference-web’s main class with @EnableZuulProxy. After that, you will be able to access the backing services through the conference web (e.g. at http://localhost:8080/talk-service/talk). Note that accessing the talk-service this way will automatically be done through a Hystrix command as well.
- Put the Git config repo in your own GitLab or GitHub repo and use Spring Cloud Bus to automatically refresh services when their configuration has changed.
- Use Spring Cloud Sleuth to add distributed tracing.
- Replace Eureka (and optionally the Spring Config server as well) with Consul and see what changes are needed.
Speaking of Consul, we’ll be publishing another blog post on using Consul here soon, so make sure to watch this space for updates!