9/19/2016 1
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: Internal
MICROSERVICES WITH SENECAJS
Trung Dang
9/19/2016 2
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
Agenda
 What is microservices?
 Factors affect when building up the system with
microservices
 Introducing SenecaJS
 SenecaJS with RabbitMQ
 Integrate with Consul, RabbitMQ
 Talk is cheap – Show me the code ------ DEMO TIME
 Q/A
9/19/2016 3
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: Internal
What is Microservices?
9/19/2016 4
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
Monolithic Application Model
1. How to scale this application to
serve more customer requests?
2. The Payments Module is taking
very long time to process the
requests. Is there any solution
with cheap cost?
9/19/2016 5
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
9/19/2016 6
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
Introducing Microservices Architecture
The Benefits of Microservices
• Faster and simpler deployments and rollbacks
• Elimination of long-term commitment to a single
technology stack
• Improved fault isolation
• Independently scalable services
• Technology diversity
• Ability to write new features as plugins
The Drawbacks of Microservices
• Increased network communication
• Serialization between microservices
• Additional complexity in testing a distributed
system
• Increased complexity in deployment
9/19/2016 7
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
Scaling an application – The Art Of Scalability
9/19/2016 8
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
9/19/2016 9
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
Main factors need to review when
building up microservices system?
 API Gateway OR Direct Client to Microservice Communication
 Stateless / Stateful
 Invoking / Consuming service’s action
 Serialization between microservices
 Service Discovery
 Transaction
 Eventual consistency
 CI / CD, configuration deployment
 Service Health check
