1. Overview
1.概述
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
3.申请书样本
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
3.1.NodeJS的Hello端点
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 ' + req.params.me + '!')
})
app.listen(port, () => {
console.log(`Hello app listening on port ${port}`)
})
Next, we’ll install express:
接下来,我们将安装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
status":"UP"}
As we have our node application ready for the next step, we are going to Springify it.
由于我们已经为下一步准备好了我们的节点应用程序,我们将对其进行Springify。
3.2. Sidecar Application
3.2.挎包应用
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
首先,我们需要建立一个Eureka服务器。在Eureka服务器启动后,我们可以通过以下方式访问它。http://127.0.0.1:8761
Let’s add spring-cloud-netflix-sidecar as a dependency:
让我们把spring-cloud-netflix-sidecar作为一个依赖项。
<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.
需要注意的是,目前最新的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应用类。
@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:
对于下一步,我们必须设置连接到Eureka的属性。此外,我们用我们的NodeJS hello应用的端口和健康URI来设置sidecar配置。
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.
现在我们可以启动我们的应用程序了。在我们的应用程序成功启动后,Spring在Eureka服务器中注册了一个名字为 “hello “的服务。
To check if it works, we can access the endpoint: http://localhost:8084/hosts/sidecar.
为了检查它是否工作,我们可以访问端点。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应用程序从Hystrix和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.
现在,我们已经准备好了我们的Spring应用程序,让我们进入下一个步骤,看看我们的生态系统中的服务之间的通信是如何工作的。
3.3. Echo Application Also Says Hello!
3.3.回声应用也说你好!
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:
首先,让我们添加依赖性。
<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.
为了与sidecar应用程序保持一致,我们在echo应用程序中使用相同版本的2.2.10.RELEASE,用于对spring-cloud-starter-netflix-zuul和spring-cloud-starter-netflix-eureka-client的依赖。 。
Then let’s create the Spring Boot main class and enable Zuul Proxy:
然后让我们创建Spring Boot主类并启用Zuul代理。
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class EchoApplication {
// ...
}
And then, we configure the Eureka client as we have done in the previous section:
然后,我们按照上一节的做法配置Eureka客户端。
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:
为了检查sidecar应用程序,让我们查询它的回声服务的元数据。
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:
然后,为了验证echo应用程序是否可以调用由sidecar应用程序暴露的NodeJS端点,让我们使用Zuul代理的魔力,curl这个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:
由于我们已经验证了一切都在工作,让我们尝试另一种方式来调用hello端点。首先,我们将在echo应用程序中创建一个控制器并注入DiscoveryClient。然后我们添加一个GET端点,使用DiscoveryClient来查询hello服务,并用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:
让我们重新启动echo程序并执行这个curl来验证从echo程序调用的echo端点。
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
4.总结
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.
像往常一样,例子的完整代码可在GitHub上获得,。