×
Community Blog The First Node.js 3.0-Alpha Version of Apache Dubbo Is Officially Released

The First Node.js 3.0-Alpha Version of Apache Dubbo Is Officially Released

This article introduces the Node.js 3.0-Alpha Version of Apache Dubbo and gives a complete example of Node.js microservice development.

By Jianyi Cai

About Apache Dubbo 3

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.

1

About the First Release of Dubbo 3 Node.js

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.

The Example of Node.js Microservice Development

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.

Prerequisites

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

Define Services

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.

Generate Code

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

Implement Services

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

Start Server

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

Access Services

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

Summary

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.

Reference

[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

0 1 0
Share on

You may also like

Comments