Apache Dubbo’s first Node.js 3.0-alpha version officially released

Author: Cai Jianyi

About Apache Dubbo3

Apache Dubbo is an easy-to-use, high-performance WEB and RPC framework that also provides capabilities, tools, and best practices for building enterprise-level microservices such as service discovery, traffic management, observability, and authentication. After several years of development, Dubbo3 has been fully promoted in all business lines of Alibaba Group, successfully replacing the HSF framework that has been running for many years. At the same time, Dubbo3’s multi-language system has also developed rapidly. The multi-language systems currently covered are:

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

Based on the Triple protocol defined by Dubbo3, you can easily write browser, mobile, and gRPC-compatible RPC services, and have these services run on HTTP/1 and HTTP/2 at the same time. Dubbo Node.js SDK supports defining services using IDL or programming language-specific methods, and provides a set of lightweight APIs to publish or call these services.

Picture

About Dubbo3 Node.js first release

The Dubbo-js project just released its first alpha version supporting the Dubbo3 protocol in September. The project is a Typescript version implementation of Dubbo3 and provides two release packages: Web and Node.js. Among them, the web framework allows developers to access back-end services directly on the browser page, and Node.js further enriches the choice of back-end microservice technology stacks. The current Node.js version mainly implements complete support for the Triple protocol. In the next version, the community will continue to improve service governance capabilities such as address discovery and load balancing. The dubbo-js project is currently developing rapidly. Developers who are interested in participating in the apache/dubbo-js project are welcome to search DingTalk group: 29775027779 to join the developer group.

Complete example of Node.js microservice development

This example is based on the latest version of Node.js and demonstrates the RPC communication mode based on the Triple protocol. The example uses Protocol Buffer to define RPC services and demonstrates the processes of code generation, service publishing, and service access.

Precondition

Because of using Protocol Buffer, we first need to install related code generation tools, including @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

Define services

Now, use Protocol Buffer (IDL) to define a Dubbo service.

Create a directory and generate files:

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

Write 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 the Say method and its request parameter SayRequest and return value SayResponse for this service.

Generate code

Create a gen directory as the target directory where generated files will be placed.

mkdir -p gen

Run the following command to generate code files 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
├── prototype
│ └── example.proto

Implementing services

Next we need to add business logic, implement ExampleService, and register it in DubboRouter.

Create 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' });

Start Server

Dubbo services can be embedded into regular Node.js servers, Next.js, Express or Fastify. Here we will be using Fastify, so let’s install Fastify and the plugins we have for Fastify.

npm install fastify @apachedubbo/dubbo-fastify

Create the server.ts file, create a new Server, and register the ExampleService implemented in the previous step to 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

Access Services

The simplest way is to access the service using an HTTP/1.1 POST request, with the parameters passed as the 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, specify the server address for it and initialize it. After that, you can initiate an RPC call.

Create 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

Summary

The current Node.js version mainly implements complete support for the Triple protocol. In the next version, the community will continue to improve service governance capabilities such as address discovery and load balancing. The dubbo-js project is currently developing rapidly. Developers who are interested in participating in the apache/dubbo-js project are welcome to search DingTalk group: 29775027779 to join the developer group.

Related links:

[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