By Jianyi Cai
Apache Dubbo is an easy-to-use, high-performance WEB and RPC framework. It provides capabilities, tools, and best practices for building enterprise-level microservices, such as service discovery, traffic governance, observability, and authentication. After years of development, Dubbo 3 has been fully promoted in all business lines of Alibaba Group, successfully replacing the long-standing HSF framework. Additionally, Dubbo 3's multilingual system has rapidly expanded and now covers the following languages:
• apache/dubbo1
• apache/dubbo-go[2]
• apache/dubbo-js 3
• apache/dubbo-rust[4]
Based on the Triple protocol defined by Dubbo 3, you can easily write RPC services that are compatible with browsers, mobile terminals, and gRPC, and run these services on both HTTP/1 and HTTP/2. The Dubbo Node.js SDK supports defining services using IDL or language-specific methods, and provides a lightweight set of APIs for publishing or invoking these services.
In September, the Dubbo-js project released its first alpha version that supports the Dubbo 3 protocol. This project is the Typescript implementation of Dubbo 3 and provides two release packages: Web and Node.js. The Web framework allows developers to directly access backend services on browser pages, while the Node.js framework offers more choices for backend microservice technology stacks. The current Node.js version mainly supports the Triple protocol. In future versions, the community will continue to enhance service governance capabilities such as address discovery and load balancing.
This example is based on the latest released Node.js version and demonstrates RPC communication based on the Triple protocol. It uses Protocol Buffers to define an RPC service and showcases the procedures of code generation, service publishing, and service access.
As Protocol Buffer will be used, you need to first install code generation tools such as @bufbuild/protoc-gen-es
, @bufbuild/protobuf
, @apachedubbo/protoc-gen-apache-dubbo-es
, and @apachedubbo/dubbo
.
npm install @bufbuild/protoc-gen-es @bufbuild/protobuf @apachedubbo/protoc-gen-apache-dubbo-es @apachedubbo/dubbo
Now, use Protocol Buffer (IDL) to define a Dubbo service.
Create a directory and generate a file:
mkdir -p proto && touch proto/example.proto
Enter the following content:
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 file declares a service called ExampleService, and defines a Say method for this service along with its request parameter SayRequest and returned value SayResponse.
Create a gen directory as the target directory for the generated files.
mkdir -p gen
Run the following command to generate a code file in the gen directory:
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
After running the command, you should see the following generated files in the target directory:
├── gen
│ ├── example_dubbo.ts
│ └── example_pb.ts
├── proto
│ └── example.proto
Next, we need to add business logic to implement ExampleService, and register it with DubboRouter.
Create a dubbo.ts file:
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' });
Dubbo services can be embedded in a common Node.js server, Next.js, Express, or Fastify. Here, we will use Fastify as an example, so let's install Fastify and the plugins we have prepared for Fastify.
npm install fastify @apachedubbo/dubbo-fastify
Create a server.ts file, create a new Server, and register the ExampleService implemented in the previous step with it.
Next, you can directly initialize and start the Server, which will receive requests on the specified port.
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();
Finally, run the code to start the service.
npx tsx server.ts
The simplest way is to access the service using an HTTP/1.1 POST request, and the parameters are passed as an HTTP payload in standard JSON format. The following is an example of access using the cURL command:
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
You can also use the standard Dubbo client to request services. We first need to obtain the service proxy from the generated code, that is, the dubbo-node package, and then specify the server address for it and initialize it. By doing so, we can initiate RPC calls.
Create a client.ts file.
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.ts
The current version of Node.js primarily focuses on implementing complete support for the Triple protocol. In upcoming versions, the community will further enhance service governance capabilities, including address discovery and load balancing.
[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
Coordinated sharing of CPU resources in Colocation Scenarios - Fine-grained CPU Orchestration
New Features of Grafana 10: Experience and Collaboration Enhancement
507 posts | 48 followers
FollowAlibaba Cloud Native Community - December 1, 2023
Alibaba Cloud Native Community - September 12, 2023
Alibaba Cloud Native Community - January 26, 2024
Aliware - August 18, 2021
Aliware - May 13, 2019
Alibaba Cloud Native Community - July 20, 2023
507 posts | 48 followers
FollowMSE provides a fully managed registration and configuration center, and gateway and microservices governance capabilities.
Learn MoreA fully-managed Apache Kafka service to help you quickly build data pipelines for your big data analytics.
Learn MoreRealtime Compute for Apache Flink offers a highly integrated platform for real-time data processing, which optimizes the computing of Apache Flink.
Learn MoreMulti-source metrics are aggregated to monitor the status of your business and services in real time.
Learn MoreMore Posts by Alibaba Cloud Native Community