Config Server无法从GitHub拉取department-service.properties文件求助
问题:Config Server无法拉取GitHub配置文件导致Department服务启动失败
Config Server无法从GitHub拉取department-service.properties文件,直接将数据库配置放入Department服务的application.properties时,服务能正常启动;但通过Config Server加载配置时,启动失败,提示无法配置DataSource,未指定url属性且无法确定合适的驱动类。
运行DepartmentApplication报错日志
2023-12-01T19:17:03.348+01:00 INFO 2376 --- [ main] c.e.department.DepartmentApplication : Starting DepartmentApplication using Java 17.0.7 with PID 2376 (C:\Users\manar\OneDrive\Bureau\MicroService\springboot-microservice\department\department\target\classes started by manar in C:\Users\manar\OneDrive\Bureau\MicroService\springboot-microservice) 2023-12-01T19:17:03.350+01:00 INFO 2376 --- [ main] c.e.department.DepartmentApplication : No active profile set, falling back to 1 default profile: "default" 2023-12-01T19:17:04.457+01:00 INFO 2376 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2023-12-01T19:17:04.617+01:00 INFO 2376 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 152 ms. Found 1 JPA repository interfaces. 2023-12-01T19:17:04.852+01:00 INFO 2376 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=d32c8bce-0098-31bc-a3cb-f680f42d88e3 2023-12-01T19:17:04.995+01:00 INFO 2376 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2023-12-01T19:17:04.996+01:00 INFO 2376 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactorDeferringLoadBalancerFilterConfig' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactorDeferringLoadBalancerFilterConfig] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2023-12-01T19:17:04.997+01:00 INFO 2376 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'reactorDeferringLoadBalancerExchangeFilterFunction' of type [org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2023-12-01T19:17:05.339+01:00 INFO 2376 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2023-12-01T19:17:05.350+01:00 INFO 2376 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-12-01T19:17:05.351+01:00 INFO 2376 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.15] 2023-12-01T19:17:05.450+01:00 INFO 2376 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2023-12-01T19:17:05.451+01:00 INFO 2376 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2066 ms 2023-12-01T19:17:05.643+01:00 WARN 2376 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class 2023-12-01T19:17:05.645+01:00 INFO 2376 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2023-12-01T19:17:05.701+01:00 INFO 2376 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2023-12-01T19:17:05.719+01:00 ERROR 2376 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active). Process finished with exit code 1
Config Server配置
application.properties文件
spring.application.name=config-server server.port=8888 eureka.instance.client.serverUrl.defaultZone=http://localhost:8761/eureka/ spring.cloud.config.server.git.uri=https://github.com/Mana1r/config-server-repo spring.cloud.config.server.git.clone-on-start=true spring.cloud.config.server.git.default-label=main
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-server</name> <description>Demo project for Spring Boot config-server</description> <properties> <java.version>17</java.version> <spring-cloud.version>2023.0.0-RC1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
ConfigServerApplication启动类
package com.example.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
Department服务配置
application.properties文件
spring.application.name=department-service spring.config.import=optional:configserver:http://localhost:8888
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>department</artifactId> <version>0.0.1-SNAPSHOT</version> <name>department</name> <description>Spring Boot</description> <properties> <java.version>17</java.version> <spring-cloud.version>2023.0.0-RC1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
排查与解决方案
1. 验证Config Server配置获取能力
启动Config Server后,访问http://localhost:8888/department-service/default,确认是否能返回GitHub仓库中department-service.properties的完整配置:
- 若返回404或空内容,检查仓库地址是否正确、分支名是否匹配、网络连接是否正常
- 私有仓库需添加GitHub用户名和token到Config Server的
application.properties:spring.cloud.config.server.git.username=你的GitHub用户名 spring.cloud.config.server.git.password=你的GitHub访问令牌
2. 修正Department服务的配置依赖
当前使用optional:configserver会导致Config Server不可用时服务继续启动,缺少数据库配置必然报错。改为强制依赖:
spring.config.import=configserver:http://localhost:8888
3. 检查GitHub配置文件完整性
确保department-service.properties包含完整数据库配置:
spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名 spring.datasource.username=数据库用户名 spring.datasource.password=数据库密码 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update
4. 切换到稳定版本依赖
当前使用的Spring Cloud 2023.0.0-RC1是候选版本,存在兼容性风险,改为稳定版:
- 修改
pom.xml中spring-cloud.version为2022.0.4 - 移除
repositories中的Spring Milestones仓库,使用Maven中央仓库
内容的提问来源于stack exchange,提问作者Manar
相关产品推荐
相关产品推荐

