Apache Dubbo 首個 Node.js 3.0-alpha 版本正式發佈

關於Apache Dubbo3

Apache Dubbo 是一款易用、高性能的 WEB 和 RPC 框架,同時爲構建企業級微服務提供服務發現、流量治理、可觀測、認證鑑權等能力、工具與最佳實踐。經過近幾年發展,Dubbo3 已在阿里巴巴集團各條業務線實現全面推廣,成功取代運行多年的 HSF 框架,同時 Dubbo3 的多語言體系也有了快速發展,目前涵蓋的多語言體系有

  • apache/dubbo[1] (java)
  • apache/dubbo-go[2]
  • apache/dubbo-js[3] (web、node.js)
  • apache/dubbo-rust[4]

基於 Dubbo3 定義的 Triple 協議,你可以輕鬆編寫瀏覽器、移動端、gRPC 兼容的 RPC 服務,並讓這些服務同時運行在 HTTP/1 和 HTTP/2 上。Dubbo Node.js SDK 支持使用 IDL 或編程語言特有的方式定義服務,並提供一套輕量的 API 來發布或調用這些服務。

關於 Dubbo3 Node.js 首個發佈版

Dubbo-js 項目於 9 月份剛剛發佈了支持 Dubbo3 協議的首個 alpha 版本,該項目是 Dubbo3 的 Typescript 版本實現,提供了 Web、Node.js 兩種發佈包。其中,Web 框架能讓開發者直接在瀏覽器頁面訪問後端服務,Node.js 則進一步豐富了後端微服務技術棧的選擇。當前 Node.js 版本主要是實現了 Triple 協議的完整支持,接下來的版本中,社區將繼續完善地址發現、負載均衡等服務治理能力。

Node.js 微服務開發完整示例

本示例基於最新發布的 Node.js 版本,演示了基於 Triple 協議的 RPC 通信模式,示例使用 Protocol Buffer 定義 RPC 服務,並演示了代碼生成、服務發佈和服務訪問等過程。

前置條件

因爲使用 Protocol Buffer 的原因,我們首先需要安裝相關的代碼生成工具,這包括 @bufbuild/protoc-gen-es、@bufbuild/protobuf、@apachedubbo/protoc-gen-apache-dubbo-es、@apachedubbo/dubbo。

npm install @bufbuild/protoc-gen-es @bufbuild/protobuf @apachedubbo/protoc-gen-apache-dubbo-es @apachedubbo/dubbo

定義服務

現在,使用 Protocol Buffer (IDL) 來定義一個 Dubbo 服務。

創建目錄,並生成文件:

mkdir -p proto && touch proto/example.proto

寫入內容:

syntax = "proto3";

package apache.dubbo.demo.example.v1;

message SayRequest {
  string sentence = 1;
}

message SayResponse {
  string sentence = 1;
}

service ExampleService {
  rpc Say(SayRequest) returns (SayResponse) {}
}

這個文件聲明瞭一個叫做 ExampleService 的服務,爲這個服務定義了 Say 方法以及它的請求參數 SayRequest 和返回值 SayResponse。

生成代碼

創建 gen 目錄,做爲生成文件放置的目標目錄。

mkdir -p gen

運行以下命令,在 gen 目錄下生成代碼文件:

PATH=$PATH:$(pwd)/node_modules/.bin \
  protoc -I proto \
  --es_out gen \
  --es_opt target=ts \
  --apache-dubbo-es_out gen \
  --apache-dubbo-es_opt target=ts \
  example.proto

運行命令後,應該可以在目標目錄中看到以下生成的文件:

├── gen
│   ├── example_dubbo.ts
│   └── example_pb.ts
├── proto
│   └── example.proto

實現服務

接下來我們就需要添加業務邏輯了,實現 ExampleService ,並將其註冊到 DubboRouter 中。

創建 dubbo.ts 文件:

import { DubboRouter } from "@apachedubbo/dubbo";
import { ExampleService } from "./gen/example_dubbo";

export default (router: DubboRouter) =>
  // registers apache.dubbo.demo.example.v1
  router.service(ExampleService, {
    // implements rpc Say
    async say(req) {
      return {
        sentence: `You said: ${req.sentence}`,
      };
    },
  }, { serviceGroup: 'dubbo', serviceVersion: '1.0.0' });

啓動 Server

Dubbo 服務可以嵌入到普通的 Node.js 服務器、Next.js、Express 或 Fastify 中。在這裏我們將使用 Fastify,所以讓我們安裝 Fastify 以及我們爲 Fastify 準備的插件。

npm install fastify @apachedubbo/dubbo-fastify

創建 server.ts 文件,新建一個 Server,把上一步中實現的 ExampleService 註冊給它。

接下來就可以直接初始化和啓動 Server 了,它將在指定的端口接收請求。

import { fastify } from "fastify";
import { fastifyDubboPlugin } from "@apachedubbo/dubbo-fastify";
import routes from "./dubbo";

async function main() {
  const server = fastify();
  await server.register(fastifyDubboPlugin, {
    routes,
  });
  server.get("/", (_, reply) => {
    reply.type("text/plain");
    reply.send("Hello World!");
  });
  await server.listen({ host: "localhost", port: 8080 });
  console.log("server is listening at", server.addresses());
}

void main();

最後,運行代碼啓動服務。

npx tsx server.ts

訪問服務

最簡單的方式是使用 HTTP/1.1 POST 請求訪問服務,參數則作以標準 JSON 格式作爲 HTTP 負載傳遞。如下是使用 cURL 命令的訪問示例:

curl \
 --header 'Content-Type: application/json' \
 --header 'TRI-Service-Version: 1.0.0' \
 --header 'TRI-Service-group: dubbo' \
 --data '{"sentence": "Hello World"}' \
 http://localhost:8080/apache.dubbo.demo.example.v1.ExampleService/Say

也可以使用標準的 Dubbo client 請求服務,我們首先需要從生成代碼即 dubbo-node 包中獲取服務代理,爲它指定 server 地址並初始化,之後就可以發起起 RPC 調用了。

創建 client.ts 文件。

import { createPromiseClient } from "@apachedubbo/dubbo";
import { ExampleService } from "./gen/example_dubbo";
import { createDubboTransport } from "@apachedubbo/dubbo-node";

const transport = createDubboTransport({
  baseUrl: "http://localhost:8080",
  httpVersion: "1.1",
});

async function main() {
  const client = createPromiseClient(ExampleService, transport, { serviceVersion: '1.0.0', serviceGroup: 'dubbo' });
  const res = await client.say({ sentence: "Hello World" });
  console.log(res);
}
void main();

運行客戶端:

npx tsx client.ts

總結

當前 Node.js 版本主要是實現了 Triple 協議的完整支持,接下來的版本中,社區將繼續完善地址發現、負載均衡等服務治理能力

相關鏈接:

[1] apache/dubbo

https://github.com/apache/dubbo

[2] apache/dubbo-go

https://github.com/apache/dubbo-go

[3] apache/dubbo-js

https://github.com/apache/dubbo-js

[4] apache/dubbo-rust

https://github.com/apache/dubbo-rust

作者:蔡建懌

點擊立即免費試用雲產品 開啓雲上實踐之旅!

原文鏈接

本文爲阿里雲原創內容,未經允許不得轉載。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章