Skip to content

Commit

Permalink
Merge pull request #24 from AxonFramework/feature/17
Browse files Browse the repository at this point in the history
[#17] Allow enforcement of fallback solution
  • Loading branch information
smcvb authored May 7, 2020
2 parents 09be2a3 + c0d269e commit ab5ab21
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class SpringCloudHttpBackupCommandRouter extends SpringCloudCommandRouter
private final RestTemplate restTemplate;
private final String messageRoutingInformationEndpoint;
private final MessageRoutingInformation unreachableService;
private final boolean enforceHttpDiscovery;

private volatile MessageRoutingInformation messageRoutingInfo;

Expand All @@ -92,6 +93,7 @@ protected SpringCloudHttpBackupCommandRouter(Builder builder) {
super(builder);
this.restTemplate = builder.restTemplate;
this.messageRoutingInformationEndpoint = builder.messageRoutingInformationEndpoint;
this.enforceHttpDiscovery = builder.enforceHttpDiscovery;
messageRoutingInfo = null;
unreachableService = new MessageRoutingInformation(0, DenyAll.INSTANCE, serializer);
}
Expand Down Expand Up @@ -132,6 +134,10 @@ public MessageRoutingInformation getLocalMessageRoutingInformation() {

@Override
protected Optional<MessageRoutingInformation> getMessageRoutingInformation(ServiceInstance serviceInstance) {
if (enforceHttpDiscovery) {
return requestMessageRoutingInformation(serviceInstance);
}

Optional<MessageRoutingInformation> defaultMessageRoutingInfo =
super.getMessageRoutingInformation(serviceInstance);
return defaultMessageRoutingInfo.isPresent() ?
Expand Down Expand Up @@ -198,6 +204,7 @@ public static class Builder extends SpringCloudCommandRouter.Builder {

private RestTemplate restTemplate;
private String messageRoutingInformationEndpoint = "/message-routing-information";
private boolean enforceHttpDiscovery = false;

public Builder() {
serviceInstanceFilter(ACCEPT_ALL_INSTANCES_FILTER);
Expand Down Expand Up @@ -270,6 +277,19 @@ public Builder messageRoutingInformationEndpoint(String messageRoutingInformatio
return this;
}

/**
* Enforces the back up solution provided by this {@link SpringCloudCommandRouter} to be used for retrieving
* {@link MessageRoutingInformation}. This should be toggled on if the utilized Spring Cloud Discovery mechanism
* has an inconsistent metadata update policy on the {@link ServiceInstance}, which can lead to inconsistent
* {@code MessageRoutingInformation} being shared.
*
* @return the current Builder instance, for fluent interfacing
*/
public Builder enforceHttpDiscovery() {
this.enforceHttpDiscovery = true;
return this;
}

/**
* Initializes a {@link SpringCloudHttpBackupCommandRouter} as specified through this Builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
import org.axonframework.commandhandling.distributed.commandfilter.AcceptAll;
import org.axonframework.commandhandling.distributed.commandfilter.DenyAll;
import org.axonframework.serialization.xml.XStreamSerializer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.*;
import org.junit.runner.*;
import org.mockito.*;
import org.mockito.junit.*;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
Expand Down Expand Up @@ -243,6 +240,37 @@ public void testGetMessageRoutingInformationReturnsUnreachableMessageRoutingInfo
assertEquals(messageRoutingInformationEndpoint, resultUri.getPath());
}

@Test
public void testGetMessageRoutingInformationWithEnforcedHttpDiscovery() {
SpringCloudHttpBackupCommandRouter testSubject =
SpringCloudHttpBackupCommandRouter.builder()
.discoveryClient(discoveryClient)
.localServiceInstance(localServiceInstance)
.routingStrategy(routingStrategy)
.restTemplate(restTemplate)
.messageRoutingInformationEndpoint(messageRoutingInformationEndpoint)
.contextRootMetadataPropertyName(contextRootMetadataPropertyName)
.enforceHttpDiscovery()
.build();

ServiceInstance remoteInstance = mock(ServiceInstance.class);
when(remoteInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID);
when(remoteInstance.getUri()).thenReturn(testRemoteUri);

Optional<MessageRoutingInformation> result = testSubject.getMessageRoutingInformation(remoteInstance);

assertTrue(result.isPresent());
assertEquals(expectedMessageRoutingInfo, result.get());

verify(restTemplate).exchange(uriArgumentCaptor.capture(),
eq(HttpMethod.GET),
eq(HttpEntity.EMPTY),
eq(MessageRoutingInformation.class));

URI resultUri = uriArgumentCaptor.getValue();
assertEquals(messageRoutingInformationEndpoint, resultUri.getPath());
}

@Test
public void testUpdateMembershipsOnHeartbeatEventRequestsMessageRoutingInformationByHttpRequest() {
testSubject.updateMembership(LOAD_FACTOR, COMMAND_NAME_FILTER);
Expand Down

0 comments on commit ab5ab21

Please sign in to comment.