Build a Node.js Microservice with Apache Dubbo3: Full Triple‑Protocol Example
This tutorial walks through setting up Apache Dubbo3’s Triple protocol in Node.js, from installing code‑generation tools and defining a protobuf service to generating TypeScript stubs, implementing the service, launching a Fastify server, and invoking the RPC via curl or a Dubbo client.
Overview
Apache Dubbo is a high‑performance web and RPC framework that provides service discovery, traffic governance, observability, authentication, and other enterprise‑grade capabilities. Dubbo 3 adds multi‑language support (Java, Go, JavaScript/Node.js, Rust) and introduces the Triple protocol.
Triple Protocol and Node.js SDK
The Triple protocol allows RPC services to run over both HTTP/1 and HTTP/2 and be compatible with browsers, mobile clients, and gRPC. The Dubbo‑js SDK (TypeScript) lets developers define services via Protocol Buffers (IDL) or code‑first and offers a lightweight API for publishing and invoking services.
Prerequisites
Install the protobuf code‑generation tools required for the Dubbo plugins:
npm install @bufbuild/protoc-gen-es @bufbuild/protobuf @apachedubbo/protoc-gen-apache-dubbo-es @apachedubbo/dubboDefine the Service
Create a proto directory and an example.proto file with the following definition:
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) {}
}This declares ExampleService with a single RPC method Say.
Generate TypeScript Stubs
Create a gen directory for generated files and run protoc with the Dubbo plugins:
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.protoThe command produces:
├── gen
│ ├── example_dubbo.ts
│ └── example_pb.ts
├── proto
│ └── example.protoImplement the Service
Create dubbo.ts and register the implementation with a DubboRouter:
import { DubboRouter } from "@apachedubbo/dubbo";
import { ExampleService } from "./gen/example_dubbo";
export default (router: DubboRouter) =>
router.service(ExampleService, {
async say(req) {
return { sentence: `You said: ${req.sentence}` };
},
}, { serviceGroup: 'dubbo', serviceVersion: '1.0.0' });Start a Fastify Server
Install Fastify and the Dubbo Fastify plugin, then create server.ts:
npm install fastify @apachedubbo/dubbo-fastify 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();Run the server:
npx tsx server.tsInvoke the Service via HTTP
Send a JSON POST request to the RPC endpoint:
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/SayInvoke the Service with a Dubbo Client
Create client.ts that uses the generated stubs and a Dubbo transport:
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();Run the client:
npx tsx client.tsFuture Work
The current Node.js release fully supports the Triple protocol. Upcoming releases will add address discovery, load balancing, and other service‑governance features.
References
https://github.com/apache/dubbo
https://github.com/apache/dubbo-go
https://github.com/apache/dubbo-js
https://github.com/apache/dubbo-rust
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
