1.2.7 • Published 1 year ago

@lakutata-module/service v1.2.7

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

NPM Version NODE Version Known Vulnerabilities NPM Download

Features

  • Micro-service
  • Distributed Events
  • Cross nodes service invoking (directly|chain|parallel)
  • Scalable

Quickstart

Service Examples

Service Registry Example

const serviceRegistryApp = await createApp({
    id: 'registry.service.test.app',
    name: 'test-service-registry',
    modules: {
        registry: {
            class: ServiceRegistry,
            logger: true,
            redisOptions: {
                host: '192.168.0.132'
            },
            token: '123456'
        }
    }
})
serviceRegistryApp.Logger.info(serviceRegistryApp.getID(), 'is running')

Service Provider Example

 const serviceProviderApp = await createApp({
    id: 'provider.service.test.app',
    name: 'test-service-provider',
    modules: {
        provider: {
            class: Service,
            registry: '127.0.0.1',
            token: '123456',
            controllers: [
                `${path.resolve(__dirname, '../controllers')}/**/*`
            ],
            logger: true
        }
    }
})
serviceProviderApp.Logger.info(serviceProviderApp.getID(), 'is running')

Service Consumer Example

const serviceConsumerApp = await createApp({
    id: 'consumer.service.test.app',
    name: 'test-service-consumer',
    modules: {
        consumer: {
            class: Service,
            registry: '127.0.0.1',
            token: '123456',
            services: [
                `${path.resolve(__dirname, '../services')}/**/*`
            ],
            logger: true
        }
    }
})
serviceConsumerApp.Logger.info(serviceConsumerApp.getID(), 'is running')
setTimeout(async () => {
    serviceConsumerApp.Logger.info('Service consumer test begin:')
    const consumer = serviceConsumerApp.Modules.get<Service>('consumer')
    consumer.channel('test').on('test', (x, y, z) => {
        serviceConsumerApp.Logger.debug('Received channel event [test] data:', x, y, z)
    })
    consumer.channel('test').triggerOne('test', 'a', 'b', 'c')
    try {
        serviceConsumerApp.Logger.info('Start test manually service invoking:')
        serviceConsumerApp.Logger.debug('Service chain invoke result:', await consumer.invoke({
                serviceId: 'provider.service.test.app',
                data: {test: true},
                transform: (response) => {
                    return {name: response}
                }
            },
            {
                serviceId: 'provider.service.test.app',
                data: {
                    test: true,
                    ccc: true
                },
                transform: (response) => {
                    return `this is response:${response}`
                }
            }))
        serviceConsumerApp.Logger.debug('Service parallel invoke result:', await consumer.parallel({
                serviceId: 'provider.service.test.app',
                data: {test: true},
                transform: (response) => {
                    return `this is response:${response}`
                }
            }, [
                {
                    serviceId: 'provider.service.test.app',
                    data: {test: true},
                    transform: (response) => {
                        return {test: true}
                    }
                },
                {
                    serviceId: 'provider.service.test.app',
                    data: {
                        test: true,
                        name: 'manually service invoking',
                        ccc: true
                    },
                    transform: (response) => {
                        return `this is response:${response}`
                    }
                }
            ]
        ))
    } catch (e) {
        serviceConsumerApp.Logger.error(e)
    }
}, 3000)

Service Controller Examples (For Service Provider)

TestController1

@Controller()
export class TestController1 extends BaseController {
    @Accept(Validator.Object({
        name: Validator.String.required()
    }))
    @PATTERN({test: true, ccc: true})
    public async serviceTest(a: any) {
        return a.name
    }

    @PATTERN({test: true, abd: true})
    public async serviceTest2() {
        return [true, true]
    }
}

TestController2

@Controller()
export class TestController2 extends BaseController {
    @PATTERN({test: true})
    public async tc2Test() {
        return 'this is tc2 test'
    }
}

@lakutata/core required.

Documentations

Configure Service Module (Registry|Provider|Consumer)

Service Registry Options

NameDataTypeDefaultRequiredDescription
portNumber6911NoService registry listen port
tokenStringNoService registry access toke for clients
loggerBoolean/LoggerfalseNoLog service registry activities
redisOptionsRedisOptionsNoIf not set, the service registry is running in single node mode, else it will build a cluster automatically

Service Options

NameDataTypeDefaultRequiredDescription
registryStringYesService registry address url,if port not specified, it will be 6911
controllersString\String[]NoThe service controllers load path/paths
timeoutNumber60000(ms)NoService consumer invoke remote service timeout
retryNumber0NoIf service consumer invoke remote service return a retryable error, it will retry to invoke again until the retry chances decreases to 0
tokenStringNoService registry access token
loggerBoolean\LoggerfalseNoLog service consumer activities

Service Controller

A valid service controller MUST extend BaseController, and it MUST decorate by @Controller(), the @Controller() decorator has an optional parameter:

@Controller(basicPattern: TPattern = {})

if basicPattern set, all patterns declared in the controller will contain it.

In the service controller, @PATTERN() decorator is for decorate method, the parameter is the invoking pattern.

@PATTERN(pattern: TPattern)

Classes

  • ServiceRegistry (Module)

    • fetchServices(appId?: string | RegExp): { appId: string: IServiceInfo[] }
  • Service (Module)

    • async invoke(...params: TInvokeObject[]): Promise
    • async parallel(...params: (TInvokeObject | TInvokeObject[])[]): Promise<any[]>
    • channel(name: string): Channel
      • trigger(event: string, ...args: any[]): boolean
      • triggerAll(event: string, ...args: any[]): boolean
      • triggerOne(event: string, ...args: any[]): boolean
      • directTrigger(serviceId: string, event: string, ...args: any[]): boolean
      • directTriggerAll(serviceId: string, event: string, ...args: any[]): boolean
      • directTriggerOne(serviceId: string, event: string, ...args: any[]): boolean
      • privateTrigger(event: string, ...args: any[]): boolean
      • privateTriggerAll(event: string, ...args: any[]): boolean
      • privateTriggerOne(event: string, ...args: any[]): boolean
      • on(event: string, listener: (...args: any[]) => void): Channel
      • once(event: string, listener: (...args: any[]) => void): Channel
      • off(event?: string, listener?: (...args: any[]) => void): Channel
    • channels(): string[]
    • async fetchServices(appId?: string | RegExp): Promise<{ appId: string: IServiceInfo[] }>
  • BaseController

Decorators

  • @Controller
  • @PATTERN

Exceptions

  • BrokenServiceInvokeException
  • IncorrectTokenException
  • InvalidRegistryProtocolException
  • PatternNotFoundException
  • RequestException
  • RequestTimeoutException
  • ServiceClientInitializationException
  • ServiceInvokeTimeoutException
  • ServiceProviderNotFoundException
  • ServiceRegistryInitializationException

Examples

Examples can be found in project src/tests.

How to Contribute

Please let us know how can we help. Do check out issues for bug reports or suggestions first.

License

MIT

1.2.0

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.1.38

1 year ago

1.1.39

1 year ago

1.1.29

1 year ago

1.1.28

1 year ago

1.1.30

1 year ago

1.1.33

1 year ago

1.1.32

1 year ago

1.1.31

1 year ago

1.1.37

1 year ago

1.1.36

1 year ago

1.1.35

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

1 year ago

1.1.20

1 year ago

1.1.27

1 year ago

1.1.26

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.17

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.12

1 year ago

1.0.44

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.40

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.33

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.23

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.2

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago