微服務框架:MicroMono
MicroMono 可以使用整體風格編寫微服務。MicroMono 是一個使用 monolithic 風格開發微服務的框架,允許你切換和混合 微服務/整體 風格,不需要修改代碼。之前兩者是通過 VS 連接,現在使用 micromono 可以很好的同時處理兩者。
MicroMono 包括 3 部分:
-
Web 框架 (http 路由,中間件,頁面渲染等等)
-
遠程方法調用 (RPC)
-
前端代碼管理 (JavaScript 和 CSS 靜態資產文件)
MicroMono 包含兩種類型的組件:
代碼示例
定義一個服務
// Require micromono and get the Service base class var Service = require('micromono').Service; // Subclass Service class to define your service // (Backbone/Ampersand style inheritance) var SimpleHttpService = Service.extend({ // `route` is the object where you define all your routing handlers route: { 'get::/hello/:name': function(req, res) { // Basically, this handler function will be directly attached to // internal express instance created by micromono. So, any express // route handler could be ported to micromono without any modification. var name = req.params.name; res.send('Hello, ' + name); } } }); The 'get::/hello/:name': function(req, res){...} part in above example equivalents to: var app = express(); app.get('/hello/:name', function(req, res){ var name = req.params.name; res.send('Hello, ' + name); });
服務初始化
var bodyParser = require('body-parser'); var Service = require('micromono').Service; var MongoClient = require('mongodb').MongoClient; module.exports = Service.extend({ // initialization function takes no arguments init: function() { // get the internal express instance var app = this.app; // use a middleware app.use(bodyParser.json()); var self = this; // create a new promise instance var promise = new Promise(function(resolve, reject){ // do the async operation (connect) MongoClient.connect('127.0.0.1', function(err, db){ if (err) { // reject the promise if there's an error reject(err); return; } self.db = db; // resolve when done resolve(); }); }); // init function should return a promise no matter what return promise; } });
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!