前段時間開源數(shù)據(jù)庫界很熱鬧吩蔑,大家都紛紛跳出來指責(zé)云廠商使用代碼但是不回饋社區(qū)。MongoDB更是直接換了開源許可證不讓云廠商使用了填抬。
https://www.sohu.com/a/260120443_465914
MongoDB還想上云咋辦烛芬,除了用MongoDB自己的Atlas之外,微軟Azure還有個Cosmos DB了解一下飒责?完全兼容MongoDB赘娄,并且輕松跨Region全球部署。
接下來我們用CosmosDB替換MongoDB搭MEAN棧的Web服務(wù)器
部署Cosmos DB
登陸Azure Portal宏蛉,搜索MongoDB遣臼,可以看到直接就有一個Database as a service for MongoDB啦,這其實(shí)就是Cosmos DB的Mongo API版
按照界面填好設(shè)置點(diǎn)擊Review+Create簡單兩部就創(chuàng)建好了
點(diǎn)擊Connection String記錄數(shù)據(jù)庫連接信息
接下來我們可以創(chuàng)建數(shù)據(jù)庫test
數(shù)據(jù)庫建好拾并,接下來就可以搭Web服務(wù)器了揍堰。我們這次使用的是Ubuntu 18.04
安裝Node.js
sudo apt-get install -y nodejs
設(shè)置服務(wù)器
首先還需要安裝body-parser包,用來處理請求中的JSON
安裝npm包管理器以及正文分析包
sudo apt-get install npm
sudo npm install body-parser
創(chuàng)建名為Books的文件夾辟灰,在該文件夾中建立server.js文件,內(nèi)容如下
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
require('./apps/routes')(app);
app.set('port', 3300);
app.listen(app.get('port'), function() {
console.log('Server up: http://localhost:' + app.get('port'));
});
安裝 Express 并設(shè)置服務(wù)器的路由
Express 是一個微型的靈活 Node.js Web 應(yīng)用程序框架篡石,為 Web 和移動應(yīng)用程序提供功能芥喇。 本教程使用 Express 將書籍信息傳入和傳出 MongoDB 數(shù)據(jù)庫。 Mongoose 提供簡潔的基于架構(gòu)的解決方案來為應(yīng)用程序數(shù)據(jù)建模凰萨。 本教程使用 Mongoose 來為數(shù)據(jù)庫提供書籍架構(gòu)继控。
安裝 Express 和 Mongoose。
sudo npm install express mongoose
在 Books 文件夾中胖眷,創(chuàng)建名為 apps 的文件夾武通,并添加包含所定義的 Express 路由的、名為 routes.js 的文件珊搀。
var Book = require('./models/book');
module.exports = function(app) {
app.get('/book', function(req, res) {
Book.find({}, function(err, result) {
if ( err ) throw err;
res.json(result);
});
});
app.post('/book', function(req, res) {
var book = new Book( {
name:req.body.name,
isbn:req.body.isbn,
author:req.body.author,
pages:req.body.pages
});
book.save(function(err, result) {
if ( err ) throw err;
res.json( {
message:"Successfully added book",
book:result
});
});
});
app.delete("/book/:isbn", function(req, res) {
Book.findOneAndRemove(req.query, function(err, result) {
if ( err ) throw err;
res.json( {
message: "Successfully deleted the book",
book: result
});
});
});
var path = require('path');
app.get('*', function(req, res) {
res.sendfile(path.join(__dirname + '/public', 'index.html'));
});
};
在 apps 文件夾中冶忱,創(chuàng)建名為 models 的文件夾,并添加包含所定義的書籍模型配置的境析、名為 book.js 的文件囚枪。將其中的mongodb連接串替換成之前創(chuàng)建的。
var mongoose = require('mongoose');
mongoose.connect('mongodb://kelemongodbtest.documents.azure.cn:10255/test?ssl=true&replicaSet=globaldb',{
auth:{
user:'kelemongodbtest',
password:'2vsIXLu1aVyrnxpcjrKGOdzA2n7dwgKmfTEpD3XyckNMpu8mAVQK48oCEjJZVMeMsQTurjEVxMZvDZOvA0gM5Q=='
}
});
mongoose.connection;
mongoose.set('debug', true);
var bookSchema = mongoose.Schema( {
name: String,
isbn: {type: String, index: true},
author: String,
pages: Number
});
var Book = mongoose.model('Book', bookSchema);
module.exports = mongoose.model('Book', bookSchema);
使用 AngularJS 訪問路由
AngularJS 提供一個 Web 框架用于在 Web 應(yīng)用程序中創(chuàng)建動態(tài)視圖劳淆。 本教程使用 AngularJS 將網(wǎng)頁與 Express 相連接链沼,并針對書籍?dāng)?shù)據(jù)庫執(zhí)行操作。
將目錄切換回到 Books (cd ../..)沛鸵,然后創(chuàng)建名為 public 的文件夾括勺,并添加包含所定義的控制器配置的、名為 script.js 的文件。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http( {
method: 'GET',
url: '/book'
}).then(function successCallback(response) {
$scope.books = response.data;
}, function errorCallback(response) {
console.log('Error: ' + response);
});
$scope.del_book = function(book) {
$http( {
method: 'DELETE',
url: '/book/:isbn',
params: {'isbn': book.isbn}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log('Error: ' + response);
});
};
$scope.add_book = function() {
var body = '{ "name": "' + $scope.Name +
'", "isbn": "' + $scope.Isbn +
'", "author": "' + $scope.Author +
'", "pages": "' + $scope.Pages + '" }';
$http({
method: 'POST',
url: '/book',
data: body
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log('Error: ' + response);
});
};
});
在 public 文件夾中疾捍,創(chuàng)建包含所定義的網(wǎng)頁的奈辰、名為 index.html 的文件。
<!doctype html>
<html ng-app="myApp" ng-controller="myCtrl">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div>
<table>
<tr>
<td>Name:</td>
<td><input type="text" ng-model="Name"></td>
</tr>
<tr>
<td>Isbn:</td>
<td><input type="text" ng-model="Isbn"></td>
</tr>
<tr>
<td>Author:</td>
<td><input type="text" ng-model="Author"></td>
</tr>
<tr>
<td>Pages:</td>
<td><input type="number" ng-model="Pages"></td>
</tr>
</table>
<button ng-click="add_book()">Add</button>
</div>
<hr>
<div>
<table>
<tr>
<th>Name</th>
<th>Isbn</th>
<th>Author</th>
<th>Pages</th>
</tr>
<tr ng-repeat="book in books">
<td><input type="button" value="Delete" data-ng-click="del_book(book)"></td>
<td>{{book.name}}</td>
<td>{{book.isbn}}</td>
<td>{{book.author}}</td>
<td>{{book.pages}}</td>
</tr>
</table>
</div>
</body>
</html>
運(yùn)行應(yīng)用程序
將目錄切換回到 Books (cd ..)拾氓,并通過運(yùn)行以下命令啟動服務(wù)器:
nodejs server.js
打開 Web 瀏覽器并導(dǎo)航到針對 VM 記錄的地址冯挎。 例如,http://13.72.77.9:3300咙鞍。 應(yīng)顯示以下頁面所示的內(nèi)容:
刷新頁面之后能看到書已經(jīng)添加成功了
回到Data Explorer房官,也能看到數(shù)據(jù)庫里面已經(jīng)有了。
總結(jié)
所以雖然MongoDB現(xiàn)在已經(jīng)收緊了開源的License续滋,但是微軟的Azure上我們還是有對應(yīng)的產(chǎn)品能夠滿足大家的需求翰守,并且CosmosDB還有一些其他的特性,比如全球部署疲酌,精確定義好的一致性級別蜡峰。更多信息可以去https://docs.azure.cn/zh-cn/cosmos-db/ 查看
參考資料
https://docs.azure.cn/zh-cn/virtual-machines/linux/tutorial-mean-stack