Share
Embed
Transcript
31 slides
-
TODAY > not today > philosophy > main features &
foundation > demo > real showcase > Q & A -
WHAT ARE NOT TODAY > Definition of Microservices > Monolithic
vs. Microservices -
SENECA.JS > microservices toolkit for Node.js > senecajs.org > https://github.com/senecajs/seneca
> First commit 21 Oct 2010 > Stable version 2.1 -
WHY USE SENECA > focus on business logic first >
easy to scale later > clear separation between infrastructure and app code -
SENECA PHILOSOPHY > Pattern matching: instead of fragile service discovery,
you just let the world know what sort of message you case about. > Transport independence: you can send messages between services in many ways, all hidden from your business logic. -
FIRST MICROSERVICE // create new seneca instance const seneca =
require('seneca')() // add new pattern seneca.add({role: 'math', cmd: 'sum'}, (msg, done) => { done(null, { answer: msg.left + msg.right }) }) // call action and receive the response seneca.act('role:math,cmd:sum', {left: 1, right: 4}, (err, result) => { if (err) return console.error(err) console.log(result) }) -
ADD A NEW SERVICE seneca.add({role: 'math', cmd: 'product'}, (msg, done)
=> { done(null, { answer: msg.left * msg.right }) }) -
ALL TOGETHER const seneca = require('seneca')() seneca.add({role: 'math', cmd: 'sum'},
(msg, done) => { done(null, { answer: msg.left + msg.right}) }) seneca.add({role: 'math', cmd: 'product'}, (msg, done) => { done(null, { answer: msg.left * msg.right }) }) seneca.act('role:math,cmd:sum', {left: 2, right: 4}, (err, result) => { if (err) return console.error(err) console.log(result) }) seneca.act('role:math,cmd:product', {left: 2, right: 4}, (err, result) => { if (err) return console.error(err) console.log(result) }) -
EXTEND FUNCTIONALITY seneca.add({role: 'math', cmd: 'sum', integer: true}, (msg, done)
=> { done(null, { answer: Math.floor(msg.left) + Math.floor(msg.right) }) }) -
CODE RE-USE seneca.add({role: 'math', cmd: 'sum', integer: true}, (msg, done)
=> { this.act({ role: 'math', cmd: 'sum', left: Math.floor(msg.left), right: Math.floor(msg.right) }, done) }) -
PATTERN QUICK RECAP > matches against top level properties >
pattern are unique > specialised patterns win over generic pattern > competing patterns win based on value -
PLUGINS OF PATTERN > we can group several patterns into
a single plugin using a namespacing convention > a plugin can be named > it's possible to pass a set of initial options to a plugin > usually a plugin is in a single Node module -
PLUGINS OF PATTERN function math (options) { this.add({role: 'math', cmd:
'sum'}, (msg, done) => { done(null, { answer: msg.left + msg.right}) }) this.add({role: 'math', cmd: 'product'}, (msg, done) => { done(null, { answer: msg.left * msg.right }) }) } require('seneca')() .use(math) .act('role:math,cmd:sum,left:2,right:4', console.log) -
SENECA TCP TRANSPORT require('seneca')() .client({type: 'tcp', port: 80}) .act('role:math,cmd:sum,left:2,right:4', console.log)
require('seneca')() .use(math) .listen({type: 'tcp', port: 80}) -
SENECA TRANSPORT AVAILABLE > seneca-rabbitmq > seneca-kafka > seneca-redis >
seneca-beanstalk > seneca-ampq > seneca-mesh -
STORAGE > seneca provides a simple data abstraction layer, with
four main operations: save, load, list, remove. > data as json > in-memory storage by default > a list of plugins for the most common databases -
STORAGE PATTERNS > load: role:entity,cmd:load,name: <entity> > list: role:entity,cmd:list,name: <entity>
> save: role:entity,cmd:save,name: <entity> > remove: role:entity,cmd:remove,name: <entity> -
ACTIVE RECORD STYLE USING seneca.make METHOD WE CAN THEN const
seneca = require('seneca')() const product = seneca.make() product.name = 'Apple' product.price = 1.99 // sends role:entity,cmd:save,name:product product.save$(console.log) -
STORAGE EXAMPLE const Promise = require('bluebird') seneca.add('role:cart,cmd:add', function (args, done)
{ const entity = this.make('cart_item') const save$ = Promise.promisify(entity.save$, {context: entity}) const user = args.user const item = args.item save$({user, item}) .then(model => done(null, {ok: true, model: _.omit(model, 'entity$')})) .catch(err => done(null, {ok: false, why: err.message})) }) seneca.add('role:cart,cmd:view', function (args, done) { const entity = this.make('cart_item') const list$ = Promise.promisify(entity.list$, {context: entity}) list$({user: args.user}) .then(items => done(null, {ok: true, items})) .catch(err => done(null, {ok: false, why: err.message})) }) -
STORAGE PLUGINS LIST > PostgreSQL > MySQL > DynamoDB >
Redis > Mongodb > Cassandra > SQLite > Hana > CouchDB > LevelDB -
SENECA-MESH - NO MORE SERVICE DISCOVERY! npm install seneca-balance-client npm
install seneca-mesh // color-client.js var seneca = require('seneca')() // load the mesh plugin seneca.use('mesh') // send a message out into the network // the network will know where to send format:hex messages .act({format: 'hex', color: 'red'}, function (err, out) { // prints #FF0000 console.log(out.color) // disconnect from the network this.close() }) $ node color-service.js -
1 1 Fuge is an execution environment for micro service
development with Node.js Fuge.io. -
LEARN MORE ABOUT SENECA > Nodezoo > Nodezoo workshop >
Twitter clone -
THANK YOU AND SEE YOU NEXT IN > seneca-mesh >
Microservices vs. Monolithic