1. Overview
Spring Cloud brings a wide range of features and libraries like client-side load balancing, service registry/discovery, concurrency control, and config server. On the other hand, in the microservice world, having polyglot services written with different languages and frameworks is a common practice. So, what if we like to take advantage of Spring Cloud in the whole ecosystem? Spring Cloud Netflix Sidecar is the solution here.
In this tutorial, we’ll learn more about Spring Cloud Sidecar with working examples.
2. What Is Spring Cloud Sidecar?
Cloud Netflix Sidecar is inspired by Netflix Prana and can be used as a utility to ease the use of service registry for services written in non-JVM languages and improve the interoperability of endpoints within the Spring Cloud ecosystem.
With Cloud Sidecar, a non-JVM service can be registered in the service registry. Moreover, the service can also use service discovery to find other services or even access the config server through host lookup or Zuul Proxy. The only requirement for a non-JVM service to be able to be integrated is having a standard health check endpoint available.
3. Sample Application
Our sample use case consists of 3 applications. To show the best of the Cloud Netflix Sidecar, we’ll create a /hello endpoint in NodeJS and then expose it via a Spring application called sidecar to our ecosystem. We’ll also develop another Spring Boot application to echo the /hello endpoint responses with the use of service discovery and Zuul.
With this project, we aim to cover two flows for the request:
- the user calls the echo endpoint on the echo Spring Boot application. The echo endpoint uses DiscoveryClient to look up the hello service URL from Eureka, i.e., the URL pointing to the NodeJS service. Then the echo endpoint calls the hello endpoint on the NodeJS application
- the user calls the hello endpoint directly from the echo application with the help of Zuul Proxy
3.1. NodeJS Hello Endpoint
Let’s start by creating a JS file called hello.js. We’re using express to serve our hello requests. In our hello.js file, we have introduced three endpoints – the default “/” endpoint, the /hello endpoint, and a /health endpoint, to fulfill the Spring Cloud Sidecar requirements:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/health', (req, res) => {
res.send({ "status":"UP"})
})
app.get('/hello/:me', (req, res) => {
res.send('Hello ' + req.params.me + '!')
})
app.listen(port, () => {
console.log(`Hello app listening on port ${port}`)
})
Next, we’ll install express:
npm install express
And finally, let’s start our application:
node hello.js
As the application’s up, let’s curl the hello endpoint:
curl http://localhost:3000/hello/baeldung
Hello baeldung!
And then, we test the health endpoint:
curl http://localhost:3000/health
status":"UP"}
As we have our node application ready for the next step, we are going to Springify it.
3.2. Sidecar Application
First, we need to have a Eureka Server up. After Eureka Server is started, we can access it at: http://127.0.0.1:8761
Let’s add spring-cloud-netflix-sidecar as a dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-sidecar</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
It’s important to note that the latest version of spring-cloud-netflix-sidecar at this time is 2.2.10.RELEASE, and it only supports spring boot 2.3.12.RELEASE. Therefore, the latest version of Spring Boot is not compatible with Netflix Sidecar at this moment.
Then let’s implement our Spring Boot application class with sidecar enabled:
@SpringBootApplication
@EnableSidecar
public class SidecarApplication {
public static void main(String[] args) {
SpringApplication.run(SidecarApplication.class, args);
}
}
For the next step, we’ve to set properties for connecting to Eureka. Furthermore, we set the sidecar config with port and health URI of our NodeJS hello app:
server.port: 8084
spring:
application:
name: sidecar
eureka:
instance:
hostname: localhost
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka
healthcheck:
enabled: true
sidecar:
port: 3000
health-uri: http://localhost:3000/health
Now we can start our application. After the successful start of our application, Spring registers a service with the given name “hello” in the Eureka Server.
To check if it works, we can access the endpoint: http://localhost:8084/hosts/sidecar.
@EnableSidecar is more than a marker for registering the side service with Eureka. It also causes @EnableCircuitBreaker and @EnableZuulProxy to be added, and subsequently, our Spring Boot application benefits from Hystrix and Zuul.
Now, as we have our Spring application ready, let’s go to the next step and see how the communication between services in our ecosystem works.
3.3. Echo Application Also Says Hello!
For the echo application, we’ll create an endpoint that calls the NodeJS hello endpoint with the help of service discovery. Moreover, we’ll enable Zuul Proxy to show other options for communication between these two services.
First, let’s add the dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
*To be consistent with the sidecar application, we use in the echo application the same version of 2.2.10.RELEASE for both dependencies to spring-cloud-starter-netflix-zuul and spring-cloud-starter-netflix-eureka-client.*
Then let’s create the Spring Boot main class and enable Zuul Proxy:
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class EchoApplication {
// ...
}
And then, we configure the Eureka client as we have done in the previous section:
server.port: 8085
spring:
application:
name: echo
eureka:
instance:
hostname: localhost
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka
...
Next, we start our echo application. After it starts, we can check the interoperability between our two services.
To check the sidecar application, let’s query it for the metadata of the echo service:
curl http://localhost:8084/hosts/echo
Then to verify if the echo application can call the NodeJS endpoint exposed by the sidecar application, let’s use the magic of the Zuul Proxy and curl this URL:
curl http://localhost:8085/sidecar/hello/baeldung
Hello baeldung!
As we have verified everything is working, let’s try another way to call the hello endpoint. First, we’ll create a controller in the echo application and inject DiscoveryClient. Then we add a GET endpoint that uses the DiscoveryClient to query hello service and calls it with RestTemplate:
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/hello/{me}")
public ResponseEntity<String> echo(@PathVariable("me") String me) {
List<ServiceInstance> instances = discoveryClient.getInstances("sidecar");
if (instances.isEmpty()) {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("hello service is down");
}
String url = instances.get(0).getUri().toString();
return ResponseEntity.ok(restTemplate.getForObject(url + "/hello/" + me, String.class));
}
Let’s restart the echo application and execute this curl to verify the echo endpoint called from the echo application:
curl http://localhost:8085/hello/baeldung
Hello baeldung!
Or to make it a bit more interesting, call it from the sidecar application:
curl http://localhost:8084/echo/hello/baeldung
Hello baeldung!
4. Conclusion
In this article, we learned about Cloud Netflix Sidecar and built a working sample with NodeJS and two Spring applications to show its usage in a Spring ecosystem.
As always, the complete code for the examples is available over on GitHub.