preface

With the popularity of BFF, more and more applications use Nodejs as the middle layer to provide SSR, WEB API aggregation and other capabilities, while the backend like Java is more inclined to service sinking, and if the call between Node and Java goes through HTTP, Performance is not as efficient as RPC. Here are some examples of node and Java using the Dubbo protocol (ws/GRPC will follow)

The installation

The Dubo-Provider extension provided by daze. Js framework is used here to provide dubbo communication capabilities

Generated projects:

$ npx @dazejs/cli create dubbo_project
Copy the code

Install extensions:

$ cd dubbo_project
$ npm install --save-dev @dazejs/dubbo-provider
Copy the code

Register extensions

Add the extension to the daze. Js configuration file located in SRC /config/app.ts

import { DubboServiceProvider } from '@dazejs/dubbo-provider'

export default {
  // ...
  providers: [
      // ...
      DubboServiceProvider  
  ]
};
Copy the code

Add the Dubbo profile

Create dubo.ts directly under the SRC /config/ directory, where you can define the registry address you want to use:

export default {
    default: { // The field name is the registry name
      type: 'zookeeper'.// Registry type, currently only ZooKeeper is supported
      host: '127.0.0.1:2181'.// Zookeeper host address, hostname+port
      port: 20880 // External dubbo service port}};Copy the code

Create consumers

Create exmple-consumer.ts in SRC /app/ (the framework will automatically load the files in this directory)

  import { DubboConsumer, dubbo } from '@dazejs/dubbo-provider';
  
  @dubbo.registry('default') // The registry address provided by dubo.ts above
  @dubbo.interface('com.alibaba.dubbo.demo.DemoProvider'.'1.0.0') // The provider interface address. 1.0.0 is the interface version number
  export class ExampleConsumer extends DubboConsumer {
      // ...
  }
Copy the code

ExampleConsumer must inherit from DubboConsumer to be recognized by the framework as a Dubbo consumer

Invoke Java methods using consumers

Create a controller under SRC /app, and we consume Java data in the controller methods

   import { Controller, http, inject } from '@dazejs/framework';
   import DemoConsumer from '.. /dubbo/consumers/demo';
   
   export class ExampleController extends Controller {
   
     @inject(DemoConsumer) demoConsumer: DemoConsumer; // Inject the consumer instance
   
     @http.get()
     async index() {
       // The consumption method is passed in as an array
       / / call the Java com. Alibaba. Dubbo. Demo. DemoProvider sayHello method of a class
       const res = await this.demoConsumer.invoke('sayHello'['world']);
       returnres; }}Copy the code

The framework converts simple arguments to Java types. Manual conversion using java.string (‘world’) is recommended

Creating a provider

We can also create providers for other parties to call

   import { DubboProvider, dubbo } from '@dazejs/dubbo-provider';
   
   @dubbo.registry('default')
   @dubbo.interface('com.alibaba.dubbo.demo.NodeDemoProvider'.'1.0.0')
   export default class extends DubboProvider {
     @dubbo.method()
     sayHello(name: string) {
       return `Hello ${name}`; }}Copy the code

Java calls, where API and XML definitions are omitted

NodeDemoProvider demoService = (NodeDemoProvider) context.getBean("demoProvider"); // Get the remote service proxy
String hello = demoService.sayHello("world"); // Execute remote method hello = 'hello world'
Copy the code

portal

  • daze.js
  • dubbo-provider