Our service is pretty much done - all that is left is to place the components we’ve developed into a process container and configure it.
When a container is started, it starts composing the microservice out of the components indicated in the configuration file. For the container to be able to build these components, it will need a component factory. In the build directory, create a BeaconsControllerFactory
class and populate it with the following code:
/src/build/BeaconsServiceFactory.ts
import { Factory } from 'pip-services4-components-node';
import { Descriptor } from 'pip-services4-components-node';
import { BeaconsMemoryPersistence } from '../../src/persistence/BeaconsMemoryPersistence';
import { BeaconsFilePersistence } from '../../src/persistence/BeaconsFilePersistence';
import { BeaconsMongoDbPersistence } from '../../src/persistence/BeaconsMongoDbPersistence';
import { BeaconsService } from '../../src/service/BeaconsService';
import { BeaconsHttpControllerV1 } from '../../src/controller/version1/BeaconsHttpControllerV1';
export class BeaconsServiceFactory extends Factory{
public static MemoryPersistenceDescriptor = new Descriptor('beacons', 'persistence', 'memory', '*', '1.0');
public static FilePersistenceDescriptor = new Descriptor('beacons', 'persistence', 'file', '*', '1.0');
public static MongoDbPersistenceDescriptor = new Descriptor('beacons', 'persistence', 'mongodb', '*', '1.0');
public static ServiceDescriptor = new Descriptor('beacons', 'service', 'default', '*', '1.0');
public static HttpControllerV1Descriptor = new Descriptor('beacons', 'controller', 'http', '*', '1.0');
constructor(){
super();
this.registerAsType(BeaconsServiceFactory.MemoryPersistenceDescriptor, BeaconsMemoryPersistence);
this.registerAsType(BeaconsServiceFactory.FilePersistenceDescriptor, BeaconsFilePersistence);
this.registerAsType(BeaconsServiceFactory.MongoDbPersistenceDescriptor, BeaconsMongoDbPersistence);
this.registerAsType(BeaconsServiceFactory.ServiceDescriptor, BeaconsService);
this.registerAsType(BeaconsServiceFactory.HttpControllerV1Descriptor, BeaconsHttpControllerV1);
}
}
/build/BeaconsControllerFactory.go
package build
import (
controllers1 "github.com/pip-services-samples/service-beacons-go/controllers/version1"
persist "github.com/pip-services-samples/service-beacons-go/persistence"
logic "github.com/pip-services-samples/service-beacons-go/service"
cbuild "github.com/pip-services4/pip-services4-go/pip-services4-components-go/build"
cref "github.com/pip-services4/pip-services4-go/pip-services4-components-go/refer"
)
type BeaconsControllerFactory struct {
cbuild.Factory
}
func NewBeaconsControllerFactory() *BeaconsControllerFactory {
c := &BeaconsControllerFactory{
Factory: *cbuild.NewFactory(),
}
memoryPersistenceDescriptor := cref.NewDescriptor("beacons", "persistence", "memory", "*", "1.0")
postgresPersistenceDescriptor := cref.NewDescriptor("beacons", "persistence", "postgres", "*", "1.0")
mongoPersistenceDescriptor := cref.NewDescriptor("beacons", "persistence", "mongodb", "*", "1.0")
filePersistenceDescriptor := cref.NewDescriptor("beacons", "persistence", "file", "*", "1.0")
controllerDescriptor := cref.NewDescriptor("beacons", "controller", "default", "*", "1.0")
httpcontrollerV1Descriptor := cref.NewDescriptor("beacons", "controller", "http", "*", "1.0")
c.RegisterType(postgresPersistenceDescriptor, persist.NewBeaconsPostgresPersistence)
c.RegisterType(mongoPersistenceDescriptor, persist.NewBeaconsMongoPersistence)
c.RegisterType(memoryPersistenceDescriptor, persist.NewBeaconsMemoryPersistence)
c.RegisterType(filePersistenceDescriptor, persist.NewBeaconsFilePersistence)
c.RegisterType(controllerDescriptor, logic.NewBeaconsController)
c.RegisterType(httpcontrollerV1Descriptor, controllers1.NewBeaconsHttpControllerV1)
return c
}
/src/build/BeaconsControllerFactory.py
from pip_services4_components.refer import Descriptor
from pip_services4_components.build import Factory
from ..logic.BeaconsService import BeaconsService
from ..persistence.BeaconsFilePersistence import BeaconsFilePersistence
from ..persistence.BeaconsMemoryPersistence import BeaconsMemoryPersistence
from ..persistence.BeaconsMongoDbPersistence import BeaconsMongoDbPersistence
from ..controllers.version1.BeaconsHttpControllersV1 import BeaconsHttpControllerV1
class BeaconsControllerFactory(Factory):
MemoryPersistenceDescriptor = Descriptor('beacons', 'persistence', 'memory', '*', '1.0')
FilePersistenceDescriptor = Descriptor('beacons', 'persistence', 'file', '*', '1.0')
MongoDbPersistenceDescriptor = Descriptor('beacons', 'persistence', 'mongodb', '*', '1.0')
ServiceDescriptor = Descriptor('beacons', 'service', 'default', '*', '1.0')
HttpControllerV1Descriptor = Descriptor('beacons', 'controller', 'http', '*', '1.0')
def __init__(self):
super(BeaconsControllerFactory, self).__init__()
self.register_as_type(BeaconsControllerFactory.MemoryPersistenceDescriptor, BeaconsMemoryPersistence)
self.register_as_type(BeaconsControllerFactory.MongoDbPersistenceDescriptor, BeaconsMongoDbPersistence)
self.register_as_type(BeaconsControllerFactory.ServiceDescriptor, BeaconsService)
self.register_as_type(BeaconsControllerFactory.HttpControllerV1Descriptor, BeaconsHttpControllerV1)
As shown in the code above, we start by creating descriptors for all of our components, and then, in the constructor, we register each component in the factory using its descriptor.
Now let’s move on to creating the container itself. In the container directory, create a BeaconsProcess file with the following code:
/src/containers/BeaconsProcess.ts
import { ProcessContainer } from 'pip-services4-container-node';
import { DefaultHttpFactory } from 'pip-services4-http-node';
import { DefaultSwaggerFactory } from 'pip-services4-swagger-node';
import {BeaconsServiceFactory} from '../build/BeaconsServiceFactory';
export class BeaconsProcess extends ProcessContainer{
public constructor(){
super('beacons', 'Beacons microservice');
this.addFactory(new BeaconsServiceFactory());
this.addFactory(new DefaultHttpFactory());
this.addFactory(new DefaultSwaggerFactory());
}
}
/containers/BeaconsProcess.go
package containers
import (
factory "github.com/pip-services-samples/service-beacons-go/build"
cproc "github.com/pip-services4/pip-services4-go/pip-services4-container-go/container"
rbuild "github.com/pip-services4/pip-services4-go/pip-services4-http-go/build"
cpg "github.com/pip-services4/pip-services4-go/pip-services4-postgres-go/build"
)
type BeaconsProcess struct {
cproc.ProcessContainer
}
func NewBeaconsProcess() *BeaconsProcess {
c := &BeaconsProcess{
ProcessContainer: *cproc.NewProcessContainer("beacons", "Beacons microservice"),
}
c.AddFactory(factory.NewBeaconsServiceFactory())
c.AddFactory(rbuild.NewDefaultHttpFactory())
c.AddFactory(cpg.NewDefaultPostgresFactory())
return c
}
/src/containers/BeaconsProcess.ts
import sys
from pip_services4_container.container import ProcessContainer
from pip_services4_http.build.DefaultRpcFactory import DefaultRpcFactory
from pip_services4_swagger.build.DefaultSwaggerFactory import DefaultSwaggerFactory
from ..build.BeaconsControllerFactory import BeaconsControllerFactory
class BeaconsProcess(ProcessContainer):
def __init__(self):
super(BeaconsProcess, self).__init__('beacons', 'Beacons microservice')
self._factories.add(BeaconsControllerFactory())
self._factories.add(DefaultRpcFactory())
self._factories.add(DefaultSwaggerFactory())
Next, add the factories that are missing from the standard container (the one from the pip-services4-container module), so that we can build all the objects our service needs. In our case, this means adding the factory for the components we’ve written, as well as the default RPC factory (from the pip-services4-rpc module), which is needed for the HTTP controller to work.
Before we run the microservice, we need to prepare an initial configuration for it. In the config folder, create a config.yml file with the following configuration:
/config/config.yml
---
---
# Container descriptor
- descriptor: "pip-services:context-info:default:default:1.0"
name: "beacons"
description: "Beacons microservice"
# Console logger
- descriptor: "pip-services:logger:console:default:1.0"
level: "trace"
# Tracer that posts records to log
- descriptor: "pip-services:tracer:log:default:1.0"
# Performance counters that post values to log
- descriptor: "pip-services:counters:log:default:1.0"
{{#unless MONGO_ENABLED}}{{#unless FILE_ENABLED}}
# Memory persistence
- descriptor: "beacons:persistence:memory:default:1.0"
{{/unless}}{{/unless}}
{{#if FILE_ENABLED}}
# File persistence
- descriptor: "beacons:persistence:file:default:1.0"
path: {{FILE_PATH}}{{#unless FILE_PATH}}"./data/beacons.json"{{/unless}}
{{/if}}
{{#if MONGO_ENABLED}}
# MongoDb persistence
- descriptor: "beacons:persistence:mongodb:default:1.0"
connection:
uri: {{MONGO_SERVICE_URI}}
host: {{MONGO_SERVICE_HOST}}{{#unless MONGO_SERVICE_HOST}}"localhost"{{/unless}}
port: {{MONGO_SERVICE_PORT}}{{#unless MONGO_SERVICE_PORT}}27017{{/unless}}
database: {{MONGO_DB}}{{#unless MONGO_DB}}"test"{{/unless}}
{{/if}}
# Service
- descriptor: "beacons:service:default:default:1.0"
# Shared HTTP Endpoint
- descriptor: "pip-services:endpoint:http:default:1.0"
connection:
protocol: http
host: 0.0.0.0
port: {{HTTP_PORT}}{{#unless HTTP_PORT}}8080{{/unless}}
# HTTP Controller V1
- descriptor: "beacons:controller:http:default:1.0"
swagger:
enable: true
# Hearbeat controller
- descriptor: "pip-services:heartbeat-controller:http:default:1.0"
# Status controller
- descriptor: "pip-services:status-controller:http:default:1.0"
# Swagger controller
- descriptor: "pip-services:swagger-controller:http:default:1.0"
Let’s take a closer look at “what’s what” in the configuration above. First of all, the file is structured in such a way that the configuration is divided into sections. Each section consists of one or more descriptors and, optionally, a set of configuration parameters. To keep things organized, a section is meant to configure no more than one component.
In our case, we first configure the context parameters for the container, to make its name and description available. This information will be displayed in the logs as well.
In the next section, the console logger is enabled, and its log level is set to “trace”.
After that, performance counters are enabled and set to send information to the logger.
The next two sections are dedicated to configuring the persistent storage. However, which one we end up using will be determined by whether or not the MONGO_ENABLED environment variable is set. If it is set, then the MongoDB persistence will be used. Otherwise, we will just default to the in-memory persistence
The service doesn’t need any special configuration parameters, so we just list its descriptor to make sure the component gets created.
To enable our microservice to work on a network, we configure an endpoint with a host + port pair. If the corresponding environment variables are set, then those values will be used. Otherwise, the default values indicated in this section will be used.
The descriptor in the next section creates our HTTP controller, which will automatically start listening for requests using the endpoint we configured in the previous section.
The last section configures controllerss that monitor the health and status of our microservice.
Now that we’ve set up the container and a valid configuration, it’s time to move on to the final Step 8. Running and testing the microservice.