Virtual network
#
1. Network configurationNetwork configuration sample:
import { NetworkConfig } from '@cellularjs/net';import { User } from 'user/$gateway/user.cell';
export const networkConfig: NetworkConfig = [ { name: 'User', space: 'neverland', driver: User, },];
#
1.1. NameThis is where you define the real name for your cell. I mean this name will be well-known among cells. You can define any cell name in network configuration and it must be unique.
#
1.2. SpaceSpace is used to describe the location where cell is placed. In hybrid microservices architecture, cells can be separated by physical or logical space and then connect together by using virtual network.
Space is very helpful, for example, with the help of space you can decide what driver should be used for communication between cells. If cell A and cell B are relatively in same space, you will normally use local driver instead of remote driver for message transportation.
tip
What happen if you start splitting your features when it all completed? You can use CellularJS to develop microservices application step-by-step. If you want to test remote call between cells, you can easily change space
and use appropriate remote driver for communication.
#
1.3. DriverTechnically, a class that is decorated by @Cell
decorator is just a driver. For now, CellularJS has 2 types of driver: local and remote driver:
local driver
: the "cell" that you saw in previous articles is local driver. When sending request to this driver, it will invoke service and you will get internal response.remote driver
: it look likelocal driver
, but "service" in this driver will make external request and transform external response into internal response.
note
'local' is reserved key for local driver, for remote driver you can choose any name for it.
Example:
// pseudo codeimport { NetworkConfig } from '@cellularjs/net';import { User } from 'user/$gateway/user.cell';...
export const solarSystemNetwork: NetworkConfig = [ // For 1-1 relationship between cell and driver, you can configure driver like below. // User driver will be treated as local driver. { name: 'User', space: 'earth', driver: User, },
// Local driver and remote driver can co-exist, it is very flexible. // This config will be useful for scenario such as remote testing before // migrating "Article" to standalone app. // // Eg: If you want to test remote call from User to Article, you can // temporarily change Article's space to 'mars' and User cell will know it need to use // different driver for communication. // // Note: if standalone app has only one driver you can config like below: // (it is ok for a driver that configured as local driver but used as remote driver) // { // driver: ArticleHTTP, // } { name: 'Article', space: 'earth', driver: { local: Article, remote: ArticleHTTP, other: ArticleX, }, },
// In this example, Mailer is a standalone app, code is separated and deployed // somewhere on the "moon":v // // Note: This network config is from the "earth" point of view, so there is no local // driver for "Mailer" here. // // You can access Mailer cell with many protocols such as amqp, http. These below // drivers work as gateway - send request and transform external response into // internal response. { name: 'Mailer', space: 'moon', driver: { amqp: MailerAMQP, http: MailerHTTP, }, },
// If you want to use third party service with CellularJS style, you can easily // configure its driver into virtual network. { name: 'AWS', driver: AWS }, { name: 'Azure', driver: Azure }, { name: 'GCP', driver: GCP },];
(You will learn how to use specific driver for communication in the next article about "Transporter")
#
1.4. Custom dataIn addition to reserved property defined by @cellularjs/net, you can add more custom data with this property. You can get your customData by calling getResolvedCell(cellNamme).cellConfig.customData
.
#
2. Create networkcreateNetwork
function will create a global network.
import { createNetwork } from '@cellularjs/net';
(async () => { await createNetwork([ { name: 'User', space: 'neverland', driver: User, }, ]);
// ...})();