Blog | Phodal - A Growth Engineerhttp://www.phodal.com/blog/2014-05-28T14:14:06.688448+00:00BlogSeneca——微服务框架实战22014-05-28T13:41:15+00:002014-05-28T14:14:06.688448+00:00Phodal Huanghttp://www.phodal.com/blog/author/root/http://www.phodal.com/blog/seneca-action-microservices-framework/当我在告别毕业答辩之后,我开始了继续学习[微服务构架](http://www.phodal.com/blog/microservices/)的道路,而这时打开Seneca的官网,发现上一篇关于微服务框架的文章——[微服务框架——Seneca,一个Node.js的微服务工具包
](http://www.phodal.com/blog/microservices-framework-seneca-a-microservices-toolkit/)。显然,已经不合我的胃口,官网首页的更适合我,于是这次比上次好多了。
##微服务框架 Seneca
**Built it Now, Scale it Later!**
> This toolkit lets you write clean code that you can scale without needing to refactor. Start with everything in one process, and split it all out onto multiple systems when you need to.
口号已经变成了:现在构架,未来缩放
这工具可以让你写出清晰的代码,你可以在不需要重构的情况下扩展。开始每件事在一个进程里,分割这一切到系统中当你需要的时候。
##Seneca示例
1.这里继续用官方的示例
require('seneca')()
.add(
{ generate:'id'},
function( message, done ) {
done( null,
{id:''+Math.random()} )
})
.listen()
这是一个微服务生成的标识符,你可以发送微服务的JSON对象,他会响应一个新的JSON对象。
2.让我们把它保存为``example.js``然后
npm install seneca
3.接着运行
node example.js
下面是一个运行结果
node example.js
2014-05-28T13:33:06.851Z INFO hello Seneca/0.5.17/chcg20
2014-05-28T13:33:07.265Z INFO plugin transport - 9u1hc3 listen localhost 10101 /act Seneca/0.5.17/chcg20
4.于是我们可以通过**curl** POST数据的方式来获取返回结果:
curl --data '{"generate":"id"}' http://localhost:10101/act
返回了
{"id":"0.30422571394592524"}
返回的是上面声明的随机数。
##Seneca实战
上面的想法让我认为我们可以通过修改,传进来的参数限制其传入,上面的
{"generate":"id"}
是一个固定的,学官网添加一个**nid**,不同的是只是做为验证的参考。
require('seneca')()
.add(
{ generate:'id',type:"nid"},
function( message, done ) {
done( null,
{id:''+"12"} )
})
.listen()
于是我们的curl就要变成
curl -d '{"generate":"id","type":"nid"}' http://localhost:10101/act
返回一个固定的结果
{"id":"12"}
微服务框架——Seneca,一个Node.js的微服务工具包2014-05-27T02:28:58+00:002014-05-27T03:06:49.474760+00:00Phodal Huanghttp://www.phodal.com/blog/author/root/http://www.phodal.com/blog/microservices-framework-seneca-a-microservices-toolkit/了解完微服务的一些知识之后,便开始寻找一些框架来玩玩这东西。
##Seneca简介
官方上是这样写的
> This toolkit lets you focus on the real, "business" code of your app. Instead of worrying about which database to use, how to structure your components, or how to manage dependencies, you can just start coding.
> You write everything as a command. Your commands get called whenever they match a set of properties. Your calling code doesn't know, or care, which command gets the work done. A JavaScript object goes in, and one comes out, asynchronously.'
一个不是很好的翻译便是
这个工具包的目的是让你可以集中于编写可用于产品环境的代码。而不是关心于使用哪个数据库,怎样去构建你的组件,或者是如何管理依赖,你需要的只是开始编程。
你写的一切代码将作为一个命令。当一些与属性集相匹配时则会调用你的命令。我的调用代码也不知道,或者说不关心,哪个命令完成了工作。在JavaScript对象中,一个进来,同步?
##Seneca实战
###Seneca安装
与工具名一致的npm安装
npm install seneca
引入这个库
var seneca = require('seneca')()
###Seneca 示例
引自官方的代码
var seneca = require('seneca')()
seneca.add( {role:'math', cmd:'sum'}, function(args,callback) {
var sum = args.left + args.right
callback(null,{answer:sum})
})
seneca.act( {role:'math', cmd:'sum', left:1, right:2}, function(err,result) {
if( err ) return console.error( err )
console.log(result)
})
运行这个示例
node example.js
运行结果
2014-05-27T02:49:45.456Z INFO hello Seneca/0.5.17/5h2jdv
{ answer: 3 }