ADR 0006: Dynamic Bus Routing for Custom Configuration Namespaces
Author: Emir Boyacı • Scope: util-configuration
1. Context and Problem Statement
Section titled “1. Context and Problem Statement”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
commonplus domain-specificiflyres-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.
2. Decision Drivers
Section titled “2. Decision Drivers”- 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.namespacesin 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.namealias resolution without regressions. - Deserialization Robustness: Ensure distributed event payloads cleanly deserialize across heterogeneous microservice runtimes without default constructor failures.
3. Considered Options
Section titled “3. Considered Options”- Option 1: Broadcast All Events (
**) - Option 2: Force Manual Configuration of
spring.cloud.config.name - Option 3 (Chosen): Dynamic
ServiceMatcherBean Bridging Custom Namespaces
4. Decision Outcome
Section titled “4. Decision Outcome”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.
4.1. Visual Architecture Slice
Section titled “4.1. Visual Architecture Slice”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:
4.2. Implementation Mechanics
Section titled “4.2. Implementation Mechanics”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])); }}Preserving Native Infrastructure
Section titled “Preserving Native Infrastructure”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.
Event Deserialization Hardening
Section titled “Event Deserialization Hardening”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() { } // ...}5. Pros and Cons of the Options
Section titled “5. Pros and Cons of the Options”Option 1: Broadcast All Events (**)
Section titled “Option 1: Broadcast All Events (**)”- 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.namespacesandspring.cloud.config.nameinapplication.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-configurationto Spring Cloud Bus internals, requiring monitoring if the upstreamPathServiceMatcherAPI evolves in future major Spring Cloud releases.
6. Validation and Verification Strategy
Section titled “6. Validation and Verification Strategy”- Granular Route Matching Tests: Integration test suites verify that an application declaring
namespaces: [common, iflyres-connector]successfully receives events addressed tocommon:**andiflyres-connector:**, while cleanly ignoring events addressed tobilling:**orbooking:**. - Legacy Property Preservation Tests: Verified that services configuring standard
spring.cloud.config.namecontinue to match their native alias patterns alongside custom namespaces. - Jackson Deserialization Tests: Verified that
ConfigurationChangedEventinstances deserialize cleanly from JSON payloads across Jackson configurations without requiring reflection workarounds.
7. Migration and Rollout Consequences
Section titled “7. Migration and Rollout Consequences”-
Developer Simplicity: Microservice developers only need to specify their consumed namespaces in
application.yaml:spring:application:name: connector-iflyresaircommerce: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.