public class SpringCloudCommandRouter extends Object implements CommandRouter
CommandRouter
implementation which uses Spring Cloud's
DiscoveryClient
s to propagate its CommandMessage Routing
Information, and to discover other Axon nodes and retrieve their Message Routing Information.
It does so by utilizing the metadata contained in a ServiceInstance
for
storing the Message Routing Information in.
Other nodes discovered through the DiscoveryClient system which do not contain any of the required Message Routing
Information fields will be black listed, so not to perform any unneeded additional checks on that node.Modifier and Type | Field and Description |
---|---|
protected XStreamSerializer |
serializer |
Constructor and Description |
---|
SpringCloudCommandRouter(org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient,
org.springframework.cloud.client.serviceregistry.Registration localServiceInstance,
RoutingStrategy routingStrategy)
Initialize a
CommandRouter with the given DiscoveryClient to update its own membership as a CommandRouter and to create its own awareness of available nodes to send commands to in a ConsistentHash . |
SpringCloudCommandRouter(org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient,
org.springframework.cloud.client.serviceregistry.Registration localServiceInstance,
RoutingStrategy routingStrategy,
Predicate<org.springframework.cloud.client.ServiceInstance> serviceInstanceFilter)
Initialize a
CommandRouter with the given DiscoveryClient to update its own membership as a CommandRouter and to create its own awareness of available nodes to send commands to in a ConsistentHash . |
SpringCloudCommandRouter(org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient,
org.springframework.cloud.client.serviceregistry.Registration localServiceInstance,
RoutingStrategy routingStrategy,
Predicate<org.springframework.cloud.client.ServiceInstance> serviceInstanceFilter,
ConsistentHashChangeListener consistentHashChangeListener)
Initialize a
CommandRouter with the given DiscoveryClient to update its own membership as a CommandRouter and to create its own awareness of available nodes to send commands to in a ConsistentHash . |
Modifier and Type | Method and Description |
---|---|
protected Member |
buildMember(org.springframework.cloud.client.ServiceInstance serviceInstance)
|
Optional<Member> |
findDestination(CommandMessage<?> commandMessage)
Returns the member instance to which the given
message should be routed. |
protected Optional<MessageRoutingInformation> |
getMessageRoutingInformation(org.springframework.cloud.client.ServiceInstance serviceInstance)
Retrieve the
MessageRoutingInformation of the provided serviceInstance . |
void |
resetLocalMembership(org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent event)
Update the local member and all the other remote members known by the
DiscoveryClient to be able to have an as up-to-date awareness
of which actions which members can handle. |
static boolean |
serviceInstanceMetadataContainsMessageRoutingInformation(org.springframework.cloud.client.ServiceInstance serviceInstance)
Boolean check if the metadata
Map of the given
ServiceInstance contains any of the expected message routing info keys. |
void |
updateMembership(int loadFactor,
Predicate<? super CommandMessage<?>> commandFilter)
Updates the load factor and capabilities of this member representing the current endpoint if the implementation
allows memberships to be updated dynamically.
|
void |
updateMemberships(org.springframework.cloud.client.discovery.event.HeartbeatEvent event)
Update the memberships of all nodes known by the
DiscoveryClient to be able to have an as up-to-date awareness
of which actions which members can handle. |
protected final XStreamSerializer serializer
public SpringCloudCommandRouter(org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient, org.springframework.cloud.client.serviceregistry.Registration localServiceInstance, RoutingStrategy routingStrategy)
CommandRouter
with the given DiscoveryClient
to update its own membership as a CommandRouter
and to create its own awareness of available nodes to send commands to in a ConsistentHash
.
The routingStrategy
is used to define the key based on which Command Messages are routed to their
respective handler nodes.
Uses a default Predicate<ServiceInstance>
filter function to remove any
ServiceInstance
which miss message routing information key-value pairs
in their metadata. This is thus the
serviceInstanceMetadataContainsMessageRoutingInformation(ServiceInstance serviceInstance)
function.discoveryClient
- The DiscoveryClient
used to discovery and notify other nodeslocalServiceInstance
- A Registration
representing
the local Service Instance of this application. Necessary to differentiate between
other instances for correct message routingroutingStrategy
- The strategy for routing Commands to a Nodepublic SpringCloudCommandRouter(org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient, org.springframework.cloud.client.serviceregistry.Registration localServiceInstance, RoutingStrategy routingStrategy, Predicate<org.springframework.cloud.client.ServiceInstance> serviceInstanceFilter)
CommandRouter
with the given DiscoveryClient
to update its own membership as a CommandRouter
and to create its own awareness of available nodes to send commands to in a ConsistentHash
.
The routingStrategy
is used to define the key based on which Command Messages are routed to their
respective handler nodes.
A Predicate<ServiceInstance>
to filter a ServiceInstance
from
the membership update loop.discoveryClient
- The DiscoveryClient
used to discovery and notify other nodeslocalServiceInstance
- A Registration
representing
the local Service Instance of this application. Necessary to differentiate between
other instances for correct message routingroutingStrategy
- The strategy for routing Commands to a NodeserviceInstanceFilter
- The Predicate<ServiceInstance>
used to filter ServiceInstance
from the update membership loop.public SpringCloudCommandRouter(org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient, org.springframework.cloud.client.serviceregistry.Registration localServiceInstance, RoutingStrategy routingStrategy, Predicate<org.springframework.cloud.client.ServiceInstance> serviceInstanceFilter, ConsistentHashChangeListener consistentHashChangeListener)
CommandRouter
with the given DiscoveryClient
to update its own membership as a CommandRouter
and to create its own awareness of available nodes to send commands to in a ConsistentHash
. The given consistentHashChangeListener
is
notified about changes in membership that affect routing of messages.
The routingStrategy
is used to define the key based on which Command Messages are routed to their
respective handler nodes.
A Predicate<ServiceInstance>
to filter a ServiceInstance
from
the membership update loop.discoveryClient
- The DiscoveryClient
used to discovery and notify other nodeslocalServiceInstance
- A Registration
representing the local Service Instance of this application. Necessary to
differentiate between other instances for correct message routingroutingStrategy
- The strategy for routing Commands to a NodeserviceInstanceFilter
- The Predicate<ServiceInstance>
used to filter ServiceInstance
from the update membership
loop.consistentHashChangeListener
- The callback to invoke when there is a change in the ConsistentHashpublic static boolean serviceInstanceMetadataContainsMessageRoutingInformation(org.springframework.cloud.client.ServiceInstance serviceInstance)
Map
of the given
ServiceInstance
contains any of the expected message routing info keys.serviceInstance
- The ServiceInstance
to check its metadata keys
fromserviceInstance
contains all expected message routing info keys; false if one
of the expected message routing info keys is missing.public Optional<Member> findDestination(CommandMessage<?> commandMessage)
CommandRouter
message
should be routed. If no suitable member could be
found an empty Optional is returned.findDestination
in interface CommandRouter
commandMessage
- the command message to find a member forpublic void updateMembership(int loadFactor, Predicate<? super CommandMessage<?>> commandFilter)
CommandRouter
updateMembership
in interface CommandRouter
loadFactor
- the new load factor of the member for this endpointcommandFilter
- the new capabilities of the member for this endpoint@EventListener public void resetLocalMembership(org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent event)
DiscoveryClient
to be able to have an as up-to-date awareness
of which actions which members can handle. This function is automatically triggered by an (unused)
InstanceRegisteredEvent
. Upon this event we may assume
that the application has fully start up. Because of this we can update the local member with the correct name and
URI
, as initially these were not provided by the
Registration
yet.event
- an unused InstanceRegisteredEvent
, serves
as a trigger for this functionbuildMember(ServiceInstance)
@EventListener public void updateMemberships(org.springframework.cloud.client.discovery.event.HeartbeatEvent event)
DiscoveryClient
to be able to have an as up-to-date awareness
of which actions which members can handle. This function is automatically triggered by
an (unused) HeartbeatEvent
.event
- an unused HeartbeatEvent
, serves as a
trigger for this functionprotected Member buildMember(org.springframework.cloud.client.ServiceInstance serviceInstance)
Member
of type URI
based on the provided serviceInstance
.
This Member is later used to send, for example, Command messages to.
A deviation is made between a local and a remote member, since if local is selected to handle the command, the
local CommandBus may be leveraged. The check if a ServiceInstance
is
local is based on two potential situations: 1) the given serviceInstance
is identical to the
localServiceInstance
thus making it local or 2) the URI of the given serviceInstance
is identical
to the URI of the localServiceInstance
.
We take this route because we've identified that several Spring Cloud implementation do not contain any URI
information during the start up phase and as a side effect will throw exception if the URI is requested from it.
We thus return a simplified Member for the localServiceInstance
to not trigger this exception.protected Optional<MessageRoutingInformation> getMessageRoutingInformation(org.springframework.cloud.client.ServiceInstance serviceInstance)
MessageRoutingInformation
of the provided serviceInstance
. If the
MessageRoutingInformation could not be found, an Optional.empty()
will be returned. Otherwise the
MessageRoutingInformation will be contained in the Optional.serviceInstance
- A ServiceInstance
to retrieve MessageRoutingInformation
from.Optional
of type MessageRoutingInformation
, containing the corresponding
MessageRoutingInformation for the given serviceInstance
if it could be retrieved.Copyright © 2010–2018. All rights reserved.