Spring Cloud Sidecar is part of Spring Cloud Netflix project that lets you setup a mediator beetwen your Spring Cloud system and any other application – non necessary written in a JVM language (generally other application that can’t easly integrate with Spring Cloud). The original idea was introduced by Netflix Prana project. This can be especially usefull when working with polyglot projects.
By setting Spring Cloud Sidecar you can gain bilateral benefits. First of all your cloud becomes aware of the application – it becomes discoverable and for instance you can monitor it through automated health checks. Second of all, all the services that are registered within cloud can be easier consumed be the service. You could lookup other service ip addresses or read configuration from central configuration server.
Setting sidecar project is fairly easy task to do, you only need a running discovery service At this point Spring Cloud gives you two options – you can use Netflix Eureka or HashiCorp Consul (though there is ongoing efford to add Zookeper integration). I guess that there is a lot of materials on the web about setting up Eureka server within Spring Cloud project, so let’s do an experiment and use Consul instead.
Reminder: When writing this post the Spring Cloud Consul project was available only as M1, so I am using it only for showcasing.
Let’s start by setting up the project, let’s use Node as our runtime, although we could use any other language instead. I’m going to make my life easier here and use a Yeoman generator:
$ npm i -g generator-express-sidecar $ yo express-sidecar
Choose your project name and select Consul as your discovery service.
You will be asked to provide the host and port, but you may relay on the default’s. The generator will install all of the Node package dependencies and will also download the dependencies for the sidecar project and assemble the project.
The project structure looks as fallows:
. ├── app │ ├── app.js │ ├── bin │ ├── package.json │ ├── public │ ├── routes │ └── views └── sidecar ├── build.gradle ├── gradle ├── gradlew ├── gradlew.bat └── src
The app directory is a simple Express application, there is nothing extraordinary about it. The sidecar has our Spring Cloud mediator.
Before we continue we will need to run Consul. On OS X all you need to do is execute to install it:
brew cask install consul
On other systems you may need to download the binary distribution and install it manually. There is one optional step – you can download the UI interface for the Consul agent available here for download. Unzip it in anywhere, the path will be specified when starting the agent.
We can now run the consul agent:
$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -ui-dir /consul/ui
You will have to adjust the -ui-dir parameter respectively.
Now, with consul running let’s start our application:
$ cd app && DEBUG=app:* npm start
Next is the sidecar project:
$ cd sidecar && ./gradlew bootRun
Let’s verify that the application has been registered in Consul, if you have installed the ui module, navigate to http://localhost:8500/ui/. If everything went successfully, your dashboard should look similar:
In logs, you should see some health checks that sidecar does for you automaticly. You may want to also investigate the sidecar home page where you will find the autogenerated links to registered services that you can consume from within the Node application.
I hope that you enjoyed this short introduction, I plan to explore this a bit more and maybe in future post additional blog posts regarding this topic.