【问题标题】:Failed to configure a DataSource: 'url' attribute is not specified spring boot microservice配置数据源失败:未指定“url”属性 spring boot 微服务
【发布时间】:2020-04-26 06:43:36
【问题描述】:

我经历了一个有线的情况,

我无法从配置服务器获取数据源配置

   @SpringBootApplication
   @EnableConfigServer
   public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

下面是我的 yml,它位于我的文件系统上,命名为 demo-services-development.yml

spring:  
  datasource:
    url: jdbc:mysql://localhost:3306/genesys?useSSL=false
    username: root
    password: Durairaj90
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect

执行此操作后,我尝试从 chrome 访问我的配置服务器 http://localhost:8888/demo-services/development

它将数据源配置作为 json 返回,如下所示

{"name":"demo-services","profiles":["development"],"label":null,"version":null,"state":null,"propertySources":[{"name":"file:///C:/Users/USHA RAJESH/config-repo/demo-services-development.yml","source":{"spring.datasource.url":"jdbc:mysql://localhost:3306/genesys?useSSL=false","spring.datasource.username":"root","spring.datasource.password":"Durairaj90","spring.datasource.driver-class-name":"com.mysql.jdbc.Driver","spring.jpa.properties.hibernate.dialect":"org.hibernate.dialect.MySQL5Dialect"}}]}

现在我使用以下内容将此配置服务器配置为我的微服务 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>2.1.11.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <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>
            </plugin>
        </plugins>
    </build>

</project>

下面是我的 application.properties

spring.application.name=demo-services
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888

下面是我的主要课程

@SpringBootApplication
public class DemoServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoServiceApplication.class, args);
    }

}

在启动应用程序时,我遇到了异常

2020-01-08 22:24:05.722  INFO 7204 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-08 22:24:05.723  INFO 7204 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 13406 ms
2020-01-08 22:24:06.879 ERROR 7204 --- [  restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthIndicatorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicatorRegistry]: Factory method 'healthIndicatorRegistry' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbHealthIndicator' defined in class path resource [org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicator]: Factory method 'dbHealthIndicator' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-01-08 22:24:07.175  INFO 7204 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-01-08 22:24:07.254  WARN 7204 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2020-01-08 22:24:07.311  INFO 7204 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-08 22:24:07.322 ERROR 7204 --- [  restartedMain] 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).

请帮我摆脱这个。

【问题讨论】:

  • 我认为您缺少 mysql 驱动程序依赖 jar。

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


【解决方案1】:

当你使用 spring-cloud-server 来检索你的属性时,你需要创建一个bootstrap.properties|yml 文件。事实上,Spring 会创建第一个上下文,并在连接到云服务器后创建第二个。

所以,有一个 bootstrap.properties 文件包含:

spring.cloud.config.name=demo-services
spring.cloud.config.profile=development
spring.cloud.config.uri=http://localhost:8888

并确保您的客户端在您的 pom.xml 文件中具有以下依赖项:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

您应该会在启动时看到一些日志,例如:

2020-01-08 18:21:48.754  INFO 22132 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888

它应该可以工作!如果您想了解更多信息:https://cloud.spring.io/spring-cloud-commons/multi/multi__spring_cloud_context_application_context_services.html

在您的情况下发生的情况是您的客户端没有调用您的服务器来获取他们需要的属性。

P.S : 我不知道它是否适用于您在 application.properties 中设置的属性,请尝试并告诉我们。

【讨论】:

  • 看起来这就是答案。 JIC
猜你喜欢
  • 2019-10-20
  • 2023-02-07
  • 2018-12-15
  • 1970-01-01
  • 2019-08-24
  • 2021-03-18
  • 1970-01-01
  • 2023-02-21
  • 2020-10-30
相关资源
最近更新 更多