Documentation ¶
Overview ¶
Contains implementation of connection parameters, using various connection strings, which are stripped of all credentials. If we need to configure a service, the port, ip address, protocol, and other parameters – we use the ConnectionParams object, and relevant helper classes (like ConnectionResolver), for acquiring these parameters, and for discovery of objects, components (which store and retrieve connection parameters).
Discovery Service that store a registry of various end-points (what services are where, and how to connect to them). It knows the end-points, but doesn't have the credentials to connect to them. Separated for security reasons.
IDiscovery – interface for creating registries.
MemoryDiscovery – registry that is stored in memory.
There exist 2 types of discovery:
Static discovery: all services have static IP addresses (like DNS, which also works using static discovery) that are configured from the start and don't change along the way. As of lately, used more often than dynamic, because it is simpler to use and more reliable.
Proxy (or reverse proxy) is created with a dns name, and all the dynamics of starting/restarting/switching from one host to another – everything is nice and clear for the clients. Infrastructure does all the hard work out of the box. Configure sets the static registry. Dynamic discovery: every time a service starts, it registers its address in the discovery service ("Service name" at the following address "IP"). Clients then ask to resolve the address by which the requested service can be reached. The service has a general name, by which other services can resolve it.
If a service stops working, you need to refresh its address, clean stale addresses, heartbeats must be used – lots of problems and challenges. One service can have more than one address.
Index ¶
- Variables
- func NewDefaultDiscoveryFactory() *build.Factory
- type ConnectionParams
- func NewConnectionParams(values map[string]string) *ConnectionParams
- func NewConnectionParamsFromConfig(config *config.ConfigParams) *ConnectionParams
- func NewConnectionParamsFromMaps(maps ...map[string]string) *ConnectionParams
- func NewConnectionParamsFromString(line string) *ConnectionParams
- func NewConnectionParamsFromTuples(tuples ...interface{}) *ConnectionParams
- func NewConnectionParamsFromTuplesArray(tuples []interface{}) *ConnectionParams
- func NewConnectionParamsFromValue(value interface{}) *ConnectionParams
- func NewEmptyConnectionParams() *ConnectionParams
- func NewManyConnectionParamsFromConfig(config *config.ConfigParams) []*ConnectionParams
- func (c *ConnectionParams) DiscoveryKey() string
- func (c *ConnectionParams) Host() string
- func (c *ConnectionParams) Port() int
- func (c *ConnectionParams) PortWithDefault(defaultValue int) int
- func (c *ConnectionParams) Protocol() string
- func (c *ConnectionParams) ProtocolWithDefault(defaultValue string) string
- func (c *ConnectionParams) SetDiscoveryKey(value string)
- func (c *ConnectionParams) SetHost(value string)
- func (c *ConnectionParams) SetPort(value int)
- func (c *ConnectionParams) SetProtocol(value string)
- func (c *ConnectionParams) SetUri(value string)
- func (c *ConnectionParams) Uri() string
- func (c *ConnectionParams) UseDiscovery() bool
- type ConnectionResolver
- func (c *ConnectionResolver) Add(connection *ConnectionParams)
- func (c *ConnectionResolver) Configure(config *config.ConfigParams)
- func (c *ConnectionResolver) GetAll() []*ConnectionParams
- func (c *ConnectionResolver) Register(correlationId string, connection *ConnectionParams) error
- func (c *ConnectionResolver) Resolve(correlationId string) (*ConnectionParams, error)
- func (c *ConnectionResolver) ResolveAll(correlationId string) ([]*ConnectionParams, error)
- func (c *ConnectionResolver) SetReferences(references refer.IReferences)
- type IDiscovery
- type MemoryDiscovery
- func (c *MemoryDiscovery) Configure(config *config.ConfigParams)
- func (c *MemoryDiscovery) ReadConnections(config *config.ConfigParams)
- func (c *MemoryDiscovery) Register(correlationId string, key string, connection *ConnectionParams) (result *ConnectionParams, err error)
- func (c *MemoryDiscovery) ResolveAll(correlationId string, key string) (result []*ConnectionParams, err error)
- func (c *MemoryDiscovery) ResolveOne(correlationId string, key string) (result *ConnectionParams, err error)
Constants ¶
This section is empty.
Variables ¶
var MemoryDiscoveryDescriptor = refer.NewDescriptor("pip-services", "discovery", "memory", "*", "1.0")
Creates IDiscovery components by their descriptors.
Functions ¶
func NewDefaultDiscoveryFactory ¶
Create a new instance of the factory. Returns *build.Factory
Types ¶
type ConnectionParams ¶
type ConnectionParams struct {
config.ConfigParams
}
func NewConnectionParams ¶
func NewConnectionParams(values map[string]string) *ConnectionParams
Creates a new connection parameters and fills it with values. Parameters:
- values map[string]string an object to be converted into key-value pairs to initialize this connection.
Returns *ConnectionParams
func NewConnectionParamsFromConfig ¶
func NewConnectionParamsFromConfig(config *config.ConfigParams) *ConnectionParams
Retrieves a single ConnectionParams from configuration parameters from "connection" section. If "connections" section is present instead, then is returns only the first connection element. Parameters:
- config *config.ConfigParams ConnectionParams, containing a section named "connection(s)".
Returns *ConnectionParams the generated ConnectionParams object.
func NewConnectionParamsFromMaps ¶
func NewConnectionParamsFromMaps(maps ...map[string]string) *ConnectionParams
Static method for creating a StringValueMap using the maps passed as parameters. Parameters:
- maps ...map[string]string the maps passed to this method to create a StringValueMap with.
Returns ConnectionParams the ConnectionParams created.
func NewConnectionParamsFromString ¶
func NewConnectionParamsFromString(line string) *ConnectionParams
Creates a new ConnectionParams object filled with key-value pairs serialized as a string. Parameters:
- line string a string with serialized key-value pairs as "key1=value1;key2=value2;..." Example: "Key1=123;Key2=ABC;Key3=2016-09-16T00:00:00.00Z"
Returns *ConnectionParams a new ConnectionParams object.
func NewConnectionParamsFromTuples ¶
func NewConnectionParamsFromTuples(tuples ...interface{}) *ConnectionParams
Creates a new ConnectionParams object filled with provided key-value pairs called tuples. Tuples parameters contain a sequence of key1, value1, key2, value2, ... pairs. Parameters:
- tuples ...interface{} the tuples to fill a new ConnectionParams object.
Returns *ConnectionParams a new ConnectionParams object.
func NewConnectionParamsFromTuplesArray ¶
func NewConnectionParamsFromTuplesArray(tuples []interface{}) *ConnectionParams
Method for creating a StringValueMap from an array of tuples. Parameters:
- tuples []interface{} the key-value tuples array to initialize the new StringValueMap with.
Returns *ConnectionParams the ConnectionParams created and filled by the 'tuples' array provided.
func NewConnectionParamsFromValue ¶
func NewConnectionParamsFromValue(value interface{}) *ConnectionParams
Method that creates a ConfigParams object based on the values that are stored in the 'value' object's properties. see [[RecursiveObjectReader.getProperties]] Parameters:
- value interface{} configuration parameters in the form of an object with properties.
Returns ConnectionParams generated ConnectionParams.
func NewEmptyConnectionParams ¶
func NewEmptyConnectionParams() *ConnectionParams
Creates a new connection parameters and fills it with values. Returns *ConnectionParams
func NewManyConnectionParamsFromConfig ¶
func NewManyConnectionParamsFromConfig(config *config.ConfigParams) []*ConnectionParams
Retrieves all ConnectionParams from configuration parameters from "connections" section. If "connection" section is present instead, than it returns a list with only one ConnectionParams. Parameters:
- config *config.ConfigParams a configuration parameters to retrieve connections
Returns []*ConnectionParams a list of retrieved ConnectionParams
func (*ConnectionParams) DiscoveryKey ¶
func (c *ConnectionParams) DiscoveryKey() string
Gets the key to retrieve this connection from [[DiscoveryService]]. If this key is null, than all parameters are already present. see UseDiscovery Returns string the discovery key to retrieve connection.
func (*ConnectionParams) Host ¶
func (c *ConnectionParams) Host() string
Gets the host name or IP address. Returns string the host name or IP address.
func (*ConnectionParams) Port ¶
func (c *ConnectionParams) Port() int
Gets the port number. Returns int the port number.
func (*ConnectionParams) PortWithDefault ¶
func (c *ConnectionParams) PortWithDefault(defaultValue int) int
Gets the port number. Parameters:
- defaultValue int default port number
Returns int the port number.
func (*ConnectionParams) Protocol ¶
func (c *ConnectionParams) Protocol() string
Gets the connection protocol. Returns string the connection protocol or the default value if it's not set.
func (*ConnectionParams) ProtocolWithDefault ¶
func (c *ConnectionParams) ProtocolWithDefault(defaultValue string) string
Gets the connection protocol. Parameters:
- defaultValue string the default protocol
Returns string the connection protocol or the default value if it's not set.
func (*ConnectionParams) SetDiscoveryKey ¶
func (c *ConnectionParams) SetDiscoveryKey(value string)
Sets the key to retrieve these parameters from [[DiscoveryService]]. Parameters:
- value string a new key to retrieve connection.
func (*ConnectionParams) SetHost ¶
func (c *ConnectionParams) SetHost(value string)
Sets the host name or IP address. Parameters:
- value string a new host name or IP address.
func (*ConnectionParams) SetPort ¶
func (c *ConnectionParams) SetPort(value int)
Sets the port number. see Host Parameters:
- value int
a new port number.
func (*ConnectionParams) SetProtocol ¶
func (c *ConnectionParams) SetProtocol(value string)
Sets the connection protocol. Parameters:
- value string a new connection protocol.
func (*ConnectionParams) SetUri ¶
func (c *ConnectionParams) SetUri(value string)
Sets the resource URI or connection string. Parameters:
- value string
a new resource URI or connection string.
func (*ConnectionParams) Uri ¶
func (c *ConnectionParams) Uri() string
Gets the resource URI or connection string. Usually it includes all connection parameters in it. Returns string the resource URI or connection string.
func (*ConnectionParams) UseDiscovery ¶
func (c *ConnectionParams) UseDiscovery() bool
Checks if these connection parameters shall be retrieved from DiscoveryService. The connection parameters are redirected to DiscoveryService when discovery_key parameter is set. Returns bool true if connection shall be retrieved from DiscoveryService
type ConnectionResolver ¶
type ConnectionResolver struct {
// contains filtered or unexported fields
}
Helper class to retrieve component connections.
If connections are configured to be retrieved from IDiscovery, it automatically locates IDiscovery in component references and retrieve connections from there using discovery_key parameter.
Configuration parameters connection:
discovery_key: (optional) a key to retrieve the connection from IDiscovery ... other connection parameters connections: alternative to connection
[connection params 1]: first connection parameters ... connection parameters for key 1 [connection params N]: Nth connection parameters ... connection parameters for key N References *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connections see ConnectionParams
see IDiscovery
Example config = NewConfigParamsFromTuples(
"connection.host", "10.1.1.100", "connection.port", 8080
);
connectionResolver := NewConnectionResolver(); connectionResolver.Configure(config); connectionResolver.SetReferences(references);
res, err := connectionResolver.Resolve("123");
func NewConnectionResolver ¶
func NewConnectionResolver(config *config.ConfigParams, references refer.IReferences) *ConnectionResolver
Creates a new instance of connection resolver. Parameters:
- config *config.ConfigParams component configuration parameters
- references refer.IReferences component references
Returns *ConnectionResolver
func NewEmptyConnectionResolver ¶
func NewEmptyConnectionResolver() *ConnectionResolver
Creates a new instance of connection resolver. Returns *ConnectionResolver
func (*ConnectionResolver) Add ¶
func (c *ConnectionResolver) Add(connection *ConnectionParams)
Adds a new connection to component connections Parameters:
- connection *ConnectionParams new connection parameters to be added
func (*ConnectionResolver) Configure ¶
func (c *ConnectionResolver) Configure(config *config.ConfigParams)
Configures component by passing configuration parameters. Parameters:
- config *config.ConfigParams configuration parameters to be set.
func (*ConnectionResolver) GetAll ¶
func (c *ConnectionResolver) GetAll() []*ConnectionParams
Gets all connections configured in component configuration. Redirect to Discovery services is not done at this point. If you need fully fleshed connection use resolve method instead. Returns []*ConnectionParams a list with connection parameters
func (*ConnectionResolver) Register ¶
func (c *ConnectionResolver) Register(correlationId string, connection *ConnectionParams) error
Registers the given connection in all referenced discovery services. This method can be used for dynamic service discovery. see IDiscovery Parameters:
- correlationId string transaction id to trace execution through call chain.
- connection *ConnectionParams a connection to register.
Returns error
func (*ConnectionResolver) Resolve ¶
func (c *ConnectionResolver) Resolve(correlationId string) (*ConnectionParams, error)
Resolves a single component connection. If connections are configured to be retrieved from Discovery service it finds a IDiscovery and resolves the connection there. see IDiscovery Parameters:
- correlationId: string transaction id to trace execution through call chain.
Returns *ConnectionParams, error resolved connection or error.
func (*ConnectionResolver) ResolveAll ¶
func (c *ConnectionResolver) ResolveAll(correlationId string) ([]*ConnectionParams, error)
Resolves all component connection. If connections are configured to be retrieved from Discovery service it finds a IDiscovery and resolves the connection there. see IDiscovery Parameters:
- correlationId string transaction id to trace execution through call chain.
Returns []*ConnectionParams, error resolved connections or error.
func (*ConnectionResolver) SetReferences ¶
func (c *ConnectionResolver) SetReferences(references refer.IReferences)
Sets references to dependent components. Parameters:
- references refer.IReferences references to locate the component dependencies.
type IDiscovery ¶
type IDiscovery interface { // Registers connection parameters into the discovery service. Register(correlationId string, key string, connection *ConnectionParams) (result *ConnectionParams, err error) // Resolves a single connection parameters by its key. ResolveOne(correlationId string, key string) (result *ConnectionParams, err error) // Resolves all connection parameters by their key. ResolveAll(correlationId string, key string) (result []*ConnectionParams, err error) }
Interface for discovery services which are used to store and resolve connection parameters to connect to external services.
type MemoryDiscovery ¶
type MemoryDiscovery struct {
// contains filtered or unexported fields
}
Discovery service that keeps connections in memory.
Configuration parameters [connection key 1]: ... connection parameters for key 1 [connection key 2]: ... connection parameters for key N see IDiscovery
see ConnectionParams
Example config := NewConfigParamsFromTuples(
"key1.host", "10.1.1.100", "key1.port", "8080", "key2.host", "10.1.1.100", "key2.port", "8082"
);
discovery := NewMemoryDiscovery(); discovery.ReadConnections(config);
discovery.Resolve("123", "key1", (err, connection) => { // Result: host=10.1.1.100;port=8080 });
func NewEmptyMemoryDiscovery ¶
func NewEmptyMemoryDiscovery() *MemoryDiscovery
Creates a new instance of discovery service. Returns *MemoryDiscovery
func NewMemoryDiscovery ¶
func NewMemoryDiscovery(config *config.ConfigParams) *MemoryDiscovery
Creates a new instance of discovery service. Parameters:
- config *config.ConfigParams configuration with connection parameters.
Returns *MemoryDiscovery
func (*MemoryDiscovery) Configure ¶
func (c *MemoryDiscovery) Configure(config *config.ConfigParams)
Configures component by passing configuration parameters. Parameters:
- config *config.ConfigParams
configuration parameters to be set.
func (*MemoryDiscovery) ReadConnections ¶
func (c *MemoryDiscovery) ReadConnections(config *config.ConfigParams)
Reads connections from configuration parameters. Each section represents an individual Connectionparams Parameters:
- config *configure.ConfigParams
configuration parameters to be read
func (*MemoryDiscovery) Register ¶
func (c *MemoryDiscovery) Register(correlationId string, key string, connection *ConnectionParams) (result *ConnectionParams, err error)
Registers connection parameters into the discovery service. Parameters:
- correlationId string transaction id to trace execution through call chain.
- key string a key to uniquely identify the connection parameters.
- connection *ConnectionParams
Returns *ConnectionParams, error registered connection or error.
func (*MemoryDiscovery) ResolveAll ¶
func (c *MemoryDiscovery) ResolveAll(correlationId string, key string) (result []*ConnectionParams, err error)
func (*MemoryDiscovery) ResolveOne ¶
func (c *MemoryDiscovery) ResolveOne(correlationId string, key string) (result *ConnectionParams, err error)
Resolves a single connection parameters by its key. Parameters:
- correlationId: string transaction id to trace execution through call chain.
- key: string a key to uniquely identify the connection.
Returns *ConnectionParams, error receives found connection or error.