Skip to content

gperreymond/openfaas-template-node-moleculer

Repository files navigation

Moleculer logo

Serverless Functions, Made Simple

OpenFaaS® makes it simple to deploy both functions and existing code to Kubernetes.

CircleCI Coverage Status Docker

Features

  • moleculer: 0.14.0-beta6
  • moleculer-web: 0.8.5

Environment vars

  • APP_RABBITMQ_URI: amqp://username:password@localhost:5672 (default)
  • APP_RABBITMQ_PREFETCH: 1 (default)
  • APP_MOLECULER_LOGGER: true (default)
  • APP_MOLECULER_METRICS: true (default)

I choose rabbitmq as broker, over nats ; Because of prefetch, the webui admin, the replay, the deadletter, etc.
Usually I take nats but not this time!

Architecture

Global schema

Mandatory knowledge

  • First you need to know how works openfass
  • Second you need to know how work moleculer and moculer-web

Configuration of moleculer with services:

// Load all domains as services
await broker.loadServices()

Configuration of moculer-web service:

// Load API Gateway
broker.createService({
  mixins: [ApiService],
  settings: {
    path: '/',
    routes: [{
      mappingPolicy: 'restrict',
      mergeParams: true,
      aliases
    }]
  }
})

Dummy service as faas

  • Create a dummy service with two actions
  • Open two routes from moleculer gateway, who match the two actions
  • Create the dockerfile
  • Create the openfaas deployment

Create a dummy service with two actions

/**
filename: dummy.service.js
**/
module.exports = {
  name: 'Dummy',
  actions: {
    HelloWorldCommand: require('./actions/HelloWorldCommand'),
    NotHelloWorldQuery: require('./actions/NotHelloWorldQuery')
  }
}

Open two routes from moleculer gateway, who match the two actions

Property metadata.aliases is from moleculer-web, it will expose the actions you want.

/**
filename: dummy.service.js
**/
module.exports = {
  name: 'Dummy',
  metadata: {
    aliases: {
      'POST dummy/hello-world': [
        'Dummy.HelloWorldCommand'
      ],
      'GET dummy/not-hello-world': 'Dummy.NotHelloWorldQuery'
    }
  },
  actions: {
    HelloWorldCommand: require('./actions/HelloWorldCommand'),
    NotHelloWorldQuery: require('./actions/NotHelloWorldQuery')
  }
}

Create the dockerfile

A very simple thing to do, because moleculer is configure to load all services in services directory, just do this:

# Dockerfile
FROM gperreymond/openfaas-node-moleculer
COPY . services

Create the openfaas deployment

# stack.yaml
version: 1.0

provider:
  name: openfaas
  gateway: [url of the openfass admin]

functions:
  dummy-service:
    lang: dockerfile
    image: [docker hub image name]
    handler: [path of you service in the repository]
    # some env to overwrite
    environment:
      APP_RABBITMQ_URI: amqp://admin:password@rabbitmq.docker.localhost:5672
      APP_RABBITMQ_PREFETCH: 2
    # mandatory for blue/green
    annotations:
      com.openfaas.health.http.path: "/hc"
      com.openfaas.health.http.initialDelay: "30s"

Final directory architecture

[dummy]
   [actions]
      HelloWorldCommand.js
      NotHelloWorldQuery.js
   .dockerignore
   Dockerfile
   dummy.service.js

stack.yaml

About

OpenFaas template based on nodejs, moleculer & moleculer-web

Resources

Stars

Watchers

Forks

Packages

No packages published