【问题标题】:Spring boot is not able to get configuration file from my file systemSpring Boot 无法从我的文件系统中获取配置文件
【发布时间】:2019-02-21 11:27:15
【问题描述】:

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

我的项目有 3 个模块

  • API
  • 数据层
  • 服务层

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

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

DataLayer和ServiceLayer的属性文件为空

但是当我运行 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

谁能帮我解决这个错误。

提前致谢。

【问题讨论】:

  • 你能分享一下属性文件的 spring.datasource.url 部分吗(当然没有任何敏感信息)?好像不见了。还要仔细检查您的 Spring Data 依赖项。

标签: java spring-boot spring-cloud spring-cloud-config


【解决方案1】:

您必须将 file: 作为属性文件位置的前缀。

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

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

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

【讨论】:

    【解决方案2】:

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

    如果你想使用Spring Cloud Config 来配置你的项目,你应该有一个单独的应用程序来管理你的配置。我们称之为config-server。 (您应该正确配置 maven 或 gradle 依赖项,请参阅文档)要在 config-server 中配置 native 配置文件的使用 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中找到。

    然后在作为 Spring Boot 应用程序的 API(或任何其他模块)中,您应该添加 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

    【讨论】:

    • 我试过你说的,现在我在运行 API 项目时得到了这个。从服务器获取配置:localhost:8888 定位环境:name=Config-Server,profiles=[default],label=null,version=null,state=null 定位属性源:CompositePropertySource {name='configService',propertySources=[ ]} 没有活动配置文件集,回退到默认配置文件:默认并得到与我在问题中提到的相同的错误,我是否必须在 config-server 项目中添加对 JPA 和 MYSQL 的依赖项?
    • 你不应该将它添加到配置服务器,因为你没有使用它们。其他项目也一样。 Spring Boot 有自动配置的概念。如果你有特定的依赖,spring boot 会尝试初始化它们,如果你没有配置,则会失败。
    猜你喜欢
    • 1970-01-01
    • 2019-02-22
    • 2023-01-10
    • 2018-03-05
    • 1970-01-01
    • 2016-08-22
    • 2019-01-25
    • 1970-01-01
    • 2019-05-19
    相关资源
    最近更新 更多