Introduction to Spring Cloud Sidecar – Spring Cloud Sidecar简介

最后修改: 2022年 5月 31日

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.

Spring Cloud带来了广泛的功能和库,如客户端负载均衡、服务注册表/发现、并发控制和配置服务器。另一方面,在微服务领域,拥有用不同语言和框架编写的多语言服务是一种常见的做法。那么,如果我们喜欢在整个生态系统中利用Spring Cloud的优势呢?Spring Cloud Netflix Sidecar是这里的解决方案。

In this tutorial, we’ll learn more about Spring Cloud Sidecar with working examples.

在本教程中,我们将通过工作实例进一步了解Spring Cloud Sidecar。

2. What Is Spring Cloud Sidecar?

2.什么是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.

云Netflix Sidecar的灵感来自于Netflix Prana,它可以作为一种实用工具,为用非JVM语言编写的服务简化服务注册表的使用,并提高Spring Cloud生态系统内的端点的互操作性。

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.

通过Cloud Sidecar,非JVM服务可以在服务注册表中注册。此外,该服务还可以使用服务发现来寻找其他服务,甚至通过主机查询或Zuul Proxy来访问配置服务器。非JVM服务能够被集成的唯一要求是拥有一个可用的标准健康检查端点。

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.

我们的示例用例由3个应用程序组成。 为了展示Cloud Netflix Sidecar的优点,我们将在NodeJS中创建一个/hello端点,然后通过一个名为sidecar的Spring应用程序将其暴露给我们的生态系统。我们还将开发另一个Spring Boot应用程序,通过使用服务发现和Zuul来呼应/hello端点的响应。

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:

让我们先创建一个名为hello.js的JS文件。我们使用express来服务我们的hello请求。在我们的hello.js文件中,我们引入了三个端点–默认的”/”端点、/hello端点和一个/health端点,以满足Spring Cloud Sidecar的要求。

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 ' + + '!')

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 hello端点。

curl http://localhost:3000/hello/baeldung
Hello baeldung!

And then, we test the health endpoint:


curl http://localhost:3000/health

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:


Let’s add spring-cloud-netflix-sidecar as a 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.

需要注意的是,目前最新的spring-cloud-netflix-sidecar版本是2.2.10.RELEASE,而它只支持spring boot2.3.12.RELEASE。因此,最新版本的Spring Boot目前与Netflix Sidecar不兼容。

Then let’s implement our Spring Boot application class with sidecar enabled:

然后让我们在启用sidecar的情况下实现我们的Spring Boot应用类。

public class SidecarApplication {
    public static void main(String[] args) {, 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:

对于下一步,我们必须设置连接到Eureka的属性。此外,我们用我们的NodeJS hello应用的端口和健康URI来设置sidecar配置。

server.port: 8084
    name: sidecar
    hostname: localhost
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
      enabled: true
  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.

现在我们可以启动我们的应用程序了。在我们的应用程序成功启动后,Spring在Eureka服务器中注册了一个名字为 “hello “的服务。

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.

@EnableSidecar不仅仅是向Eureka注册侧面服务的一个标记。它还导致@EnableCircuitBreaker@EnableZuulProxy被添加,随后,我们的Spring Boot应用程序从HystrixZuul受益。

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.

对于回声应用,我们将创建一个端点,在服务发现的帮助下调用NodeJS hello端点。此外,我们将启用Zuul Proxy来显示这两个服务之间的其他通信选项。

First, let’s add the dependencies:



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:

然后让我们创建Spring Boot主类并启用Zuul代理。

public class EchoApplication {
    // ...

And then, we configure the Eureka client as we have done in the previous section:


server.port: 8085
    name: echo
    hostname: localhost
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2

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:


DiscoveryClient discoveryClient;

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.

在这篇文章中,我们了解了Cloud Netflix Sidecar,并使用NodeJS和两个Spring应用程序建立了一个工作样本,以展示其在Spring生态系统中的应用。

As always, the complete code for the examples is available over on GitHub.