9/19/2016 10
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: Internal
Introducing SenecaJS
9/19/2016 11
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
9/19/2016 12
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
Who’s using it
9/19/2016 13
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
SenecaJS Philosophy
• Pattern Matching: instead of fragile service discovery, you just let
the world know what sort of messages you are about.
• Transport Independence: you can send messages between
services in many ways, all hidden from your business logic.
9/19/2016 14
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
SenecaJS – First microservice
var seneca = require('seneca')()
seneca.add('role:math,cmd:sum', (msg, reply) => {
reply(null, { answer: (msg.left + msg.right) })
})
seneca.act({ role: 'math', cmd: 'sum', left: 1, right: 2 },
(err, result) => {
if (err) return console.error(err)
console.log(result)
}
)
9/19/2016 15
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
SenecaJS with AMQP Transport
• seneca-amqp-transport
https://github.com/seneca-contrib/seneca-amqp-transport
• It is a plugin to allow seneca listeners and clients to communicate
over AMQP
• Currently support AMQP 0.9.1
• For AMQP 1.0, please use seneca-servicebus-transport
• It use customized RPC (Remote Procedure Call) to implement the
transportation.
9/19/2016 16
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
SenecaJS with AMQP Transport – Show me the code
var seneca = require('seneca')()
.use('seneca-amqp-transport')
.add('role:create,foo:1', function (args, done) {
console.log(“Server: with i = " + args.zed);
done(null, { zed: args.zed, bar: args.zed + 1}) })
.listen({
type: 'amqp', pin: ‘module:test',
url: 'amqp://trungdt:123absoft.vn@pm.absoft.vn:5672’
});
var client = seneca
.client({
type: 'amqp', pin: ‘module:test’
url: 'amqp://trungdt:123absoft.vn@pm.absoft.vn:5672’
});
for (var i = 0; i < 10; i++) {
client.act('role:create,foo:1,zed:' + i , function (err, ret) {
console.log(‘Client: with i = ‘ + ret.zed);
});
}
9/19/2016 17
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
SenecaJS with AMQP Transport – How it works
Routing by topic: module:test Consumer Queue
Individual Queue
created by each Client
9/19/2016 18
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
Microservices using SenecaJS, RabbitMQ, Consul,
SequelizeJS and FeathersJS
servicebase
Database Models
Service Logic
Service B
NGINX–LoadBalancer
GatewayAPI1GatewayAPI2
RabbitMQ
Configuration Storage & Health Checker
Postgresql
Docker
servicebase
Database Models
Service Logic
Service A
9/19/2016 19
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: InternalSecurity Classification: Internal
Introducing package servicebase
https://bitbucket.org/trungdang_apium/microservices_servicebase
Features :
• Listener / Client mode, with AMQP Transport or HTTP Transport
• Promisify all functions
• Use Consul as Configuration Storage & Service Health Checher
• Support multiple database adapters (Sequelize). Postgresql & Sqlite are build-in support
• Loggly logs monitoring
• Support Authorization when consuming the service’s action
• Error handler: no more terminating your service because of TIMEOUT or fatal$ error
• Including unit-test helper
• Including typed-definitions file
9/19/2016 20
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: Internal
Show Me Your Code
9/19/2016 21
CLICK TO EDIT MASTER TITLE STYLE
Security Classification: Internal
Q&A
Thank you very much

Microservices With SenecaJS

  • 1.
    9/19/2016 1 CLICK TOEDIT MASTER TITLE STYLE Security Classification: Internal MICROSERVICES WITH SENECAJS Trung Dang
  • 2.
    9/19/2016 2 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal Agenda  What is microservices?  Factors affect when building up the system with microservices  Introducing SenecaJS  SenecaJS with RabbitMQ  Integrate with Consul, RabbitMQ  Talk is cheap – Show me the code ------ DEMO TIME  Q/A
  • 3.
    9/19/2016 3 CLICK TOEDIT MASTER TITLE STYLE Security Classification: Internal What is Microservices?
  • 4.
    9/19/2016 4 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal Monolithic Application Model 1. How to scale this application to serve more customer requests? 2. The Payments Module is taking very long time to process the requests. Is there any solution with cheap cost?
  • 5.
    9/19/2016 5 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal
  • 6.
    9/19/2016 6 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal Introducing Microservices Architecture The Benefits of Microservices • Faster and simpler deployments and rollbacks • Elimination of long-term commitment to a single technology stack • Improved fault isolation • Independently scalable services • Technology diversity • Ability to write new features as plugins The Drawbacks of Microservices • Increased network communication • Serialization between microservices • Additional complexity in testing a distributed system • Increased complexity in deployment
  • 7.
    9/19/2016 7 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal Scaling an application – The Art Of Scalability
  • 8.
    9/19/2016 8 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal
  • 9.
    9/19/2016 9 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal Main factors need to review when building up microservices system?  API Gateway OR Direct Client to Microservice Communication  Stateless / Stateful  Invoking / Consuming service’s action  Serialization between microservices  Service Discovery  Transaction  Eventual consistency  CI / CD, configuration deployment  Service Health check
  • 10.
    9/19/2016 10 CLICK TOEDIT MASTER TITLE STYLE Security Classification: Internal Introducing SenecaJS
  • 11.
    9/19/2016 11 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal
  • 12.
    9/19/2016 12 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal Who’s using it
  • 13.
    9/19/2016 13 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal SenecaJS Philosophy • Pattern Matching: instead of fragile service discovery, you just let the world know what sort of messages you are about. • Transport Independence: you can send messages between services in many ways, all hidden from your business logic.
  • 14.
    9/19/2016 14 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal SenecaJS – First microservice var seneca = require('seneca')() seneca.add('role:math,cmd:sum', (msg, reply) => { reply(null, { answer: (msg.left + msg.right) }) }) seneca.act({ role: 'math', cmd: 'sum', left: 1, right: 2 }, (err, result) => { if (err) return console.error(err) console.log(result) } )
  • 15.
    9/19/2016 15 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal SenecaJS with AMQP Transport • seneca-amqp-transport https://github.com/seneca-contrib/seneca-amqp-transport • It is a plugin to allow seneca listeners and clients to communicate over AMQP • Currently support AMQP 0.9.1 • For AMQP 1.0, please use seneca-servicebus-transport • It use customized RPC (Remote Procedure Call) to implement the transportation.
  • 16.
    9/19/2016 16 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal SenecaJS with AMQP Transport – Show me the code var seneca = require('seneca')() .use('seneca-amqp-transport') .add('role:create,foo:1', function (args, done) { console.log(“Server: with i = " + args.zed); done(null, { zed: args.zed, bar: args.zed + 1}) }) .listen({ type: 'amqp', pin: ‘module:test', url: 'amqp://trungdt:123absoft.vn@pm.absoft.vn:5672’ }); var client = seneca .client({ type: 'amqp', pin: ‘module:test’ url: 'amqp://trungdt:123absoft.vn@pm.absoft.vn:5672’ }); for (var i = 0; i < 10; i++) { client.act('role:create,foo:1,zed:' + i , function (err, ret) { console.log(‘Client: with i = ‘ + ret.zed); }); }
  • 17.
    9/19/2016 17 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal SenecaJS with AMQP Transport – How it works Routing by topic: module:test Consumer Queue Individual Queue created by each Client
  • 18.
    9/19/2016 18 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal Microservices using SenecaJS, RabbitMQ, Consul, SequelizeJS and FeathersJS servicebase Database Models Service Logic Service B NGINX–LoadBalancer GatewayAPI1GatewayAPI2 RabbitMQ Configuration Storage & Health Checker Postgresql Docker servicebase Database Models Service Logic Service A
  • 19.
    9/19/2016 19 CLICK TOEDIT MASTER TITLE STYLE Security Classification: InternalSecurity Classification: Internal Introducing package servicebase https://bitbucket.org/trungdang_apium/microservices_servicebase Features : • Listener / Client mode, with AMQP Transport or HTTP Transport • Promisify all functions • Use Consul as Configuration Storage & Service Health Checher • Support multiple database adapters (Sequelize). Postgresql & Sqlite are build-in support • Loggly logs monitoring • Support Authorization when consuming the service’s action • Error handler: no more terminating your service because of TIMEOUT or fatal$ error • Including unit-test helper • Including typed-definitions file
  • 20.
    9/19/2016 20 CLICK TOEDIT MASTER TITLE STYLE Security Classification: Internal Show Me Your Code
  • 21.
    9/19/2016 21 CLICK TOEDIT MASTER TITLE STYLE Security Classification: Internal Q&A Thank you very much