Docker中Spring Boot微服务Config Server配置文件获取异常排查
Docker环境下Spring Cloud Config Server持续重复加载配置文件问题
问题现象
执行docker-compose up后,Config Server容器持续输出以下日志:
Adding property source: Config resource ' file [/tmp/config-repo-11055867458011548527/application.yml]' via location 'file:/tmp/config-repo-11055867458011548527/'
当前环境配置
使用的docker-compose.yml内容如下:
services: serviceregistry: image: 'noyandocker/serviceregistry:latest' container_name: serviceregistry ports: - '8761:8761' networks: - backend configserver: image: 'noyandocker/configserver:latest' container_name: configserver ports: - '9296:9296' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka healthcheck: test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ] interval: 10s timeout: 5s retries: 5 depends_on: - serviceregistry networks: - backend apigateway: image: 'noyandocker/apigateway:latest' container_name: apigateway ports: - '9090:9090' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - ZIPKIN_URL=http://zipkin:9411 - REDIS_URL=redis://redis:6379 depends_on: configserver: condition: service_healthy zipkin: condition: service_healthy redis: condition: service_started networks: - backend zipkin: image: openzipkin/zipkin ports: - "9411:9411" networks: - backend redis: image: redis ports: - "6379:6379" networks: - backend authservice: image: 'noyandocker/authservice:latest' container_name: authservice ports: - '7777:7777' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - spring.datasource.url=jdbc:mysql://database:3306/userdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey - spring.datasource.username=root - spring.datasource.password=ippavlova_1990 depends_on: database: condition: service_healthy configserver: condition: service_started networks: - backend productservice: image: 'noyandocker/productservice:latest' container_name: productservice ports: - '8081:8081' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - spring.datasource.url=jdbc:mysql://database:3306/productdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey - spring.datasource.username=root - spring.datasource.password=ippavlova_1990 - ZIPKIN_URL=http://zipkin:9411 depends_on: database: condition: service_healthy configserver: condition: service_started zipkin: condition: service_healthy networks: - backend orderservice: image: 'noyandocker/orderservice:latest' container_name: orderservice ports: - '8082:8082' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - spring.datasource.url=jdbc:mysql://database:3306/orderdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey - spring.datasource.username=root - spring.datasource.password=ippavlova_1990 - ZIPKIN_URL=http://zipkin:9411 depends_on: database: condition: service_healthy configserver: condition: service_started zipkin: condition: service_healthy networks: - backend paymentservice: image: 'noyandocker/paymentservice:latest' container_name: paymentservice ports: - '8083:8083' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - spring.datasource.url=jdbc:mysql://database:3306/paymentdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey - spring.datasource.username=root - spring.datasource.password=ippavlova_1990 - ZIPKIN_URL=http://zipkin:9411 depends_on: database: condition: service_healthy configserver: condition: service_started zipkin: condition: service_healthy networks: - backend database: container_name: mysql-database image: 'mysql:latest' ports: - "3306:3306" restart: always environment: #MYSQL_USER: root MYSQL_PASSWORD: ippavlova_1990 MYSQL_ROOT_PASSWORD: ippavlova_1990 volumes: - db-data:/var/lib/mysql networks: - backend healthcheck: test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] timeout: 20s retries: 10 networks: backend: driver: bridge volumes: db-data:
问题分析
这条日志反复输出,是因为Config Server使用本地文件系统作为配置仓库时,默认会开启文件监听机制。容器内的临时目录/tmp/config-repo-xxx要么是因为文件系统特性导致监听误判文件变化,要么是刷新机制配置不合理,进而触发持续的配置重新加载。
解决办法
1. 关闭文件监听机制
给Config Server添加配置,直接关闭本地仓库的自动监听:
- 若使用配置文件,添加:
spring.cloud.config.server.native.refresh-rate=0
- 若通过Docker环境变量传递,在
docker-compose.yml的configserver服务中新增环境变量:
environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - spring.cloud.config.server.native.refresh-rate=0
2. 切换为Git仓库存储配置
将配置文件迁移到Git仓库(比如你的项目仓库),修改Config Server配置指向Git仓库,避免本地临时目录的监听问题。示例配置:
spring.cloud.config.server.git.uri=https://github.com/Rapter1990/springappconfig spring.cloud.config.server.git.clone-on-start=true
同时调整Docker环境变量,确保Config Server能正常拉取Git仓库的配置。
3. 调整监听刷新间隔
如果需要保留监听机制,设置一个合理的刷新间隔(比如5分钟/300秒),避免频繁加载:
spring.cloud.config.server.native.refresh-rate=300
4. 使用固定目录存储配置
放弃使用/tmp临时目录,给Config Server指定固定的配置存储目录:
spring.cloud.config.server.native.search-locations=file:/config-repo/
然后在docker-compose.yml的configserver服务中添加卷挂载,将宿主机的配置目录映射到容器内:
configserver: # 其他配置保持不变 volumes: - ./config-repo:/config-repo
内容的提问来源于stack exchange,提问作者Sercan Noyan Germiyanoğlu
相关产品推荐
相关产品推荐

