This article gives a brief introduction to Dubbo, what RPC is, using Dubbo2.js in egg.js, and how dubbo interacts with ZK. Github address of dubbo2.js: github.com/apache/dubb…

1. What is dubbo

With the increase of website scale, system development will generally experience single application architecture, vertical application architecture and distributed service architecture. With the increasing number of vertical applications, the interaction between applications is inevitable. Core businesses are extracted as independent services, gradually forming a stable service center, so that front-end applications can respond to changing market demands more quickly. How to deal with the communication between microservices and improve the reuse and integration of services in the distributed service architecture is the key. In this case, a distributed Services framework (RPC) is needed to deal with this problem. Dubbo is one of many excellent open source RPC frameworks.

2. What is RPC(partial understanding)

Remote Procedure Call Remote Procedure Call

HTTP call: Publishes backend resources as URIs through which the front-end accesses resources

RPC calls: Use remote functions or methods as if they were local. Two servers, computer1 and computer2; An application deployed on a Computer1 server wants to call a function or method provided by the application on a Computer2 server. Since it is not in a memory space and cannot be called directly, the RPC framework is needed to solve this problem. This can be done through HTTP, or through the socket itself to implement a set of protocols

Dubbo itself is a Java distributed RPC framework, so when we use Node as a consumer to call Dubbo services, we need a mechanism to support us to make cross-language dubbo RPC calls, Dubbo2.js is to solve this problem.

The implementation principle of Dubbo2. js is not described in this article

3. Dubbo2. Use js

The use of dubbo2.js can be wrapped according to their own requirements, and the following is the use of egg.js. You can also use dubo.js as an egg plug-in.

1. Interface method mapping information

'use strict'; const java = require('js-to-java'); function HelloService(dubbo) { return dubbo.proxyService({ dubboInterface: 'com.linge.service.HelloService', timeout: 10, group: 'hello_app', methods: { sayHello(obj) { return [ { $class: 'com.linge.service.req.HelloReqDTO', $: { name: java.Long(obj.name), age: java.Integer(obj.age), // ...... }, }, ]; ,}}}); }Copy the code

2. Interface configuration file

// app/dubbo_interface/index.js
'use strict';
const { setting } = require('dubbo2.js');
const helloSrv = require('./helloSrv');
module.exports.service = {
  helloSrv,
};
module.exports.dubboSetting = setting
  .match(
    [
      'com.linge.HelloService', 
    ],
    { version: undefined }
  )
Copy the code

3. Dubbo configuration information

Config. dubbo = {application: {name: 'bone'}, register: '192.168.1.12:2181', // dubboVersion: '2.10.0x', root: 'dubbo', environment: 'test',};Copy the code

4. Dubbo initialization

Service, dubboSetting, config information

const { Dubbo } = require('dubbo2.js'); const { service, dubboSetting } = require('./app/dubbo_interface'); class AppBootHook { constructor(app) { this.app = app; } async didReady() { const opt = { dubboSetting, service }; Object.assign(opt, this.app.config.dubbo); this.app.dubbo = new Dubbo(opt); }}Copy the code

4. How do ZooKeeper and Dubbo2.js work together

1. The use of zk

1. MAC installed zk

brew install zookeeper
Copy the code

2. Start the zk

CD/usr/local/Cellar/zookeeper 3.4.13 / bin/zkServer startCopy the code

3. Connect Connect to the ZK address of the test environment

CD /usr/bin zkcli-server 192.168.1.12:2181Copy the code

4. View related commands

Ls/dubbo / / enumerate the current dubbo directory services registered by the / / / com. Linge. Service. HelloService, Com. Linge. Service. HelloService2] ls/dubbo/com. Linge. Service. HelloService / / select one service to view / / [consumers, routers, providers, configurators] ls /dubbo/com.linge.service.HelloService/providers // [dubbo: / / 192.168.1.12:30881 / com. Linge. Service. The HelloService? Anyhost = true&application = linge - main&default. Group = hello&dubbo = 2.8.4 x&environment = test&generic = false&group = hello_app & interface = com. Linge. Service. HelloService&methods = sayHello&organiz Ation = dubbox&owner = programmer, wanghaidong01 & pid = 11561 & retries = 0 & revision = 1.1.3 - SNAPSHOT&side = provider&timeout = 10000 & time stamp=1598720543276]Copy the code

2. Dubbo is saved in ZK

3. Interactive process between Dubbo and ZK

  1. When a microservice is started, it registers its available services with a registry (in the PROVIDERS directory of the ZK)
  2. When the client (consumer) starts, it subscribs to ZK for the service information it needs and writes its information to the Consumers directory of ZK.
  3. Zk returns corresponding service information (service address, interface name, configuration, etc.) to the client (consumer).
  4. The client (consumer) gets the information about the service and initiates an RPC call

5. Common problems in using Dubbo2.js

1.zk get DubboSericeUrls result is empty with service path xxx

The server does not register the XXX service in the ZK. Check whether the XXX service on the server is published successfully and whether the publishing environment is correct

2.Could not find any agent worker with xxx

Similar to HTTP interface 404, whether the service information used by the front end (consumer) configuration is consistent with that provided by the server. Check the environment, group, and interface, and check whether the XXX service on the server is published successfully.

3.Fail to decode request due to

The parameter name or type of the interface parameter is inconsistent. 1. Check whether the interface parameter name and type are correct based on the interface document and actual code. Check whether the interface parameters are correct