A Comprehensive Guide to Using Apollo Configuration Center with Spring Boot and Kubernetes
This article provides a step‑by‑step tutorial on Apollo's configuration center, covering its core concepts, four‑dimensional model, client design, Spring Boot integration, testing of dynamic updates, and deployment via Docker and Kubernetes for cloud‑native applications.
Apollo is an open‑source configuration management center developed by Ctrip that centralizes configuration for applications across environments, clusters, and namespaces, enabling real‑time updates, permission control, and audit mechanisms.
The platform’s basic model consists of three steps: a user modifies and publishes configuration in the portal, the Apollo server notifies clients of updates, and the client pulls the latest configuration and applies it locally.
Apollo manages configuration along four dimensions—application, environment, cluster, and namespace—allowing fine‑grained separation of settings such as DEV, FAT, UAT, PRO environments, data‑center clusters, and logical namespaces.
Clients cache configuration files locally (e.g., /opt/data/{appId}/config-cache ) to ensure availability when the server is unreachable, and they maintain a long‑polling HTTP connection for push notifications, falling back to periodic pulls if needed.
To integrate Apollo with a Spring Boot project, add the apollo-client dependency in the Maven pom.xml , configure properties in application.yml (including apollo.meta , apollo.cluster , apollo.bootstrap.enabled , etc.), create a simple @RestController that reads a @Value("${test:默认值}") property, and define the main application class with @SpringBootApplication .
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
club.mydlq
apollo-demo
0.0.1
apollo-demo
Apollo Demo
1.8
org.springframework.boot
spring-boot-starter-web
com.ctrip.framework.apollo
apollo-client
1.4.0
org.springframework.boot
spring-boot-maven-plugin #应用配置
server:
port: 8080
spring:
application:
name: apollo-demo
#Apollo 配置
app:
id: apollo-test
apollo:
cacheDir: /opt/data/
cluster: default
meta: http://192.168.2.11:30002 # DEV 环境地址
autoUpdateInjectedSpringProperties: true
bootstrap:
enabled: true
namespaces: application
eagerLoad:
enabled: falseRun the application with JVM arguments that specify the target environment and config service, for example:
-Dapollo.configService=http://192.168.2.11:30002 -Denv=DEVOr start via the command line:
$ java -Dapollo.configService=http://192.168.2.11:30002 -Denv=DEV -jar apollo-demo.jarAfter deployment, you can test the dynamic configuration by accessing http://localhost:8080/test , which initially returns the value set in Apollo (e.g., 123456 ). Updating the value in the portal and publishing it instantly changes the response, demonstrating real‑time push. Rollback, deletion, and network failure scenarios are also covered, showing how the client falls back to local cache or default values.
The guide also explores how to switch environments, clusters, and namespaces by adjusting apollo.meta , apollo.cluster , and apollo.bootstrap.namespaces respectively, allowing the same codebase to run with different configurations such as PRO environment or Beijing/Shanghai clusters.
For containerization, a Dockerfile is provided that copies the built JAR, defines JAVA_OPTS and APP_OPTS environment variables, and starts the JVM. Build the image with:
$ docker build -t mydlqclub/springboot-apollo:0.0.1 .The Kubernetes deployment yaml defines a Service (NodePort) and a Deployment that sets JAVA_OPTS (e.g., -Denv=DEV ) and APP_OPTS containing all Apollo parameters, mounts the cache directory, and configures resource limits.
apiVersion: v1
kind: Service
metadata:
name: springboot-apollo
spec:
type: NodePort
ports:
- name: server
nodePort: 31080
port: 8080
targetPort: 8080
- name: management
nodePort: 31081
port: 8081
targetPort: 8081
selector:
app: springboot-apollo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-apollo
labels:
app: springboot-apollo
spec:
replicas: 1
selector:
matchLabels:
app: springboot-apollo
template:
metadata:
name: springboot-apollo
labels:
app: springboot-apollo
spec:
restartPolicy: Always
containers:
- name: springboot-apollo
image: mydlqclub/springboot-apollo:0.0.1
imagePullPolicy: Always
ports:
- containerPort: 8080
name: server
env:
- name: JAVA_OPTS
value: "-Denv=DEV"
- name: APP_OPTS
value: "\
--app.id=apollo-demo\
--apollo.bootstrap.enabled=true\
--apollo.bootstrap.eagerLoad.enabled=false\
--apollo.cacheDir=/opt/data/\
--apollo.cluster=default\
--apollo.bootstrap.namespaces=application\
--apollo.autoUpdateInjectedSpringProperties=true\
--apollo.meta=http://service-apollo-config-server-dev.mydlqcloud:8080 \"
resources:
limits:
memory: 1000Mi
cpu: 1000m
requests:
memory: 500Mi
cpu: 500mDeploy with:
$ kubectl apply -f springboot-apollo.yaml -n mydlqcloudFinally, access the service via the NodePort (e.g., http://192.168.2.11:31081/test ) to verify that the application retrieves configuration from Apollo inside the Kubernetes cluster.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.