Skip to content

Commit

Permalink
Introduce enforce http discovery option
Browse files Browse the repository at this point in the history
The SpringCloudHttpBackupCommandRouter.Builder should allow specifying
the enforcement of http command discovery

#17
  • Loading branch information
smcvb committed May 6, 2020
1 parent 0068ea6 commit c0d269e
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 @@ -74,6 +74,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 @@ -91,6 +92,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 @@ -131,6 +133,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 @@ -197,6 +203,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 @@ -269,6 +276,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 @@ -21,13 +21,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 @@ -242,6 +239,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 c0d269e

Please sign in to comment.