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


I’ve started this topic in the old google forum:

@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:

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

    private final String routingPrefix;

    public EspRoutingStrategy(@Value("${}") final String clusterName,
                              @Value("${}") final String applicationName) {
        if(!clusterName.isEmpty()) {
            routingPrefix = replaceBlanks(clusterName);
        } else if(!applicationName.isEmpty()){
            routingPrefix = replaceBlanks(applicationName);
        } else {
            throw new IllegalStateException("At least hast to be configured!");
        }"Use EspRoutingStrategy for Axon! RoutingKey: {}", routingPrefix);

    public String getRoutingKey(final CommandMessage<?> command) {
        //First let axon resolve the key, looks like e.g.:
        //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:


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…