Multiple apps in a single parse server

2019-04-28 06:16发布

问题:

I worked all week to migrate my apps hosted on parse.com to parse server, managed to make everything work perfectly, the only problem is to get it to run multiple apps on a single hardware, without my having to allocate a server app for that it has, it would become expensive.

I read this discussion about it, and on this basis, follow the following solution:

var app1 = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'myAppId1',
  masterKey: process.env.MASTER_KEY || 'myMasterKey1', //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed
  push: pushConfig,
  liveQuery: {
    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
  }
});

var app2 = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/app2',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'myAppId2',
  masterKey: process.env.MASTER_KEY || 'myMasterKey2', //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed
  push: pushConfig,
  liveQuery: {
    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
  }
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

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 = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, app1);
app.use(mountPath, app2);

This works until the time test environment can use multiple apps for sending push on the same hardware, just creating multiple instances of the server parse pointing to different database.

Can anyone tell me if something could go wrong with the apps in production? This would cause me a problem in the future?

Someone supports this solution?

Thank you!

回答1:

Parse Server as of v2.2.9 doesn't offer multi-app support.

It requires individual instances (and mount paths) for each app. You would otherwise run into complications regarding cloud code as the core is not designed for multi-app support although it carries some legacy from Parse.com like the appId property which would be a step towards it.

However since it is now an open source project it may offer multi-app support in the future.

Update

Parse server v2.2.18 still does only supports one single app per instance, according to the wiki:

Parse Server only supports single app instances. There is ongoing work to make Parse Server multi-app aware. However, if you intend to run many different apps with different datastores, you currently would need to instantiate separate instances.



回答2:

Use PM2 and launch 2 instances of Parse Server. Here's how PM2 config file would look like:

{
  "apps": [
    {
      "name": "app1",
      "script": "servers/app1/server.js",
      "cwd": "/home/parse",
      "log_file": "logs/server-app1.log",
      "error_file": "logs/server-app1-error.log",
      "log_date_format": "YYYY-MM-DD HH:mm:ss Z",
      "instances": 1,
      "merge_logs": true,
      "watch": ["/usr/bin/parse-server", "/home/parse/servers/app1"],
      "ignore_watch": ["logs"],
      "env": {"VERBOSE": true}
    },
    {
      "name": "app2",
      "script": "servers/app2/server.js",
      "cwd": "/home/parse",
      "log_file": "logs/server-app2.log",
      "error_file": "logs/server-app2-error.log",
      "log_date_format": "YYYY-MM-DD HH:mm:ss Z",
      "instances": 1,
      "merge_logs": true,
      "watch": ["/usr/bin/parse-server", "/home/parse/servers/app2"],
      "ignore_watch": ["logs"],
      "env": {"VERBOSE": true}
    },
    {
      "name": "parse-dashboard",
      "script": "/usr/bin/parse-dashboard",
      "args": "--config dashboard/parse-dashboard-config.json  --mountPath /dashboard",
      "cwd": "/home/parse",
      "log_file": "logs/dashboard.log",
      "error_file": "logs/dashboard-error.log",
      "log_date_format": "YYYY-MM-DD HH:mm:ss Z",
      "instances": 1,
      "watch": ["/usr/bin/parse-dashboard"]
    }
  ]
}