Coder Social home page Coder Social logo

feeler.js's Introduction

feeler.js

MicroServices framework

Install

npm install git+https://[email protected]/k33g/feeler.js.git --save

You need Redis as a discovery backend, but you can write your own (see DiscoveryBackend.js)

Sample

const Haikunator = require('haikunator')
const Feeler = require('feeler')
const Service = Feeler.Service
const Client = Feeler.Client
const DiscoveryBackend = Feeler.DiscoveryBackend

let port = process.env.PORT || 9090;
let externalPort = process.env.EXTERNAL_PORT || port;

let haikunator = new Haikunator()

let discoveryBackend = new DiscoveryBackend({
  redisOptions: {
    port: 6379,
    host: '127.0.0.1'
  },
  keyServices: "my-micro-services"
}) 

let record = {
  name: process.env.SERVICE_NAME || haikunator.haikunate(),
  domain: `http://localhost:${externalPort}`,
  root:"/api",
  methods: [{name: "hi", type: "GET", path: "/hi"},{name: "hello", type: "GET", path: "/hello"}],
  metadata: {
    kind: "http"
  } 
}

// discover existing services
discoveryBackend.getServices(service => service.metadata.kind == "http", results => {
  results.when({
    DiscoveryResult: services => {
      // calling each method of each service
      services.forEach(service => {
        let client = new Client({service: service})

        client.healthCheck().then(res => {
          console.log("๐ŸŒ service informations:", service)
          console.log("calling methods...")
          client.callMethod({name:"hi"}).then(res => console.log(res))
          client.callMethod({name:"hello"}).then(res => console.log(res))
        })
      })
    },
    DiscoveryFailure: failure => {
      console.log("๐Ÿ˜ก failure when disocvering other services")
      console.log(failure.message, failure.error.message)      
      process.exit()
    },
    _: () => console.log("๐Ÿค”")

  })
})

let service = new Service({
  discoveryBackend: discoveryBackend, record: record
})

// stop and exit
service.stop = (cause) => {
  console.log(`๐Ÿ‘‹ ${service.record.registration} ${cause}`)
}

service.get("/api/hi", (req, res) => {
  res.send({message: "hi ๐ŸŒ"});
})

service.get("/api/hello", (req, res) => {
  res.send({message: "hello ๐ŸŒ"});
})

// publish the service, then start the service
service.createRegistration(resultOfRegistration => {
  resultOfRegistration.when({
    // the service is registered
    ServiceResult: registrationResult => {
      console.log(registrationResult.record.registration, registrationResult.message)
      
      service.start(port, startResult => {
        startResult.when({
          // the service is up, then we start the service
          ServiceResult: updateResult => {
            console.log(updateResult.record.registration, updateResult.message)
            console.log(`๐ŸŒ Service ${updateResult.record.name} is ${updateResult.record.status} - listening on ${port}`)
          },
          ServiceFailure: failure => {
            console.log(`๐Ÿ˜ก Houston? we have a problem!`)
            process.exit()
          }
        })
      })
    },
    ServiceFailure: failure => { 
      console.log(failure.message, failure.error.message)
      process.exit()
    }
  })
})

Remarks

  • each service has a /discover route to fetch all services connected to the same discovery backend
  • each service has a /healthcheck route to check if the service is up

TODO

  • documentation
  • circuitbreaker
  • use pub/sub of Redis

feeler.js's People

Contributors

k33g avatar

Stargazers

Zero avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.