-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Reference configuration of app.js
In pomelo there are two app.js, one is in the game-server directory and the other is in web-server directory.They are corresponding to the game server and the web server, and each of them is server's entry. The following app.js configuration reference is just for game server.
The app.js is the entrance of pomelo project.When using the pomelo command line to create a new project, a default app.js file will be generated according to the basic information of the project. Its main content is as follows:
var pomelo = require('pomelo');
var app = pomelo.createApp();
app.set('name', 'nameofproject');
app.set('dirname', __dirname);
app.defaultConfiguration();
app.start();
Application variables can be accessed through the set and get methods, for example, to access the server object, and the specific code as follows:
app.set('server',server);
var server = app.get('server');
Application states information can be activated and inactivated by the enable and disable methods, for example, you want to activate or inactivate application's schedulerService state.Also, the user can use enabled and disabled methods to judge whether the state exists, if exists returns true, otherwise returns false. Specific code as follows:
app.enable('filter');
app.enabled('filter'); //返回true
app.disable('filter');
app.disabled('filter'); //返回true
Users can load configuration files and services through loadConfig and loadService methods, after loading the file parameters will be mounted directly to the application object. For example to load mysql.json file, the specific code as follows:
{
"development":
{
"host":"127.0.0.1",
"port":"3306",
"database":"pomelo"
}
}
app.loadConfig('mysql.json');
var host = app.mysql.host; //返回 127.0.0.1
app.loadService('scheduleService');
The server configuration is mainly done by the configure () method, a complete app.configure configuration parameters are as follows:
app.configure([env], [serverType], [function]);
The first two parameters are optional, the following parameter description:
- env: Runtime environment, can be set to development, production or development | production.
- serverType: Server type, if set this parameter only do initialization on this type of server, otherwise all servers perform initialization function.
- function: The specific initialization operation, the internal can contain any js method.
Some configuration examples are as follows:
app.configure(function(){
});
This configuration will bring all servers under all modes (development / production) into effect.
app.configure('development', function(){
});
This configuration will only bring all servers under a fixed mode into effect.
app.configure('development', 'chat', function(){
});
This configuration will only bring chat servers under development mode into effect.
Users can make different configurations for different servers according to various demands of the application, for example to configure the startup file of mysql in the global
app.configure('development|production', function(){
app.set('mysql', app.get('dirname')+'/config/mysql.json');
});
Furthermore you can make configurations on a specific server, such as:
var initArea = function(){
//area init
};
app.configure('development|production', 'area', function(){
initArea();
});
Each component has a life cycle, and it is usually loaded during the initialization of application. The components of pomelo are as follows: master, monitor, filter, proxy, handler, remote, server, sync and connection, and their main function are as follows:
- master: master component is primarily responsible for starting the master server.
- monitor: monitor component is primarily responsible for starting each server monitor service, the service is responsible for collecting information of servers and regularly pushing the message to the master, maintaining the master server heartbeat connection.
- proxy: proxy component is primarily responsible for generating server rpc client, multiple server processes exist in the system, between different server processes to communicate with each other through rpc call(master server exclude).
- handler: handler component is primarily mainly responsible for loading the file the handler directory of front-end server.
- filter: filter component is primarily responsible for loading the filter for requests service, including the filter after the calling of rpc and pre-filter before the calling of rpc.
- remote: remote component is primarily responsible for loading the back-end servers, and generates rpc server.
- server: server component is primarily responsible for the start of the front-end server.
- sync: sync component is primarilyresponsible for the start data synchronization module and provide external data synchronization service.
- connection: connection component is primarily responsible for starting the service of user connection statistical data.
Most of components will be loaded by default.Components loading can use the load method, for example:
app.load(pomelo.proxy, [options]); //[options] optional
Users can have custom components depending on the application needs, custom components design can refer to the Custom Component Development Reference.
The router is mainly responsible for maintaining the routing information, route calculation, route result cache, switch routing strategies and update routing information according to the application demands. The user can customize different routing rules for different servers, and these can be configured in app.js. Such as chat server configuration routing rules:
app.route('chat', routeUtil.chat);
Users can define the routing rules for different servers, for example:
routeUtil.chat = function(session, msg, app, callback) {
var chatServers = app.getServersByType('chat');
if (!chatServers) {
callback(new Error('can not find chat servers.'));
return;
}
var server = dispatcher.dispatch(session.rid, chatServers);
callback(null, server.id);
};
The callback function returns the id of the server, and here uses the dispatcher on session.rid which just uses the hash processing to do server selection.
When a client requests to reach the server, after filter chain and handler processing, and finally generate a response back to the client. The handler is the business logic implemented, the filter is ready to work and clean-up work to achieve facilitate code modularization and reuse. pomelo itself provides some filter, another user can customize the filter according to the application needs.
- serial: 主要负责保证所有从客户端到服务端的请求能够按顺序地发送。
app.filter(pomelo.filters.serial());
- time: 主要负责统计请求的相应时间。
app.filter(pomelo.filters.time());
- timeOut: 主要负责监控请求响应时间,如果超时就给出警告。
app.filter(pomelo.filters.timeout());
var pomelo = require('pomelo');
var routeUtil = require('./app/util/routeUtil');
/**
* Init app for client.
*/
var app = pomelo.createApp();
app.set('name', 'chatofpomelo');
app.set('dirname', __dirname);
app.defaultConfiguration();
// app configure
app.configure('production|development', function() {
// route configures
app.route('chat', routeUtil.chat);
app.route('connector', routeUtil.connector);
// remote configures
app.set('remoteConfig', {
cacheMsg: true,
interval: 30
});
// filter configures
app.filter(pomelo.filters.timeout());
// mysql configures
app.loadConfig('mysql', app.get('dirname') + '/config/mysql.json');
});
// start app
app.start();