创建多个Parse Server的实例
2021-08-26 本文已影响0人
启发禅悟
一个APP对应一个Parse Server服务器应该是最好的,不过对于一个独立开发者来说,能省则省,尤其在用户量还没有上来的时候,那么一台服务器上部署多个Parse Server的实例,来对应多个APP,是不错的选择。
在网上搜罗了一大圈,大致的结论就是,可以在同一个Parse Server下建立多个实例,但是,这种情况下,每个实例不能拥有独立的Cloud Code。
所以,大部分的推荐是,用不同的端口,开启多个Parse Server,每一个Server对应一个实例。
假定,我们已经根据在Ubuntu上部署Parse Server在服务器上已经部署了一个Parse Server,下面说下如何利用不同端口开启多个Parse Server。
- 创建新的数据库。
首先链接数据库
//然后需要输入密码
mongo --port 27017 --ssl --sslAllowInvalidCertificates --authenticationDatabase admin --username sammy --password
创建一个新的DB和该DB的用户
use database_name
db.createUser({ user: "database_user", pwd: "password", roles: [ "readWrite", "dbAdmin" ] })
- 配置新的Parse Server,例如我们这里取名为makeit.js
// Packtor Server
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var ParseDashboard = require('parse-dashboard');
var path = require('path');
// Set up parse server
var api = new ParseServer({
databaseURI: '刚我们创建的新的DB',
cloud: '新的Cloud文件的绝对路径/main.js',
appId: 'makeitappid',
masterKey: 'makeitmasterkey',
serverURL: 'https://xxxx.com/parsemakeit',
publicServerURL: 'https://xxxx.com/parsemakeit',
auth: {
apple:{
}
},
verbose: true,
appName: 'Make It',
});
var app = express();
// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));
// Serve the Parse API on the /parse URL prefix
var mountPath = '/parsemakeit';
app.use(mountPath, api);
// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
res.status(200).send('Parse Server App');
});
var port = 1338;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server running on port ' + port + '.');
});
// Set up parse dashboard
var dashboard = new ParseDashboard({
"apps": [{
"serverURL": 'https://xxxx.com/parsemakeit',
"appId": 'makeitappid',
"masterKey": 'makeitmasterkey',
"appName": "Make It",
"production": false,
"iconName": "app-icon.png",
}],
"users": [
{
"user":"xxxxxx",
"pass":"xxxxxx"
}
],
"iconsFolder": "icons"
});
var dashApp = express();
// make the Parse Dashboard available at /dashboard
dashApp.use('/dashboardmakeit', dashboard);
// Parse Server plays nicely with the rest of your web routes
dashApp.get('/', function(req, res) {
res.status(200).send('Parse Dashboard App');
});
var httpServerDash = require('http').createServer(dashApp);
httpServerDash.listen(4041, function() {
console.log('dashboard-server running on port 4041.');
});
上面的配置文件,创建了一个Parse Server的实例以及其对应的Dashboard。
接下来要在Nginx中进行配置:
location /parse/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:1337/parse/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /dashboard/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:4040/dashboard/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /parsemakeit/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:1338/parsemakeit/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /dashboardmakeit/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:4041/dashboardmakeit/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
}
可以对比下parse,dashboard和新创建的parsemakeit,dashboardmakeit的区别,主要就是端口和对应的mount的名字。
最终,我们可以用https://xxxx.com/parsemakeit作为新的App的server接口,并用https://xxxx.com/dashboardmakeit进行管理。