This pet store backend cell consists of 4 microservices.
- Catalog (Catalog of the accessories available in the pet store)
- Customers (Existing customers of the Pet Store)
- Orders (Orders placed at the Pet Store by Customers)
- Controller (Controller service which fetches data from the above 3 microservices and processes them to provide useful functionality)
All 4 micro services are implemented in node.js.
The below shown is the cell file for the pet-be cell which is in pet-be.bal.
import celleryio/cellery;
public function build(cellery:ImageName iName) returns error? {
// Orders Component
// This component deals with all the orders related functionality.
cellery:Component ordersComponent = {
name: "orders",
source: {
image: "wso2cellery/samples-pet-store-orders:latest"
},
ingresses: {
orders: <cellery:HttpApiIngress>{
port: 80
}
}
};
// Customers Component
// This component deals with all the customers related functionality.
cellery:Component customersComponent = {
name: "customers",
source: {
image: "wso2cellery/samples-pet-store-customers:latest"
},
ingresses: {
customers: <cellery:HttpApiIngress>{
port: 80
}
}
};
// Catalog Component
// This component deals with all the catalog related functionality.
cellery:Component catalogComponent = {
name: "catalog",
source: {
image: "wso2cellery/samples-pet-store-catalog:latest"
},
ingresses: {
catalog: <cellery:HttpApiIngress>{
port: 80
}
}
};
// Controller Component
// This component deals depends on Orders, Customers and Catalog components.
// This exposes useful functionality from the Cell by using the other three components.
cellery:Component controllerComponent = {
name: "controller",
source: {
image: "wso2cellery/samples-pet-store-controller:latest"
},
ingresses: {
controller: <cellery:HttpApiIngress>{
port: 80,
context: "controller",
expose: "local",
definition: <cellery:ApiDefinition>cellery:readSwaggerFile(
"./resources/pet-store.swagger.json")
}
},
envVars: {
CATALOG_HOST: { value: cellery:getHost(catalogComponent) },
CATALOG_PORT: { value: 80 },
ORDER_HOST: { value: cellery:getHost(ordersComponent) },
ORDER_PORT: { value: 80 },
CUSTOMER_HOST: { value: cellery:getHost(customersComponent) },
CUSTOMER_PORT: { value: 80 }
},
dependencies: {
components: [catalogComponent, ordersComponent, customersComponent]
}
};
// Cell Initialization
cellery:CellImage petStoreBackendCell =
components: {
catalog: catalogComponent,
customer: customersComponent,
orders: ordersComponent,
controller: controllerComponent
}
};
return cellery:createImage(petStoreBackendCell, untaint iName);
}
public function run(cellery:ImageName iName, map<cellery:ImageName> instances) returns error? {
cellery:CellImage petStoreBackendCell = check cellery:constructCellImage(untaint iName);
return cellery:createInstance(petStoreBackendCell, iName, instances);
}
- The
build
method will be executed whencellery build
is performed, and user can pass thecellery:ImageName iName
as a parameter during cellery build. And therefore the controller component's HttpAPIIngress's API definition is updated by reading the swagger file in thebuild
method. - There are four components defined to deploy four micro-services (catalog, orders, customers, and controller), and all four components have HTTP ingress to receive the external requests.
- Only
controller
component has definedexpose
parameter tolocal
in the HttpAPIIngress, and therefore onlycontroller
component is exposed as cell API, and all other three components are only accessible within the cell and not from other cells. - The API definition of the controller micro service is defined in the swagger file pet-store.swagger.json.
- The
controller
component cell has definedenvVars
to get the runtime value of host and ports for other components catalog, customer, and orders. - All four components are defined included in the cell during
petStoreBackendCell
initialization. cellery:getHost
method will return the actual host value of other micro-services components so that respectiveenvVars
will be properly set at thecontroller
component.- Finally, the method
cellery:createImage
within thebuild
method will create the cell image.
- The
run
method will be executed whencellery run
is performed. cellery:ImageName iName
will be passed into the run method which will have both cell image and the corresponding instance name that the cellery runtime should spawn.- As the cell image is already built during
build
method, users can only change theenvVars
in the component and pass those as runtime values. - Finally, the method
cellery:createInstance
will spawn an instance of thecellery:ImageName iName