未分类

分布式Parse Server服务环境搭建

thumbnail

  • 随着近几年HTML5的兴起,前端也出现了许多新的框架,例如angular,几乎所有业务逻辑都在前端这块就处理完了,后端仅仅只是将数据库进行了一次封装,也就是说如果有办法将数据库直接变成接口调用不就省事多了吗
  • 刚好无意中看到一款叫Parse Server的开源BaaS服务,然后了解了下BaaS概念,发现正好可以解决这一问题

环境要求

  • node.js (建议 >= 8.5)
  • mongodb或者postgresql
  • redis (建议 >= 3.2)

初始化安装

这里采用pm2管理node进程

1
2
3
4
5
npm install -g pm2
mkdir baas
cd baas
npm init
npm install --save express parse-server parse-dashboard parse-server-sendmail-adapter

应用开发

  • 我们需要借助redis的subscribe方式实现websocket分布式支持,因此需要配置config.app.liveQuery.redisURL
  • 同时如果我们使用redis缓存,则需要配置config.app.cacheAdapter如下所示,但必须与liveQuery中的进行隔离,否则可能会出现问题

文件 app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const {ParseServer} = require('parse-server');
const ParseDashboard = require('parse-dashboard');
const express = require('express');
const {createServer} = require('http');
const app = express();

const config = {
app: {
appName: "baas",
serverURL: "http://127.0.0.1",
collectionPrefix: "",
databaseURI: "mongodb://127.0.0.1:27017/parse",
appId: "c8047173f0b9d402cd0eee03e4c43544",
masterKey: "01125213a2581f39567779880ca87b5e",
fileKey: "ea406365180670755c69585d5849f39a",
clientKey: "2be761418de634b23e2178fbc615abee",
javascriptKey: "0c7ed7725f7d520a0d972b8a64aa5222",
restAPIKey: "53d395a53fed3475a8e01db06639c72f",
dotNetKey: "b0f305d332bf6140915d1091d0638a72",
webhookKey: "5e620332edf519097fbcc98db8fc2b51",
liveQuery: {
redisURL: "redis://127.0.0.1:6379/0",
classNames: [
"Notification"
]
},
cacheAdapter: {
module: "parse-server/lib/Adapters/Cache/RedisCacheAdapter.js",
options: {
url: "redis://127.0.0.1:6379/1"
}
}
},
dashboard: {
users: [
{
user: "mail@wangdong.io",
pass: "wangdong"
}
]
}
};

app.use('/parse', new ParseServer(config.app));

app.use('/dashboard', new ParseDashboard(Object.assign(config.dashboard, {apps: [config.app]}), true));

const server = createServer(app);

server.listen(80, () => {
console.log('Server running on port 80');
});

ParseServer.createLiveQueryServer(server, {redisURL: config.app.liveQuery.redisURL});

启动服务

1
pm2 start app.js -i 4   # 启动4个进程,测试分布式支持是否生效

BaaS服务后台

打开 http://127.0.0.1/dashboard ,使用config.dashboard.users用户进行登录

分享到