Quick Start

Develop backend microservices using Node.js

Based on the Triple protocol defined by Dubbo, you can easily write browser and gRPC compatible RPC services that run simultaneously on HTTP/1 and HTTP/2. The Dubbo Node.js SDK supports defining services using IDL or language-specific methods and provides a lightweight API for publishing or invoking these services.

This example demonstrates the RPC communication model based on the Triple protocol, using Protocol Buffers to define RPC services, and illustrating processes such as code generation, service publishing, and service access.

Preconditions

Due to the use of Protocol Buffers, we first need to install the relevant code generation tools, including @bufbuild/protoc-gen-es, @bufbuild/protobuf, @apachedubbo/protoc-gen-apache-dubbo-es, @apachedubbo/dubbo.

  1. npm install @bufbuild/protoc-gen-es @bufbuild/protobuf @apachedubbo/protoc-gen-apache-dubbo-es @apachedubbo/dubbo

Define Service

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

Create a directory and generate a file.

  1. mkdir -p proto && touch proto/example.proto

Write the content.

  1. syntax = "proto3";
  2. package apache.dubbo.demo.example.v1;
  3. message SayRequest {
  4. string sentence = 1;
  5. }
  6. message SayResponse {
  7. string sentence = 1;
  8. }
  9. service ExampleService {
  10. rpc Say(SayRequest) returns (SayResponse) {}
  11. }

This file declares a service called ExampleService, defining a Say method along with its request parameter SayRequest and return value SayResponse.

Generate Code

Create a gen directory as the target directory for generated files.

  1. mkdir -p gen

Run the following command to generate code files in the gen directory.

  1. PATH=$PATH:$(pwd)/node_modules/.bin \
  2. protoc -I proto \
  3. --es_out gen \
  4. --es_opt target=ts \
  5. --apache-dubbo-es_out gen \
  6. --apache-dubbo-es_opt target=ts \
  7. example.proto

After running the command, you should see the following generated files in the target directory:

  1. ├── gen
  2. ├── example_dubbo.ts
  3. └── example_pb.ts
  4. ├── proto
  5. └── example.proto

Implement Service

Next, we’ll need to add business logic to implement ExampleService and register it with DubboRouter.

Create a dubbo.ts file.

  1. import { DubboRouter } from "@apachedubbo/dubbo";
  2. import { ExampleService } from "./gen/example_dubbo";
  3. export default (router: DubboRouter) =>
  4. // registers apache.dubbo.demo.example.v1
  5. router.service(ExampleService, {
  6. // implements rpc Say
  7. async say(req) {
  8. return {
  9. sentence: `You said: ${req.sentence}`,
  10. };
  11. },
  12. }, { serviceGroup: 'dubbo', serviceVersion: '1.0.0' });

Start Server

Dubbo services can be embedded into a regular Node.js server, Next.js, Express, or Fastify. Here we will use Fastify, so let’s install Fastify and the plugin we prepared for Fastify.

  1. npm install fastify @apachedubbo/dubbo-fastify

Create a server.ts file, create a Server, and register the implemented ExampleService from the previous step. Then initialize and start the Server, which will receive requests on the specified port.

  1. import { fastify } from "fastify";
  2. import { fastifyDubboPlugin } from "@apachedubbo/dubbo-fastify";
  3. import routes from "./dubbo";
  4. async function main() {
  5. const server = fastify();
  6. await server.register(fastifyDubboPlugin, {
  7. routes,
  8. });
  9. server.get("/", (_, reply) => {
  10. reply.type("text/plain");
  11. reply.send("Hello World!");
  12. });
  13. await server.listen({ host: "localhost", port: 8080 });
  14. console.log("server is listening at", server.addresses());
  15. }
  16. void main();

Finally, run the code to start the service.

  1. npx tsx server.ts

Access Service

The simplest way to access the service is to use an HTTP/1.1 POST request, passing parameters in standard JSON format as the HTTP payload. Below is an example using the cURL command:

  1. curl \
  2. --header 'Content-Type: application/json' \
  3. --header 'TRI-Service-Version: 1.0.0' \
  4. --header 'TRI-Service-group: dubbo' \
  5. --data '{"sentence": "Hello World"}' \
  6. http://localhost:8080/apache.dubbo.demo.example.v1.ExampleService/Say

You can also use a standard Dubbo client to request the service. We first need to get the service proxy from the generated code, i.e., the dubbo-node package, specify the server address for it, and initialize it before we can make the RPC call.

Create a client.ts file.

  1. import { createPromiseClient } from "@apachedubbo/dubbo";
  2. import { ExampleService } from "./gen/example_dubbo";
  3. import { createDubboTransport } from "@apachedubbo/dubbo-node";
  4. const transport = createDubboTransport({
  5. baseUrl: "http://localhost:8080",
  6. httpVersion: "1.1",
  7. });
  8. async function main() {
  9. const client = createPromiseClient(ExampleService, transport, { serviceVersion: '1.0.0', serviceGroup: 'dubbo' });
  10. const res = await client.say({ sentence: "Hello World" });
  11. console.log(res);
  12. }
  13. void main();

Run the client.

  1. npx tsx client.ts

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)