Spring启动无法从我的文件系统获取配置文件

问题描述 投票:0回答:2

我正在尝试在Spring启动和Spring云上构建一个示例应用程序。我已经在config.properties属性文件中编写了我的数据库和hibernate配置,该文件位于我的桌面中,我希望我的spring boot能够使用这个配置。

我的项目有3个模块

  • API
  • 数据层
  • ServiceLayer

这是我在API的application.property文件中提到的代码

spring.profiles.active = native spring.cloud.config.server.native.searchLocation = C:/Users/DEV/Desktop/configuration/config.properties

并且数据层和服务层的属性文件为空

但是当我运行API时,我收到以下错误

***************************
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

任何人都可以帮我解决这个错误。

提前致谢。

java spring-boot spring-cloud spring-cloud-config
2个回答
0
投票

这不是从您的API模块完成的。您将配置服务器属性添加到“客户端”(从配置的角度来看)应用程序。

如果您想使用Spring Cloud Config配置项目,您应该有单独的应用程序来管理您的配置。我们称之为config-server。 (您应该正确配置maven或gradle依赖项,请参阅文档)要在nativeconfig-server中配置application.properties配置文件的使用,您必须添加问题中提到的属性(native配置文件的示例)。

spring.profiles.active=native
spring.cloud.config.server.native.searchLocation=file:<path-to-the-directory-with-conf-files> or classpath:/<path-to-the-directory-with-conf-files>

注意:config-server可以处理许多服务的配置。更多信息可以在文档Spring Cloud Config Server section中找到。

然后在您的API(或任何其他模块)中,这是一个Spring启动应用程序,您应该添加spring-cloud-config-client依赖项并添加bootstrap.properties(或.yml)配置文件。你应该添加描述与config-server通信的属性。默认情况下,config-server侦听端口8888。

spring.application.name=<your app name>    
spring.cloud.config.uri=http://localhost:8888 # this is also default value for this property

在启动时,它将通过http发送到config-server并根据服务名称(spring.application.name)获取配置属性。更多信息可以在Spring Cloud Config client section找到

重要提示:确保在配置目录中正确组织文件(由native使用的config-server配置文件),找到一些示例。属性文件命名很重要。首先,您可以尝试使用your-application-name.properties


0
投票

您必须有文件:前缀到属性文件位置。

来自https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html的文档

Config Server中还有一个“本机”配置文件,它不使用Git,但从本地类路径或文件系统加载配置文件(使用spring.cloud.config.server.native.searchLocations指向任何静态URL) )。要使用本机配置文件,请使用spring.profiles.active = native启动配置服务器。

  • [注意]请记住使用file:prefix作为文件资源(默认情况下没有前缀通常是classpath)。与任何Spring Boot配置一样,您可以嵌入$ {}样式的环境占位符,但请记住,Windows中的绝对路径需要额外的/(例如,file:/// $ {user.home} / config-repo)。
  • [警告] searchLocations的默认值与本地Spring Boot应用程序相同(即[classpath:/,classpath:/ config,file:./,file:./ config])。这不会将application.properties从服务器暴露给所有客户端,因为服务器中存在的任何属性源在被发送到客户端之前都会被删除。
  • [提示]文件系统后端非常适合快速入门和测试。要在生产中使用它,您需要确保文件系统可靠并在Config Server的所有实例之间共享。
© www.soinside.com 2019 - 2024. All rights reserved.