问题描述

将配置文件上传git,使用server端拉取后供client端使用,然而由于client端引用配置文件使用了数据库插件,启动时却报错

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

尝试解决

本地配置文件名改为bootstrap.yml(或bootstrap.properties)无效

首先大版本一定要对应好

大版本对应好,无效

client使用以下两种都无效

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

最终解决

首先本地配置文件名改为bootstrap.yml(或bootstrap.properties)

其次不适用serveice-id,而必须是用uri(使用bus有bug的看下文)

spring:
  application:
    name: order
  cloud:
    config:
      uri: http://localhost:8080
      profile: dev

其他的坑

1.

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xxx/config-repo.git
          username: 
          password: 
          basedir: D:/ideaproject/SpringCloud-Sell/basedir

server端修改git的uri后,拉取的还是之前的git,修改并未生效。必须删除basedir文件夹才可以

2. client使用uri并且discover的enabled为true而不用service-id不设置之后,会报以下的错误

No instances found of configserver (configserver)

说找不到服务端,那我就把服务端的name改为configserver呗,可改完有重新回到了最开始的错误

无语,直接把discover去掉

3.使用bus 后 server服务端可以拉取git自动刷新可client却不能

首先一定要pom添加依赖

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

原来service-id的作用是完成bus一个配置的拼接,必须使用service-id

而我又没有配置,所以必须让系统知道service-id啊

所以加个配置

bus:
  id: ${spring.application.name}:${spring.cloud.config.profile}:${random.value}

完整的

spring:
  application:
    name: order
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080
#      discovery:
#        service-id: CONFIGSERVER
#        enabled: true
    bus:
      id: ${spring.application.name}:${spring.cloud.config.profile}:${random.value}

server:
  port: 8081
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