spring cloud config


学习Spring Cloud Config

要学习这部分的知识需要先了解配置中心的工作流程

配置中心服务器端

配置中心的依赖

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

配置中心在启动类上边的注解

@EnableConfigServer

在启动类加上注解之后,发现项目启动有问题。

If you are using the git profile, you need to set a Git URI in your configuration.  If you are using a native profile and have spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

上述问题是因为,你没有配置远程的git仓库,spring配置中心的工作流程就是:远程仓库 -> 配置中心 -> 本地系统

配置中心配置文件配置远程git仓库

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/XXX/config-repo
          username: XXX # 账户
          password: XXX # 密码
          basedir: /Users/XXX/workspace/microservice/config/basedir # 本地存放配置中心文件的位置

访问远程仓库的配置文件的命名规则。例子http://localhost:8080/config-XXX.yml。XXX为自定义的名字

/{name}-{profiles}.yml
/{label}/{name} -{profiles}. yml

name: 服务器名
profiles: 环境
label: 分支(branch)

注:访问配置仓库的配置信息不仅支持yml,还支持properties,json等配置文件格式

客户端获取配置中心的配置

首先就是引入配置中心客户端的依赖啦

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

我将我的客户端的配置文件信息(数据库账户密码等)放在远程git仓库上边。这时候要怎样才能访问到config配置中心呢。通过service-id访问到config配置中心,获取的文件为项目名-profile.yml -> (order-test.yml)。这样就能获取到仓库上边的order-test.yml文件了

spring:
  application:
    name: order
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG
      profile: test

这时候启动项目是会出问题的。

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).

提示:找不到数据库的配置。需要配置数据库信息。

疑问:数据库配置不是放置到配置中心上了吗?而且我们在项目中配置了配置中心,按理说应该能拿到数据库的配置的。为什么拿不到配置呢?

首先我们项目启动先加载的是我们的配置文件application.yml文件。而不是先去获取配置中心的信息。

解决方法:我们将配置文件的优先级提高,先通过eureka去访问config配置中心,获取到项目的配置文件之后,在执行后边的操作。

操作:将application.yml改成bootstrap.yml。修改配置文件的文件名。这时候才能启动我们的项目

注:配置中心在获取文件的时候order-test.yml的时候会默认获取一个order.yml的文件。获取到两个配置文件之后在进行二者的合并

Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/lishengqi/config-repo/order-test.yml'}, BootstrapPropertySource {name='bootstrapProperties-https://gitee.com/lishengqi/config-repo/order.yml'}]

Spring Cloud Config使用过程中碰到的坑

坑1

个人虽解决如下问题,但是不明白为何会出现这样的情况。如果哪位大佬了解,能否告知一番

spring:
  cloud:
    config:
      discovery:
        service-id: config
        enabled: true
      profile: dev
  application:
    name: product

按照bootstrap.xml配置文件中的配置,按道理来说是获取config服务下边的product-dev.xml的配置文件。但是却是显示启动错误。


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).

我仔细查看日志中的信息。ConfigServicePropertySourceLocator加载的是远程git仓库上边的product-test.xml文件。但是我的远程git仓库中的配置文件不叫这个名字,我将名字改掉后就能完好的启动项目了。

ConfigServicePropertySourceLocator : Located environment: name=product, profiles=[test], label=null, version=e2f64f3e6dac18ce0741c3f8a1088dc17ad5706f, state=null

 上一篇
Spring Cloud Bus 配置文件实现自动更新 Spring Cloud Bus 配置文件实现自动更新
上篇文章中描述了使用git仓库保存项目的配置文件,其他项目通过关联配置中心,实现统一配置中心。 在生产环境中,很少通过重启项目来改变项目的配置文件的。本篇将对Spring Cloud Bus如何实现保证项目运行的过程下,改变远程git仓库
2020-08-18
下一篇 
重构SpringBoot项目 重构SpringBoot项目
最近在学习微服务的过程中,需要将之前学到的SpringBoot项目重构。 个人在学习过程中参考 慕课网小马哥的 Spring Boot 2.0深度实践-初遇Spring Boot 这门课程是免费的。如果博客阐述不当,可以去看看小马哥的视频
2020-08-17
  目录