Discovery services
Key takeaways
MemoryDiscovery | Component used to create discovery services that keep data in memory. |
Configure | Method used to configure a MemoryDiscovery component. |
Register | Method used to add connection parameters to a MemoryDiscovery component. |
resolveOne | Method used to obtain a set of connection parameters identified by a common key. |
resolveAll | Method used to obtain all sets of connection parameters identified by a common key. |
Introduction
In this tutorial, you will learn how to create and operate a discovery service that stores connection parameters in memory. We will begin by explaining the necessary pre-requisites. Then, we will continue by showing how to create the service, as well as add and extract connection parameters from it. We will finish with a comprehensive example that illustrates all the learned concepts.
Pre-requisites
To create a discovery service, we can use the MemoryDiscovery class, which models a discovery service that stores connections in memory. To import this class, we can use the following code:
import { MemoryDiscovery } from "pip-services4-config-node";
import (
cconn "github.com/pip-services4/pip-services4-go/pip-services4-config-go/connect"
)
from pip_services4_config.connect import MemoryDiscovery
Creating a discovery service
In order to create our discovery service, we need to create an instance of the MemoryDiscovery class. Here, we have two options: we add one or more sets of connection parameters to the constructor through a config object
let config = ConfigParams.fromTuples(
"key1.host", "10.1.1.100",
"key1.port", "8080",
"key2.host", "10.1.1.100",
"key2.port", "8082"
);
let discovery = new MemoryDiscovery(config);
config := cconf.NewConfigParamsFromTuples(
"key1.host", "10.1.1.100",
"key1.port", "8080",
"key2.host", "10.1.1.100",
"key2.port", "8082",
)
discovery := cconn.NewEmptyMemoryDiscovery()
config = ConfigParams.from_tuples(
"key1.host", "10.1.1.100",
"key1.port", "8080",
"key2.host", "10.1.1.100",
"key2.port", "8082"
)
discovery = MemoryDiscovery(config)
or we add them after instantiation via the configure() method.
let config = ConfigParams.fromTuples(
"key1.host", "10.1.1.100",
"key1.port", "8080",
"key2.host", "10.1.1.100",
"key2.port", "8082"
);
let discovery = new MemoryDiscovery();
discovery.configure(config);
config := cconf.NewConfigParamsFromTuples(
"key1.host", "10.1.1.100",
"key1.port", "8080",
"key2.host", "10.1.1.100",
"key2.port", "8082",
)
discovery := cconn.NewEmptyMemoryDiscovery()
discovery.Configure(context.Background(), config)
config = ConfigParams.from_tuples(
"key1.host", "10.1.1.100",
"key1.port", "8080",
"key2.host", "10.1.1.100",
"key2.port", "8082"
)
discovery = MemoryDiscovery()
discovery.configure(config)
Adding connections
Once we have created our component, we can use the register() method to add connections to our discovery service. This method takes the connection parameters to be registered as inputs. The following example shows how to use this method.
await discovery.register(ctx, "key3", ConnectionParams.fromTuples(
"host", "localhost",
"port", "8000"
)); // Returns {"host": "localhost", "port": "8000"}
discovery.Register(context.Background(), "key3", cconn.NewConnectionParamsFromTuples(
"host", "localhost",
"port", "8000",
)) // Returns {"host": "localhost", "port": "8000"}
discovery.register('123', 'key3', ConnectionParams.from_tuples(
'host', 'localhost',
'port', '8000'
)) # Returns {'host': 'localhost', 'port': '8000'}
Resolving connections
We can obtain a connection with the resolveOne() method, which considers as input parameter the key identifying the connection parameters we are looking for.
let connection = await discovery.resolveOne(ctx, "key1"); // Returns {'host': '10.1.1.100', 'port': '8080'}
connection, _ := discovery.ResolveOne(context.Background(), "key1") // Returns {'host': '10.1.1.100', 'port': '8080'}
connection = discovery.resolve_one("123", "key1") # Returns {'host': '10.1.1.100', 'port': '8080'}
Alternatively, we can use the resolveAll() method, which asks for the same inputs, but returns a list containing all the sets of connection parameters identified by a common key.
await discovery.register(ctx, "key1", ConnectionParams.fromTuples(
"param1", "val1",
"param2", "val2"
));
let connection = await discovery.resolveAll(ctx, "key1"); // Returns {'host': '10.1.1.100', 'port': '8080'}
// Returns [{'host': '10.1.1.100', 'port': '8080'}, {'param1': 'val1', 'param2': 'val2'}]
import (
cconn "github.com/pip-services4/pip-services4-go/pip-services4-config-go/connect"
)
discovery.register('123', 'key1', ConnectionParams.from_tuples(
'param1', 'val1',
'param2', 'val2'
))
connections = discovery.resolve_all(None, "key1")
# Returns [{'host': '10.1.1.100', 'port': '8080'}, {'param1': 'val1', 'param2': 'val2'}]
Complete example
In this section, we have an example that illustrates the use of a memory discovery service, from creation to addition of parameters to resolving a connection. The code is as follows:
Code Example
import { ConfigParams } from "pip-services4-components-node";
import { MemoryDiscovery, ConnectionParams } from "pip-services4-config-node";
export async function main() {
// Defining the component
let config = ConfigParams.fromTuples(
"key1.host", "10.1.1.100",
"key1.port", "8080",
"key2.host", "10.1.1.100",
"key2.port", "8082"
);
let discovery = new MemoryDiscovery();
discovery.configure(config);
// Adding more parameters
await discovery.register(ctx, "key1", ConnectionParams.fromTuples(
"param1", "val1",
"param2", "val2"
));
await discovery.register(ctx, "key3", ConnectionParams.fromTuples(
"host", "localhost",
"port", "8000"
));
// Resolving connections
console.log(await discovery.resolveOne(ctx, "key1"));
console.log(await discovery.resolveAll(ctx, "key1"));
console.log(await discovery.resolveOne(ctx, "key3"));
}
Code Example
Code Example
discovery.Register(context.Background(), "key1", cconn.NewConnectionParamsFromTuples(
"param1", "val1",
"param2", "val2",
))
connection, _ := discovery.ResolveAll(context.Background(), "key1")
// Returns [{'host': '10.1.1.100', 'port': '8080'}, {'param1': 'val1', 'param2': 'val2'}]
Code Example
Code Example
# Pre-requisites
from pip_services4_components.config import ConfigParams
from pip_services4_components.config import ConfigParams
from pip_services4_config.connect import MemoryDiscovery
# Defining the component
config = ConfigParams.from_tuples(
"key1.host", "10.1.1.100",
"key1.port", "8080",
"key2.host", "10.1.1.100",
"key2.port", "8082"
)
discovery = MemoryDiscovery()
discovery.configure(config)
# Adding more parameters
discovery.register('123', 'key1', ConnectionParams.from_tuples(
'param1', 'val1',
'param2', 'val2'
))
discovery.register('123', 'key3', ConnectionParams.from_tuples(
'host', 'localhost',
'port', '8000'
))
# Resolving connections
print(discovery.resolve_one("123", "key1"))
print(discovery.resolve_all("123", "key1"))
print(discovery.resolve_one("123", "key3"))
Which after running produces the following result:
Wrapping up
In this tutorial, we learned how to create a discovery service that stores connection parameters in memory. We also saw how to add a set of connection parameters and extract them from the component. We ended with a complete example that illustrated all the operations that can be performed on this component.