微服务(十二)—— 配置中心(backend-config-server)

上一篇: 微服务(十三)—— 单点登录.

1. 介绍

对于微服务而言,服务比较多,而且每个服务都需要配置文件,而且配置文件如果需要修改,就需要重新启动服务,这无疑比较麻烦。Spring Cloud 提供了Spring Cloud Config 来做配置中心。

2. 项目

2.1 创建远程仓库

首先我们可以将配置文件放在远程,Spring Cloud Config支持Git,所以可以将配置文件放在Git。由于Git访问比较慢,所以我将配置文件放在了Gitee.
在Gitee创建一个仓库,然后将配置文件都放在这个远程仓库里,backend-config-center.

2.2 创建本地项目

然后本地创建一个Spring Boot项目,引入Spring Cloud Config依赖,如下:

<!-- 引入父依赖 -->
<parent>
    <groupId>com.aiun</groupId>
    <artifactId>BackendManageSystem</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

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

因为父依赖引入了Spring cloud ,所以这里可以不用指定版本。

2.3 配置文件

在说配置文件,首先说一下application和bootstrap 的区别:
在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。

因此,对比 application 配置文件,bootstrap 配置文件具有以下几个特性:

  • boostrap 由父 ApplicationContext 加载,比 applicaton 优先加载
  • boostrap 里面的属性不能被覆盖
  • bootstrap/ application 的应用场景
  • application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置。

bootstrap 配置文件有以下几个应用场景:

  • 使用 Spring Cloud Config 配置中心时,这时需要在 bootstrap 配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;
  • 一些固定的不能被覆盖的属性
  • 一些加密/解密的场景;

来源:https://segmentfault.com/a/1190000015741797

我们这里是从远程仓库获取配置信息,所以需要使用bootstrap.yml配置文件

server:
  port: 8081

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/aiuncode/backend-config-center.git
          default-label: master
          timeout: 50
          username: 账户
          password: 密码
          basedir: F:\Study\GitTemp

主要是配置Gitee的连接信息,然后拉取的配置文件会保存在本地F:\Study\GitTemp文件夹。
然后在启动类加上@EnableConfigServer注解。
以上是配置中心的实现过程。

需要使用配置中心的服务,做如下配置:
首先是引入config client 依赖

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

然后将application.yml配置文件放在远程仓库,并且改名为backend-user.yml(非必须)
然后在项目原来的位置创建一个bootstrap.yml,配置如下:

spring:
  cloud:
    config:
      uri: http://localhost:8081    # 指定要连接的 config server
      label: master
      name: backend-user            # 指定要从 git 读取的文件
      profile: dev                  # 环境选择

主要是配置配置中心的地址,需要访问的配置文件。
这样就可以了。
如果需要使用配置中心的服务使用的是application.yml配置文件,那么启动服务会报如下错误:

2021-07-22 14:02:45.824  INFO 12840 --- [           main] com.aiun.product.ProductApplication      : The following profiles are active: dev
2021-07-22 14:02:47.293 ERROR 12840 --- [           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

意思是 如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有配置文件处于活动状态)。

下一篇: 微服务(十三)—— Swagger.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值