config客户端在SpringBoot中不工作

kmynzznz  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(403)

配置客户端在spring boot中不工作
我在springboot中尝试配置服务器客户端时遇到了这个奇怪的问题。
我已经成功地启动并运行了配置服务器,但是在尝试启动配置客户机/使用者时,似乎客户机没有从配置服务器获取任何信息。
运行客户端时的输出:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

尚未包含任何配置客户端的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文件由于某些原因不能加载,但现在我感觉配置根本不能为我的客户机工作,因为在启动日志中提到了来自特定配置服务器名称的配置/link:port was 被人试图抓走。另外,请忽略我语法中的错误:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题