java - Config Client 在 Spring boot 中不工作

标签 java spring spring-boot hibernate spring-mvc

Config Client 在 Spring boot 中不工作

我在 Spring boot 中试用配置服务器客户端时遇到了这个奇怪的问题。

我已经能够成功启动并运行配置服务器,但是在尝试启动配置客户端/消费者时,客户端似乎没有从配置服务器获取任何信息。

运行客户端时的输出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2020-12-24 02:01:28.717  INFO 3000 --- [           main] c.r.service.profile.ProfileApplication   : Starting ProfileApplication using Java 1.8.0_121 on DESKTOP-RID7KR with PID 3000 (E:\Spring\rent-a-car\profile\target\classes started by Dawg in E:\Spring\rent-a-car\profile)
2020-12-24 02:01:28.727  INFO 3000 --- [           main] c.r.service.profile.ProfileApplication   : No active profile set, falling back to default profiles: default
2020-12-24 02:01:30.161  INFO 3000 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-12-24 02:01:30.263  INFO 3000 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 82 ms. Found 1 JPA repository interfaces.
2020-12-24 02:01:30.642  INFO 3000 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=99ce795d-c5e8-3f6e-87d9-3efbe096b86a
2020-12-24 02:01:32.012  INFO 3000 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-12-24 02:01:32.027  INFO 3000 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-24 02:01:32.027  INFO 3000 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2020-12-24 02:01:32.287  INFO 3000 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-24 02:01:32.287  INFO 3000 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3430 ms
2020-12-24 02:01:32.431  WARN 3000 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.autoconfigure.RefreshAutoConfiguration$JpaInvokerConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '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-12-24 02:01:32.444  INFO 3000 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-12-24 02:01:32.478  INFO 3000 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-24 02:01:32.513 ERROR 3000 --- [           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

bootstrap.yml(配置客户端)

spring:
  application:
    name: profile
  profiles:
    active: prod
  cloud:
    config:
      name: profile
      uri: http://localhost:8181

没有为 config-client 包含任何 application.yml 文件。

我的配置服务器在端口 8181 上,我能够成功地从 github 获取配置,

示例成功响应:

{
   "name":"profile",
   "profiles":[
      "prod"
   ],
   "label":null,
   "version":"24275f56bf516d847f171c9fc419ddef141bd39b",
   "state":null,
   "propertySources":[
      {
         "name":"https://github.com/vishu221b/rentaca-config-store.git/file:C:\\Users\\Vishal\\AppData\\Local\\Temp\\config-repo-2331899650646506\\service-config\\profile-service\\profile-prod.yml",
         "source":{
            "server.port":8081
         }
      },
      {
         "name":"https://github.com/vishu221b/rentaca-config-store.git/file:C:\\Users\\Vishal\\AppData\\Local\\Temp\\config-repo-2331899650646506\\application.yml",
         "source":{
            "spring.datasource.url":"jdbc:postgresql://127.0.0.1:5432/rent-a-car?createDatabaseIfNotExist=true",
            "spring.datasource.username":"cofix",
            "spring.datasource.password":"C0fiX",
            "spring.datasource.driver-class-name":"org.postgresql.Driver",
            "spring.jpa.hibernate.naming.physical-strategy":"org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl",
            "spring.jpa.properties.hibernate.dialect":"org.hibernate.dialect.PostgreSQLDialect"
         }
      }
   ]
}

我知道上面的文件中肯定有一些键/值错误,我会解决这个问题,但前提是我的配置客户端开始从我的配置服务器成功运行中获取配置。 我的配置服务器没有安全性,所以我不需要用户名和密码。

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.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.reantacar.service</groupId>
    <artifactId>profile</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>profile</name>
    <description>Profile service for Rent A Car application.</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

<!--        &lt;!&ndash;The jar needs to be imported to make the bootstrap.yml configuration file take effect &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-context</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.rentacar.commons</groupId>
            <artifactId>commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</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>
        </repository>
    </repositories>

</project>

PS: 我已经尝试通过互联网上的其他 stackoverflow 问题和官方文档对此进行了大量调试,但似乎没有任何东西可以解决我的问题。最初我认为我的 bootstrap.yml 文件不能因为某些原因加载,但现在我觉得配置根本不适合我的客户端,因为在启动日志中提到来自特定配置服务器的配置-正在尝试获取名称/链接:端口。 另外,请忽略我语法中的错误:)

最佳答案

2020.0.0 Spring Cloud Config 不支持引导文件。

For a workaround use bootstrap.{yml|properties} and add a dependency on spring-cloud-starter-bootstrap to restore the old behavior.

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

更多信息:
https://github.com/spring-cloud/spring-cloud-release/wiki/Spring-Cloud-2020.0-Release-Notes#breaking-changes

关于java - Config Client 在 Spring boot 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65430810/

相关文章:

java - 抽象类中的多重性和聚合

java - Spring:@DateTimeFormat 在嵌套对象中被忽略

java - 来自变量的 Spring 动态 HttpStatus

java.io.IOException : invalid constant type: 19 at 5 异常

java - 使用 Spring Boot 启动 tomcat 时出现 NotReadablePropertyException

java - AnyLogic 弃用方法 getJComponent() 的替代方法?

java - 上传文件和图像

java - JDBC PreparedStatement 查询

java - 使用 Spring SimpleJpaRepository 出现泛型错误 "bound dismatch"

java - Spring Boot应用程序无法启动?