public class SpringCloudCommandRouter extends Object implements org.axonframework.commandhandling.distributed.CommandRouter
CommandRouter implementation which uses Spring Cloud's DiscoveryClients to propagate its Command
Message 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.
The localServiceInstance is added for correct deviation between the local instance and the instances
retrieved from the DiscoveryClient. A RoutingStrategy is in place to decide which instance will handle a
given Command Message. Lastly, a Predicate of generic type ServiceInstance can be provided to filter
out specific service instances which need to be disregarded, and a ConsistentHashChangeListener is
configurable to notify if the memberships have been updated.
| Modifier and Type | Class and Description |
|---|---|
static class |
SpringCloudCommandRouter.Builder
Builder class to instantiate a
SpringCloudCommandRouter. |
| Modifier and Type | Field and Description |
|---|---|
protected org.axonframework.serialization.Serializer |
serializer |
| Modifier | Constructor and Description |
|---|---|
protected |
SpringCloudCommandRouter(SpringCloudCommandRouter.Builder builder)
Instantiate a
SpringCloudCommandRouter based on the fields contained in the SpringCloudCommandRouter.Builder. |
| Modifier and Type | Method and Description |
|---|---|
static SpringCloudCommandRouter.Builder |
builder()
Instantiate a Builder to be able to create a
SpringCloudCommandRouter. |
protected org.axonframework.commandhandling.distributed.Member |
buildMember(org.springframework.cloud.client.ServiceInstance serviceInstance)
|
Optional<org.axonframework.commandhandling.distributed.Member> |
findDestination(org.axonframework.commandhandling.CommandMessage<?> commandMessage) |
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)
Deprecated.
no current
CapabilityDiscoveryMode bases itself on ServiceInstance.getMetadata() and
as such this filter will be removed |
void |
updateMembership(int loadFactor,
org.axonframework.commandhandling.distributed.CommandMessageFilter commandFilter) |
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 SpringCloudCommandRouter(SpringCloudCommandRouter.Builder builder)
SpringCloudCommandRouter based on the fields contained in the SpringCloudCommandRouter.Builder.
Will assert that the DiscoveryClient, localServiceInstance of type Registration, RoutingStrategy and CapabilityDiscoveryMode are not null, and will throw an AxonConfigurationException if any of them is null.
builder - the SpringCloudCommandRouter.Builder used to instantiate a SpringCloudCommandRouter instancepublic static SpringCloudCommandRouter.Builder builder()
SpringCloudCommandRouter.
The Serializer is defaulted to a XStreamSerializer, the serviceInstanceFilter to accept
all instances, and the ConsistentHashChangeListener to a no-op solution. The DiscoveryClient,
localServiceInstance of type Registration, the RoutingStrategy and the CapabilityDiscoveryMode are hard requirements and as such should be provided.
SpringCloudCommandRouter@Deprecated public static boolean serviceInstanceMetadataContainsMessageRoutingInformation(org.springframework.cloud.client.ServiceInstance serviceInstance)
CapabilityDiscoveryMode bases itself on ServiceInstance.getMetadata() and
as such this filter will be removedMap 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<org.axonframework.commandhandling.distributed.Member> findDestination(org.axonframework.commandhandling.CommandMessage<?> commandMessage)
findDestination in interface org.axonframework.commandhandling.distributed.CommandRouterpublic void updateMembership(int loadFactor,
org.axonframework.commandhandling.distributed.CommandMessageFilter commandFilter)
updateMembership in interface org.axonframework.commandhandling.distributed.CommandRouter@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. The interval of this HeartbeatEvent thus specifies the speed within which cluster
topologies, a.k.a. each nodes command handling capabilities, are propagated.event - an unused HeartbeatEvent, serves as a trigger for this functionprotected org.axonframework.commandhandling.distributed.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, CommandMessages to.
A deviation is made between a local and a remote member, since if a local node is selected to handle the command,
the local command bus may be leveraged. The check if a ServiceInstance is local is based on two potential
situations:
serviceInstance is identical to the localServiceInstance thus making it local.serviceInstance is identical to the URI of the localServiceInstance.
We take this route because we've identified that several Spring Cloud implementations 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.
serviceInstance - a ServiceInstance to build a Member forMember based on the contents of the provided serviceInstanceCopyright © 2010–2023. All rights reserved.