知識庫 / Spring / Spring Cloud RSS 訂閱

Spring Cloud Sidecar 簡介

Spring Cloud
HongKong
5
12:02 PM · Dec 06 ,2025

1. 概述

Spring Cloud 提供了廣泛的功能和庫,例如客户端負載均衡、服務註冊/發現、併發控制和配置服務器。另一方面,在微服務世界中,使用不同語言和框架編寫的“聚多語言服務”是一種常見的做法。那麼,如果我們想充分利用 Spring Cloud 在整個生態系統中,該怎麼辦? Spring Cloud Netflix Sidecar 就是解決方案。

在本教程中,我們將通過實際示例學習更多關於 Spring Cloud Sidecar 的內容。

2. 什麼是 Spring Cloud Sidecar?

Cloud Netflix Sidecar 靈感來源於 Netflix Prana,可作為工具,簡化非 JVM 語言編寫的服務對服務註冊表的利用,並提高 Spring Cloud 生態系統中端點之間的互操作性。

藉助 Cloud Sidecar,非 JVM 服務可以註冊到服務註冊表中。 此外,該服務還可以使用服務發現來查找其他服務,甚至可以通過主機查找或 Zuul Proxy 訪問配置服務器。 非 JVM 服務能夠被集成所必需的唯一要求是提供標準健康檢查端點。

3. 示例應用程序

我們的示例用例包含 3 個應用程序。為了充分展示 Cloud Netflix Sidecar 的優勢,我們將在一個 NodeJS 應用程序中創建一個 /hello 端點,然後通過一個名為 sidecar 的 Spring 應用程序將其暴露到我們的生態系統中。我們還將開發另一個 Spring Boot 應用程序,使用服務發現和 Zuul 來回顯 /hello 端點的響應。

通過這個項目,我們旨在覆蓋兩個請求流程:

  • 用户調用回顯應用程序上的回顯端點。回顯端點使用 DiscoveryClient 從 Eureka 中查找 hello 服務的 URL,即指向 NodeJS 服務的 URL。然後,回顯端點調用 NodeJS 應用程序上的 hello 端點。
  • 用户藉助 Zuul Proxy 直接從回顯應用程序上調用 hello 端點。

3.1. NodeJS Hello Endpoint

讓我們首先創建一個名為 hello.js的JS文件。我們使用 express來處理我們的Hello請求。在 hello.js文件中,我們引入了三個Endpoint——默認“/” Endpoint、/hello Endpoint以及/health Endpoint,以滿足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}`)
})

接下來,我們將安裝 express

npm install express

最後,我們開始我們的應用程序:

node hello.js

當應用程序啓動後,讓我們使用 curl 調用 hello 端點:

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

然後,我們測試健康端點:

curl http://localhost:3000/health
status":"UP"}

由於我們的 Node.js 應用程序已準備好進入下一步,我們將對其進行 Spring 改造。

3.2. 側載應用

首先,我們需要啓動 Eureka Server。啓動 Eureka Server 後,可以通過以下地址訪問:http://127.0.0.1:8761

接下來,添加 spring-cloud-netflix-sidecar 作為依賴:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-sidecar</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

請注意,目前 spring-cloud-netflix-sidecar 的最新版本是 2.2.10.RELEASE,它僅支持 Spring Boot 2.3.12.RELEASE。因此,最新版本的 Spring Boot 與 Netflix Sidecar 目前不兼容。

接下來,讓我們使用啓用了 sidecar 的 Spring Boot 應用程序類:

@SpringBootApplication
@EnableSidecar
public class SidecarApplication {
    public static void main(String[] args) {
        SpringApplication.run(SidecarApplication.class, args);
    }
}

下一步,我們需要設置連接 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

現在我們可以啓動我們的應用程序。在應用程序成功啓動後,Spring 會在 Eureka Server 上註冊一個名為“hello”的服務。

要檢查其是否正常工作,我們可以訪問以下端點:http://localhost:8084/hosts/sidecar

@EnableSidecar不僅僅是一個標記,用於在 Eureka Server 上註冊 side service。它還會導致@EnableCircuitBreaker@EnableZuulProxy被添加,從而使我們的 Spring Boot 應用程序受益於 Hystrix 和 Zuul。

現在,由於我們已經準備好 Spring 應用程序,讓我們繼續下一步,看看我們生態系統中的服務間通信是如何工作的。

3.3. 鏡像應用也説“你好!”

對於鏡像應用,我們將使用服務發現機制調用 NodeJS 的 “hello” 端點,創建一個端點。 此外,我們還將啓用 Zuul Proxy 以展示這兩個服務之間通信的其他選項。

首先,讓我們添加依賴項:

 <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>

為了保持與側載應用程序的一致性,我們同時在 echo 應用程序中使用 2.2.10.RELEASE 版本的所有依賴項,包括 spring-cloud-starter-netflix-zuulspring-cloud-starter-netflix-eureka-client

接下來,我們創建 Spring Boot 主類並啓用 Zuul 代理:

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class EchoApplication {
    // ...
}

然後,我們按照之前章節中所做的那樣配置 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
 ...

接下來,我們開始啓動我們的 echo 應用程序。啓動後,我們可以檢查我們兩個服務之間的互操作性。

為了檢查 sidecar 應用程序,我們查詢其關於 echo 服務的元數據:

curl http://localhost:8084/hosts/echo

然後為了驗證 echo 應用是否可以調用 sidecar 應用暴露的 NodeJS 端點,我們使用 Zuul Proxy 的魔法並使用 curl 命令訪問以下 URL:

curl http://localhost:8085/sidecar/hello/baeldung
Hello baeldung!

鑑於我們已驗證所有功能正常,我們現在嘗試另一種方式調用 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));
}

讓我們重新啓動 echo 應用程序,並執行以下 curl 命令來驗證 echo 應用程序調用的 echo 端點:

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

為了讓它更具吸引力,可以從側載應用程序中調用它:

curl http://localhost:8084/echo/hello/baeldung
Hello baeldung!

4. 結論

在本文中,我們學習了 Cloud Netflix Sidecar 的概念,並使用 NodeJS 和兩個 Spring 應用構建了一個可運行的示例,以展示其在 Spring 生態系統中的應用。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.