Skip to content

Apollo client integration pattern

The Apollo client integration pattern refers to the methodology of configuring microservice applications to dynamically retrieve settings from the Apollo configuration center. This approach decouples application code from configuration files, allowing for centralized management, real-time updates, and environment-specific separation (e.g., Test vs. Production) without the need to rebuild or redeploy application images.

Client Configuration

Integration is primarily achieved by passing specific environment variables to the application's container or Deployment configuration^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].

Key Parameters

To connect an application (client) to the Apollo Config Service, the following Java options are typically passed via the C_OPTS environment variable^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md]:

  • -Denv: Specifies the target environment (e.g., dev, fat for test, pro for production).^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md]
  • -Dapollo.meta: Specifies the address of the Apollo Config Service.
    • Cluster Access: Typically uses an external URL like http://config.od.com or http://config-test.od.com^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].
    • Intra-namespace Access: Can utilize the internal Kubernetes service name (e.g., http://apollo-configservice:8080) when the client resides in the same namespace as the Config Service^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].

Application Integration Example

When deploying a Java application (such as a Dubbo service) in a Kubernetes environment, the configuration is injected into the Deployment resource under the env section^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].

Kubernetes Deployment Manifest

The following example demonstrates the standard pattern for integrating a Dubbo consumer or provider with Apollo. Note the C_OPTS variable which directs the application to the correct Apollo environment and server^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].

# ... (Deployment metadata omitted)
spec:
  containers:
  - name: dubbo-demo-consumer
    image: harbor.od.com/app/dubbo-demo-consumer:apollo_tag
    env:
    - name: JAR_BALL
      value: dubbo-client.jar
    - name: C_OPTS
      # Example for Test Environment
      value: "-Denv=fat -Dapollo.meta=http://config-test.od.com"
    imagePullPolicy: IfNotPresent

For a Production environment running in a dedicated namespace, the configuration might adjust the environment variable and utilize internal DNS for efficiency, provided the Apollo services are reachable within the cluster^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md]:

    env:
    - name: C_OPTS
      # Example for Production Environment
      value: "-Denv=pro -Dapollo.meta=http://apollo-configservice:8080"

Operational Workflow

  1. Centralized Configuration: Configuration items (e.g., database URLs, Dubbo ports) are managed and pushed via the [[Apollo Portal|apollo-portal]]^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].
  2. Client Pull/Push: The application client interacts with the [[Apollo Config Service|apollo-configservice]] (often via a long-polling mechanism) to fetch updates^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].
  3. Runtime Updates: When a configuration is published in the Portal, connected clients can receive the updated configuration immediately without a container restart, depending on the specific namespace and configuration setup^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].
  4. Validation: Changes can be verified by accessing the application's endpoints or observing logs to confirm the new settings are active^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].

Benefits

  • Single Image, Multiple Environments: A single Docker image can be deployed to different environments (test, prod) simply by changing the C_OPTS environment variable, ensuring parity between environments^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].
  • Dynamic Updates: Modifying properties such as dubbo.port in the Portal allows for immediate runtime adjustments without requiring a pod restart or image rebuild^[400-devops__06-Kubernetes__k8s-paas__06.在K8S中集成Apollo配置中心.md].

Sources