Skip to content

ADR 0006: Dynamic Bus Routing for Custom Configuration Namespaces

Author: Emir Boyacı • Scope: util-configuration

AirCommerce microservices leverage util-configuration to retrieve externalized configuration properties partitioned into logical namespaces (e.g., common, iflyres-connector, booking). When an operational configuration is modified, Configuration Service emits a ConfigurationChangedEvent via Spring Cloud Bus over RabbitMQ, addressed directly to the affected namespace (e.g., destinationService = 'common:**').

However, Spring Cloud Bus routes events by evaluating the event’s destinationService against the receiving application’s spring.cloud.bus.id (which defaults to spring.application.name:profile:port). Because a service named connector-iflyres does not naturally match a destination of common:**, Spring’s internal DefaultBusPathMatcher silently dropped the events before they ever reached our cache invalidation listeners.

Key architectural challenges:

  • Missed Invalidation Events: Services did not receive updates for namespaces whose names differed from their application name.
  • Multiple Subscriptions: A single microservice often consumes multiple configuration namespaces (e.g., shared common plus domain-specific iflyres-connector), requiring a one-to-many routing strategy.
  • Network & Computation Waste: Broadcasting all configuration events indiscriminately to ** would force every microservice in the cluster to execute redundant HTTP cache refresh queries for irrelevant namespaces.
  • Performance & Efficiency: Ensure zero-broadcasting; events must route strictly and exclusively to the microservices that consume the affected namespace.
  • Developer Experience (DX): Developers should only declare aircommerce.configuration.namespaces in their YAML configuration, without needing to understand underlying Spring Cloud Bus routing internals or duplicate settings.
  • Ecosystem Compatibility: The mechanism must preserve native Spring Cloud Bus behavior and standard spring.cloud.config.name alias resolution without regressions.
  • Deserialization Robustness: Ensure distributed event payloads cleanly deserialize across heterogeneous microservice runtimes without default constructor failures.
  • Option 1: Broadcast All Events (**)
  • Option 2: Force Manual Configuration of spring.cloud.config.name
  • Option 3 (Chosen): Dynamic ServiceMatcher Bean Bridging Custom Namespaces

We chose Option 3, because it safely bridges our custom domain concepts (aircommerce.configuration.namespaces) directly into the native Spring Cloud Bus routing infrastructure. This enables perfectly granular event routing without forcing developers to manually duplicate properties or understand internal bus matching semantics.

The diagram below illustrates how util-configuration embeds within downstream consumers (such as iFlyRes Connector and Business Orchestrator Service), listens on the Configuration Events Bus, and evicts entries from the local cache:

Dynamic ServiceMatcher via BusMatcherConfig

Section titled “Dynamic ServiceMatcher via BusMatcherConfig”

We introduced BusMatcherConfig, which defines a custom ServiceMatcher bean named pathServiceMatcher. By matching the exact bean name expected by PathServiceMatcherAutoConfiguration, Spring gracefully backs off its default auto-configuration:

package one.aircommerce.util.configuration.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.cloud.bus.PathServiceMatcher;
import org.springframework.cloud.bus.ServiceMatcher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.PathMatcher;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@Configuration
@ConditionalOnClass(ServiceMatcher.class)
public class BusMatcherConfig {
@Bean
public ServiceMatcher pathServiceMatcher(
PathMatcher busPathMatcher,
BusProperties busProperties,
Environment environment,
ConfigurationUtilProperties configurationUtilProperties) {
String[] configNames = environment.getProperty("spring.cloud.config.name", String[].class, new String[0]);
Set<String> allNamespaces = new HashSet<>(Arrays.asList(configNames));
if (configurationUtilProperties.getNamespaces() != null) {
allNamespaces.addAll(configurationUtilProperties.getNamespaces());
}
return new PathServiceMatcher(busPathMatcher, busProperties.getId(), allNamespaces.toArray(new String[0]));
}
}

Rather than completely overwriting bus aliases, the custom ServiceMatcher preserves native Spring Cloud Bus behavior. It extracts the standard spring.cloud.config.name array from the Environment, merges it into a HashSet with our custom aircommerce.configuration.namespaces, and supplies the unified collection to PathServiceMatcher. This ensures that standard Spring Cloud Config applications running alongside util-configuration suffer zero routing degradation.

To guarantee fault-tolerant deserialization across Jackson object mappers in consuming services, @JsonCreator was added to the default constructor of ConfigurationChangedEvent:

package one.aircommerce.util.configuration.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class ConfigurationChangedEvent extends RemoteApplicationEvent {
private String key;
private ConfigurationChangeAction action;
@JsonCreator
public ConfigurationChangedEvent() {
}
// ...
}
  • Good: Solves the routing issue without consumer-side custom matching logic.
  • Bad: Triggers unnecessary cache invalidations and network fetches across all microservices for every single configuration lifecycle event, regardless of namespace relevance.

Option 2: Force Manual Configuration of spring.cloud.config.name

Section titled “Option 2: Force Manual Configuration of spring.cloud.config.name”
  • Good: Relies entirely on out-of-the-box Spring Cloud Bus routing without custom beans.
  • Bad: Severe developer experience penalty. Developers must duplicate namespace definitions across both aircommerce.configuration.namespaces and spring.cloud.config.name in application.yaml, inevitably causing configuration drift, human error, and missed invalidations.

Option 3 (Chosen): Dynamic ServiceMatcher Bean Bridging Custom Namespaces

Section titled “Option 3 (Chosen): Dynamic ServiceMatcher Bean Bridging Custom Namespaces”
  • Good: Transparent to developers; provides strictly granular routing (zero-broadcasting) and full backward compatibility with native Spring Cloud properties.
  • Bad: Slightly couples util-configuration to Spring Cloud Bus internals, requiring monitoring if the upstream PathServiceMatcher API evolves in future major Spring Cloud releases.
  • Granular Route Matching Tests: Integration test suites verify that an application declaring namespaces: [common, iflyres-connector] successfully receives events addressed to common:** and iflyres-connector:**, while cleanly ignoring events addressed to billing:** or booking:**.
  • Legacy Property Preservation Tests: Verified that services configuring standard spring.cloud.config.name continue to match their native alias patterns alongside custom namespaces.
  • Jackson Deserialization Tests: Verified that ConfigurationChangedEvent instances deserialize cleanly from JSON payloads across Jackson configurations without requiring reflection workarounds.
  • Developer Simplicity: Microservice developers only need to specify their consumed namespaces in application.yaml:

    spring:
    application:
    name: connector-iflyres
    aircommerce:
    configuration:
    namespaces:
    - common
    - iflyres-connector
  • Zero Breaking Changes: Fully backward compatible with existing services. Services that only use their default application name continue to function without modifications.