關(guān)于Apache Dubbo3
Apache Dubbo 是一款易用、高性能的 WEB 和 RPC 框架,同時(shí)為構(gòu)建企業(yè)級(jí)微服務(wù)提供服務(wù)發(fā)現(xiàn)、流量治理、可觀測(cè)、認(rèn)證鑒權(quán)等能力、工具與最佳實(shí)踐。經(jīng)過近幾年發(fā)展,Dubbo3 已在阿里巴巴集團(tuán)各條業(yè)務(wù)線實(shí)現(xiàn)全面推廣,成功取代運(yùn)行多年的 HSF 框架,同時(shí) Dubbo3 的多語言體系也有了快速發(fā)展,目前涵蓋的多語言體系有
- apache/dubbo[1]?(java)
- apache/dubbo-go[2]
- apache/dubbo-js[3]?(web、node.js)
- apache/dubbo-rust[4]
基于 Dubbo3 定義的 Triple 協(xié)議,你可以輕松編寫瀏覽器、移動(dòng)端、gRPC 兼容的 RPC 服務(wù),并讓這些服務(wù)同時(shí)運(yùn)行在 HTTP/1 和 HTTP/2 上。Dubbo Node.js SDK 支持使用 IDL 或編程語言特有的方式定義服務(wù),并提供一套輕量的 API 來發(fā)布或調(diào)用這些服務(wù)。
關(guān)于 Dubbo3 Node.js 首個(gè)發(fā)布版
Dubbo-js 項(xiàng)目于 9 月份剛剛發(fā)布了支持 Dubbo3 協(xié)議的首個(gè) alpha 版本,該項(xiàng)目是 Dubbo3 的 Typescript 版本實(shí)現(xiàn),提供了 Web、Node.js 兩種發(fā)布包。其中,Web 框架能讓開發(fā)者直接在瀏覽器頁面訪問后端服務(wù),Node.js 則進(jìn)一步豐富了后端微服務(wù)技術(shù)棧的選擇。當(dāng)前 Node.js 版本主要是實(shí)現(xiàn)了 Triple 協(xié)議的完整支持,接下來的版本中,社區(qū)將繼續(xù)完善地址發(fā)現(xiàn)、負(fù)載均衡等服務(wù)治理能力。
Node.js 微服務(wù)開發(fā)完整示例
本示例基于最新發(fā)布的 Node.js 版本,演示了基于 Triple 協(xié)議的 RPC 通信模式,示例使用 Protocol Buffer 定義 RPC 服務(wù),并演示了代碼生成、服務(wù)發(fā)布和服務(wù)訪問等過程。
前置條件
因?yàn)槭褂?Protocol Buffer 的原因,我們首先需要安裝相關(guān)的代碼生成工具,這包括 @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
定義服務(wù)
現(xiàn)在,使用 Protocol Buffer (IDL) 來定義一個(gè) Dubbo 服務(wù)。
創(chuàng)建目錄,并生成文件:
mkdir -p proto && touch proto/example.proto
寫入內(nèi)容:
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) {}
}
這個(gè)文件聲明了一個(gè)叫做 ExampleService 的服務(wù),為這個(gè)服務(wù)定義了 Say 方法以及它的請(qǐng)求參數(shù) SayRequest 和返回值 SayResponse。
生成代碼
創(chuàng)建 gen 目錄,做為生成文件放置的目標(biāo)目錄。
mkdir -p gen
運(yùn)行以下命令,在 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
運(yùn)行命令后,應(yīng)該可以在目標(biāo)目錄中看到以下生成的文件:
├── gen
│ ├── example_dubbo.ts
│ └── example_pb.ts
├── proto
│ └── example.proto
實(shí)現(xiàn)服務(wù)
接下來我們就需要添加業(yè)務(wù)邏輯了,實(shí)現(xiàn) ExampleService ,并將其注冊(cè)到 DubboRouter 中。
創(chuàng)建 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' });
啟動(dòng) Server
Dubbo 服務(wù)可以嵌入到普通的 Node.js 服務(wù)器、Next.js、Express 或 Fastify 中。在這里我們將使用 Fastify,所以讓我們安裝 Fastify 以及我們?yōu)?Fastify 準(zhǔn)備的插件。
npm install fastify @apachedubbo/dubbo-fastify
創(chuàng)建 server.ts 文件,新建一個(gè) Server,把上一步中實(shí)現(xiàn)的 ExampleService 注冊(cè)給它。
接下來就可以直接初始化和啟動(dòng) Server 了,它將在指定的端口接收請(qǐng)求。
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();
最后,運(yùn)行代碼啟動(dòng)服務(wù)。
npx tsx server.ts
訪問服務(wù)
最簡(jiǎn)單的方式是使用 HTTP/1.1 POST 請(qǐng)求訪問服務(wù),參數(shù)則作以標(biāo)準(zhǔn) JSON 格式作為 HTTP 負(fù)載傳遞。如下是使用 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
也可以使用標(biāo)準(zhǔn)的 Dubbo client 請(qǐng)求服務(wù),我們首先需要從生成代碼即 dubbo-node 包中獲取服務(wù)代理,為它指定 server 地址并初始化,之后就可以發(fā)起起 RPC 調(diào)用了。
創(chuàng)建 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();
運(yùn)行客戶端:
npx tsx client.ts
總結(jié)
當(dāng)前 Node.js 版本主要是實(shí)現(xiàn)了 Triple 協(xié)議的完整支持,接下來的版本中,社區(qū)將繼續(xù)完善地址發(fā)現(xiàn)、負(fù)載均衡等服務(wù)治理能力
相關(guān)鏈接:
[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
作者:蔡建懌
點(diǎn)擊立即免費(fèi)試用云產(chǎn)品 開啟云上實(shí)踐之旅!
原文鏈接文章來源:http://www.zghlxwxcb.cn/news/detail-718213.html
本文為阿里云原創(chuàng)內(nèi)容,未經(jīng)允許不得轉(zhuǎn)載。文章來源地址http://www.zghlxwxcb.cn/news/detail-718213.html
到了這里,關(guān)于Apache Dubbo 首個(gè) Node.js 3.0-alpha 版本正式發(fā)布的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!