项目架构:springcloud + nacos + gitlab ci/cd
问题描述:在跑一个springcloud项目的时候,本地运行完全正常,而通过gitlab ci/cd部署至服务器时,发生如下错误,其原因是找不到数据库连接配置。
2022-10-17 14:28:38.943 ERROR 1 --- [ 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 (the profiles develop are currently active)
在经过一天各种Google与尝试后,依然没找到问题所在,最后在我司架构师帮助下,十分钟解决了,问题出在spring.profiles.active配置上。
本地bootstrap.yml部分配置如下
# Spring
spring:
profiles:
# 环境配置
active: rk_develop
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: [已隐藏]
namespace: ${spring.profiles.active}
运维在部署的时候,执行了以下脚本,给了一个"spring.profiles.active=${CI_BUILD_REF_NAME}“的启动入参,而”${CI_BUILD_REF_NAME}“的值为"develop”,覆盖了本地bootstrap.yml中的相应配置,导致nacos找错了命名空间。后与运维联系去掉此入参后,项目得以正常启动。
ENTRYPOINT ["java", "-XX:+UseG1GC", "-Xms512m", "-Xmx512m", "-server", "-Djava.security.egd=file:/dev/./urandom", \
"-jar", "rk.jar", "-spring.config.location=/bootstrap.yml", \
"--spring.profiles.active=${CI_BUILD_REF_NAME}"]