SpringCloud——中级(三)Config分布式配置中心

SpringCloud Config分布式配置中心

一、概述

1、分布式系统面临的配置问题

微服务意味着将单体应用中的业务拆分成一个个子业务,每个服务的力度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
SpringCloud提供了ConfigServer来解决这个问题,我们每一个 微服务都带着一个application.yml,上百个配置文件的管理。。。

2、是什么

在这里插入图片描述
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

怎么玩

Spring Cloud Config分为服务端和客户端两部分。

服务端也称为分布式配置中心,它是个独立的微服务应用,用来连接配置服务器井为客户端提供获取記置信息,加密/解密信息等访问接口。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容

3、能干嘛

1、集中管理配置文件
2、不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
3、运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
4、当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
5、将配置信息以REST接口的形式暴露(Post、crul访问刷新均可)

4、与GitHub整合配置

由于SpringCloud Config默认使用Git来存储配置文件(也有其他方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式

5、官网

官网地址

二、Config服务端配置与测试

1、用自己的账号在GitHub上新建一个名为springcloud-config的新Repository

2、由上一步获得新建的git地址

3、本地硬盘目录上新建git仓库clone

4、此时在本地D盘符下

5、新建Module模块cloud-config-center-3344它即为Cloud的配置中心模模块cloudConfig Center

6、pom

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

7、yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center #注册进Eureka服务器的微服务名
  config:
    config:
      server:
        git:
          uri: git@github.com:18735171447/springcloud-config.git #GitHub上面的git仓库名字
          #搜索目录
          search-path:
            - springcloud-config
      #读取分支
      label: meater

#服务器注册到eureka的地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

8、主启动

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class,args);
    }
}

10、Window下修改hosts文件增加映射

127.0.0.1  config-3344.com

11、测试通过Config微服务是否可以从GitHub上获取配置内容

启动微服务3344
http://config-3344.com:3344/master/config-dev.yml
遇到的坑(404):参考这篇文章

12、配置读取规则

label:分支(branch)
name:服务名
profiles:环境(dev/test/prod)

1) /{label}/{application}-{profile}.yml

http://config-3344.com:3344/master/config-dev.yml

2) /{application}-{profile}.yml(默认访问master分支下的)

http://config-3344.com:3344/config-dev.yml

3) /{application}/{profile}[/{label}](出json串)

http://config-3344.com:3344/config/dev/master

13、成功实现了用Spring Cloud Config通过GitHub获取配置信息

三、Config客户端配置与测试

1、新建cloud-config-client-3355

2、pom

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

3、bootstrap.yml

1)是什么

application.yml是雍虎吉的资源配置项
bootstrap.yml是系统级的,优先级更高
Spring Cloud会创建一个" Bootstrap Context",作为 Spring应用的 Application Context的父上下文。初始化的时候, BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的 Environment。

Bootstrap属性有高优先级,默认情況下,它们不会被本地配置覆盖。 Bootstrap context和 Application Context有着不同的约定所以新増了一个 bootstrap.ym文件,保证 Bootstrap Context和 Application Context配置的分离。

要将 Client模下的 application.yml文件改为 bootstrap.ym这是很关键的因为 bootstrap .yml是t比 application.yml先加载的。 bootstrap.yml优先级高于 application.yml

server:
  port: 3355

spring:
  application:
    name: Config-client
  cloud:
    #Config客户端配置
    config:
      label: meater #分支配置
      name: config #配置文件名称
      profile: dev #读取后缀名称,等同于读取:http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

4、修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version

5、主启动类

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class,args);
    }
}

6、业务类

package com.atguigu.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String  getConfigInfo(){
        return configInfo;
    }
}

7、测试

http://localhost:3355/configInfo

8、成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息

9、分布式配置的动态刷新问题

1、运维修改GitHub上的配置文件内容
2、刷新3344,发现ConfigServer配置中心立刻响应
3、刷新3355,发现ConfigClient客户端没有任何响应
4、3355没有变化,除非重启或者重新加载

四、Config客户端之动态刷新

避免每次更新配置都要重启客户端微服务3355

1、动态刷新

1)修改3355模块
2)pom引入actuator监控
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
3)修改YML,暴露监控端口

新加:

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
4)@RefreshScope业务类Controller修改

在Controller类上新加注解:@RefreshScope 实现刷新功能

5)此时修改github文件
6)How

需要运维人员发送post请求刷新3355

curl -X POST "http://localhost:3355/actuator/refresh"
7)测试

在没有重启3355的情况下,再次访问http://localhost:3355/configInfo
测试通过

2、想想还有什么问题

假设有多个微服务客户端3355/3366/3377。。。。
每个微服务都要执行一次post请求,手动刷新太麻烦了。
广播通知,精确通知————消息总线(Bus)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值