microsia

    0.0.7 • Public • Published

    Build Status Codacy Badge Coverage Status Dependency Status

    UNDER CONSTRUCTION

    This library is under construction, so use at your own risk

    Why microsia?

    Microsia is an lightweight microservices server with simple, familiar syntax that was inspired by koa and express, allow you to create transport layer for microservices as quick as possible.

    Currently, microsia is building around nats as central messaging system. Microsia also has local pubsub system, that allow services on same server communicate with each other fastest without remote messaging system (nats).

    Concept:

    Concept

    Usages:

    Install package

    npm install --save microsia
    # OR
    yarn add microsia
    

    Sample service services/bar.js:

    const broker = require('microsia')
    const service = broker().createService({ name: 'bar' })
     
    service.use(function (ctx, next) {
      console.log('bar was called')
      next()
    })
     
    service.subscribe('bar', function(ctx) {
      ctx.res.send({
        msg: `SERVICE bar: Hi, This is bar!`,
      })
    })
     
    // Call to other service using current context
    // Metadata (requestId,...) will be past to other service, too
    service.subscribe('call-to-other', async function(ctx) {
      const res = await ctx.call('baz.status')
      ctx.res.send({
        msg: `SERVICE bar: Status of baz`,
        data: res.body,
      })
    })
     
    // Call directly from service will create new context
    service.call('foo.foo', {})
        .then(data => console.log(data))

    Run group services with service runner:

    const loadServices = require('microsia/runner')
     
    // Service path
    const services = [
      'services/foo.js',
      'services/bar.js',
    ]
    const options = {
      transporter: {
        name: 'nats',
        options: {
          servers: ['nats://demo.nats.io:4222'],
          timeout: 3000,
          pingInterval: 120000,
          reconnect: true,
          reconnectTimeWait: 2000,
          maxReconnectAttempts: 10,
          maxRequestRetryAttempts: 3,
        },
      },
    }
     
    loadServices(options, services)

    You also can run only one service directly by node, just pass config to broker() and then run node services/bar.js:

    const broker = require('microsia/broker')
    const service = broker({
      transporter: {
        name: 'nats',
        options: {
          servers: ['nats://demo.nats.io:4222'],
          timeout: 3000,
          pingInterval: 120000,
          reconnect: true,
          reconnectTimeWait: 2000,
          maxReconnectAttempts: 10,
          maxRequestRetryAttempts: 3,
        },
      },
    }).createService({ name: 'bar' })
     
    ...

    Please see example code in example folder for more information.

    Run example code:

    Internal Services using runner:

    cd example
    node index.js
    

    ApiGateway using broker directly and using express as http server:

    cd example/gateway
    yarn
    node index.js
    

    Make request to ApiGateway

    curl -i -H "Accept: application/json" "http://localhost:3000/api/bar" 
    

    TODO:

    • Api Gateway
    • Group route
    • Middleware with route
    • Streaming file
    • Circuit Breaker (inside broker)
    • Logger
    • Nats authrorize
    • Serialize (protobuf, ...)
    • Polish code
    • Unit test & code coverage
    • Benchmark
    • Optimize
    • Improve documentation & example
    • Kafka transporter
    • Multi transporter in one broker (consider later)

    Install

    npm i microsia

    DownloadsWeekly Downloads

    0

    Version

    0.0.7

    License

    ISC

    Unpacked Size

    190 kB

    Total Files

    28

    Last publish

    Collaborators

    • bahung1221