Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RPC-like client-service implementation over messaging queue

License

NotificationsYou must be signed in to change notification settings

imqueue/rpc

Repository files navigation

License

RPC-like client-service implementation over messaging queue. This moduleprovides base set of abstract classes and decorators to build services andclients for them.

Why?

To provide fast and reliable way of communication between backend services.

IMQ-RPC provides a simple and reliable solution, using which developer can focusexactly on business logic implementation and be assured the servicesinter-communication is handled properly, performs fast and is scalable enoughto handle any load.

Installation

npm i --save @imqueue/rpc

Usage

For next examples it is expected redis server is running onlocalhost:6379.

1. Building Service

When building service doc-blocks for exposed service methods are mandatory.First of all it guarantees good level of documentation. From other handit provides better types information for building service clients and complextypes usages.

Fileservice.ts:

import{IMQService,expose}from'@imqueue/rpc';classHelloextendsIMQService{/**     * Says hello using given name     *     *@param {string} [name] - name to use withing hello message     *@returns {string} - hello string     */    @expose()publichello(name?:string):string{return`Hello,${name}!`;}}(async()=>{constservice=newHello();awaitservice.start();})();

2. Building Client

There are 3 ways of building service clients:

  1. Writing/updating clients manually.In this case you will be fully responsible for maintaining clientscode but will have an ability to extend client code as you wish.
  2. Generating/updating clients automatically using IMQClient.create()at runtime.This will give an ability do not care about the need to keep clientcode up-to-date with the service changes. Each time client started itwill re-generate its interface and will reflect all changes made onservice side. BTW, this method has disadvantages in code developmentand maintenance (especially from TypeScript usage perspective) whichare directly related to dynamic module creation, compilation and loading.There will be problems using service complex types interfaces inTypeScript. From perspective of JavaScript usage it is OK.
  3. Generating/updating pre-compiled clients automatically usingIMQClient.create()This will require additional actions on client side to update its codebaseeach time the service changed its interfaces. BTW it gives an advantageof full support of all typing features on TypeScript side and providesautomated way to manage clients up-to-date state.

File:client.ts (manually written client example):

import{IMQClient,IMQDelay,remote}from'@imqueue/rpc';classHelloClientextendsIMQClient{/**     * Says hello using given name     *     *@param {string} name     *@returns {Promise<string>}     */    @remote()publicasynchello(name?:string,delay?:IMQDelay):Promise<string>{returnawaitthis.remoteCall<string>(...arguments);}}(async()=>{try{constclient=newHelloClient();awaitclient.start();// client is now ready for useconsole.log(awaitclient.hello('IMQ'));}catch(err){console.error(err);}})();

Using dynamically built clients (for the same service described above):

import{IMQClient}from'@imqueue/rpc';(async()=>{try{consthello:any=awaitIMQClient.create('Hello');constclient=newhello.HelloClient();awaitclient.start();console.log(awaitclient.hello('IMQ'));awaitclient.destroy();}catch(err){console.error(err);}})();

In this case above,IMQClient.create() will automatically generate clientcode, compiles it to JS, loads and returns compiled module. As far as ithappens at runtime there is no possibility to refer type informationproperly, but there is no need to take care if the client up-to-date withthe service code base. Each time client created it will be re-generated.

BTW,IMQClient.create() supports a source code generation without a moduleloading as well:

import{IMQClient}from'@imqueue/rpc';(async()=>{awaitIMQClient.create('Hello',{path:'./clients',compile:false});})();

In this case client code will be generated and written to a correspondingfile./clients/Hello.ts under specified path. Then it can be compiled andimported within your project build process, and referred in your codeas expected:

import{hello}from'./clients/Hello';(async()=>{constclient=newhello.HelloClient();awaitclient.start();console.log(client.hello('IMQ'));})();

In this case all complex types defined within service implementationwill be available under imported namespace of the client.

Notes

For image containers builds assign machine UUID in/etc/machine-id and/var/lib/dbus/machine-id respectively. UUID should be assigned once ona first build then re-used each new build to make it work consistently.

License

This project is licensed under the GNU General Public License v3.0.See theLICENSE

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp