微服务—SpringConfig配置中心

前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


一、SpringConfig概念介绍

在实际操作中会出现多个微服务操作同一个数据库的情况,那么对于每个微服务都需要重复配置数据库信息,而且当数据库信息改动时也难以维护。因此一套集中的、动态的配置管理设施是必不可少的。

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。SpringCloud Config 具体分为客户端和服务端两部分。

具体作用:

  • 集中管理配置文件;
  • 不同运行环境不同的配置,动态化地进行配置更新,分环境部署(dev/test/prod/release)
  • 运行期间动态调整配置,不需要在每个服务部署的机器上编写配置文件,配置变动各个微服务能够自动拉取信息

二、配置中心服务器端搭建

配置中心服务端

  1. SpringCloud Server使用Git 作为远程的配置文件仓库存储地址,我们需要先在远程建立好仓库
    在这里插入图片描述
  2. 修改pom.xml、application.yml
        <!-- SpringConfig Server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
server:
  port: 3344
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:         #配置远程仓库的地址
          uri: https://gitee.com/loserii/my_-spring-cloud_config.git
          password: xjxhhxjava2048
          username: loserii
          search-paths: xxxxx     #git仓库下相对搜索地址
      label: master  #配置远程分支名
eureka:
  instance:
    instance-id: configServer
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

配置完成后启动服务,本地通过HTTP请求访问远程仓库上的配置文件时遵循以下格式的请求资源:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

{label} 作为远程仓库的分支;
{application}-{profile} 作为远程仓库上的配置文件名,比如config-test.yml

在这里插入图片描述
3. 此外还可以配置多个存储库,配置相应的匹配规则来读取文件

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

pattern表示模式匹配,是带有通配符{application}/{profile}名称的逗号分隔列表。如果pattern不匹配任何模式,那么它就会使用spring.cloud.config.server.git.uri下的默认uri。

三、配置中心客户端搭建

3.1 配置同步机制介绍

客户端通过SpringCloud Config Server来获取到远程的Config环境配置,但是如果想要自己定义一些额外的配置,就需要另外创建一个配置文件。

客户端项目中包含两种配置文件:application.yml(用户级的资源配置项)、bootstrap.yml(系统级的资源配置项,优先级更高)

SpringCloud 会创建一个 Bootstrap Context上下文,该上下文作为 Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并且解析配置。这两个上下文共享一个从外部获取的Environment

什么是Environment?
Environment用于存储服务器的配置数据,管理此行为的是EnvironmentRepository。Environment是Spring Environment(包括PropertySources作为主要功能)的域的浅层副本。Environment资源由三个变量参数化:

  • {application}映射到客户端的spring.application.name
  • {profile}映射到客户端上的 spring.profiles.active
  • {label}标记版本这一组件;

3.2 详细配置

客户端从配置服务中心获取配置。

  • 修改 pom.xml、bootstrap.yml
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
spring:
  application:
    name: ConfigCenterClient
  cloud:
    config:
      fail-fast: false     #客户端连接失败时开启重试,需要结合spring-retry、spring-aop
      label: master       #获取配置文件的分支名
      name: config        #获取的文件名
      profile: test       #文件后缀
      uri: http://localhost:3344  #配置中心服务端地址

eureka:
  instance:
    instance-id: config-client
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka
server:
  port: 3355

注意与eureka、cloud相关的配置需要放在bootstrap.yml文件中,程序启动时会先读取bootstrap.yml然后再读取application.yml

  • 测试配置读取
@RestController
@RequestMapping("/test")
public class TestController {
    @Value("${config.info}")
    private String info;

    @GetMapping("/testInfo")
    public String test(){
        return info;
    }
}

在这里插入图片描述

3.3 分布式下的问题

测试时发现当在远程仓库修改配置文件数据后,本地的SpringCloud Config Server能够在不重启的情况下接收到新配置,但是本机的SpringCloud Config Client就不能直接获取,必须进行重启。这在微服务很多时是不方便处理的。

解决方法: 客户端向外部暴露监控的端点,结合@RefreshScope注解来解决

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值