Routing with distributed services and axon server - Routing key doesn't work - V2

Hi,

I’ve started this topic in the old google forum: https://groups.google.com/g/axonframework/c/t6T4cDPzZ6s/m/0R66GG4tBgAJ

@Steven_van_Beelen: I hope I understand your comment in a right way, so the routing key should just be the application name, to make sure that always this service will get the commands? Sadly this is also not working. I’ve two services running, and when I fire a command on Service A, it still happens that Service B is working on it, although Service A has also the Aggregate to work on the command. But you are right, when Axon Server found his path, he always routes to this Service. (But sadly not the service I like. )

And both services are using the following Routing Strategy:

@Primary
@ConditionalOnProperty(prefix = "esp.axon.custom-routing", name = "enabled", havingValue = "true", matchIfMissing = true)
@Component
@Slf4j
public class EspRoutingStrategy extends AnnotationRoutingStrategy {

    private final String routingPrefix;

    public EspRoutingStrategy(@Value("${esp.axon.custom-routing.cluster.name:}") final String clusterName,
                              @Value("${spring.application.name:}") final String applicationName) {
        if(!clusterName.isEmpty()) {
            routingPrefix = replaceBlanks(clusterName);
        } else if(!applicationName.isEmpty()){
            routingPrefix = replaceBlanks(applicationName);
        } else {
            throw new IllegalStateException("At least spring.application.name hast to be configured!");
        }

        log.info("Use EspRoutingStrategy for Axon! RoutingKey: {}", routingPrefix);
    }

    @Override
    public String getRoutingKey(final CommandMessage<?> command) {
        //First let axon resolve the key, looks like e.g.: de.edeka.digital.esp.Request#EXAMPLE:2020-09-08:0001
        //final String routingKey = super.getRoutingKey(command);
        //log.trace("Routing Key from Axon: {}", routingKey);
        final String newRoutingKey = routingPrefix;
        log.trace("Routing Key with Prefix: {}", newRoutingKey);
        return newRoutingKey;
    }

    protected String replaceBlanks(final String value) {
        return value.replaceAll("\\s+","");
    }
}

(I extend AnnotationRoutingStrategy, because when I created the post, I thought I need a routing key where I join the app name + the original routing key. So I got something like this: ServiceA#de.edeka.digital.esp.Request#EXAMPLE:2020-09-08:0001

Cheers,
Patrick

Hi Patrick,

that’s not actually quite how it works. The value of a routing key doesn’t primarily instruct Axon where to send it. It is just that two messages with the same routing key will be routed to the same destination. So putting the application name in the routing key, doesn’t mean it gets routed to that destination.

In the AnnotationRoutingStrategy, you can provide a policy to use when no annotation can be found on an object. The default is to fail, but you can also choose a fixed value (meaning those messages all go to the same destination) or a random value.

Influencing in detail where a message is routed is something that currently only AxonServer supports. Unless you implement your own RoutingStrategy…