springcloud(十)--SpringCloud Config分布式配置中心

一、概述

官网: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

在这里插入图片描述
分布式系统面临的–配置问题:
微服务意味着要将单体应用中的业务拆分成一个一个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

SpringCloud提供了Config Server来解决这个问题。我们每一个微服务自己带着一个applcation.yml。

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

SpringCloud Config分为服务端和客户端两部分

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

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

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

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

二、Config服务端配置与测试

1、在自己GitHub上新建一个用作配置中心的新仓库,并克隆到本地开发硬盘目录。
在这里插入图片描述
2、 新建Module模块:cloud-config-center-3344作为配置中心微服务
config依赖:

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

pom文件

<dependencies>
        <!--config server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 引入自定义的API通用包 实体类 -->
        <dependency>
            <groupId>com.zzp.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般基础通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

yml配置文件:

server:
  port: 3344

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

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

启动类,添加注解:@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class);
    }

}

修改本地 hosts文件,增加映射,使本机模拟网址服务配置中心网址:

127.0.0.1 config-3344.com

启动7001,3344服务
测试通过服务配置中心微服务3344是否可以从GitHub上获取配置内容
在GitHub查看config-dev.yml内容
在这里插入图片描述

访问: http://config-3344.com:3344/main/config-dev.yml
在这里插入图片描述
与GitHub上的config-dev.yml内容一致

配置取规则:参考官网
在这里插入图片描述

/main/config-dev.yml
/{label}/{application}-{profile}.yml
/{application}/{profile}/{label}.yml

访问: http://config-3344.com:3344/config/dev/main
在这里插入图片描述
返回json字符串

三、Config客户端配置与测试

1、新建Module模块:cloud-config-client-3355作为访问配置中心的客户端
client config依赖:

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

pom文件:

<dependencies>
        <!--config client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 引入自定义的API通用包 实体类 -->
        <dependency>
            <groupId>com.zzp.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般基础通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

编写配置文件bootstrap.yml

  • application.yml是用户级的资源配置项
  • bootstrap.yml是系统级的资源配置项,bootstrap.yml的优先级更高

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

”Bootstrap“属性有高优先级,默认情况系,它们不会被本地配置覆盖。"Bootstrap Context"和"Application Context"有着不同的约定,所以新增一个"bootstrap.yml"文件,保证"Bootstrap Context"和"Application Context"配置分离。

编写bootstrap配置文件如下:

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: main   #分支名称
      name: config  #配置文件名称
      profile: dev  #读取后缀名称   上述3个综合:main分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/main/config-dev.yml
      uri: http://localhost:3344  #配置中心地址

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

启动类:

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {

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

控制类:

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

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

@Value("${config.info}") 注解对应GitHub的config-dev.yml文件的内容
在这里插入图片描述

2、测试客户端是否能够通过访问配置中心获取配置信息
启动3355服务测试:
访问:http://localhost:3355/configInfo
在这里插入图片描述3、分布式存在的刷新的问题:

在GitHub上修改配置文件内容,刷新3344配置中心服务端,发现Config Server配置中心立刻响应并刷新了配置信息,但是!!我们刷新3355客户端Config Client,发现没有任何响应,配置信息仍然是原来的配置信息。

在这里插入图片描述
客户端都需要重启来进行对配置信息的重新加载(重启3355服务)
在这里插入图片描述

四、Config客户端之动态刷新

避免每次更新配置都要重启客户端微服务3355
1、修改3355模块
pom文件依赖添加:

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2、 修改配置文件,暴露监控端口:添加

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3、ConfigClientController添加@RefreshScope注解

# 暴露监控端点
@RestController
@RefreshScope
public class ConfigClientController {...}

重启测试:

修改 gitHub >> 3344 >> 3355
在这里插入图片描述
再修改 gitHub >> 3344 >> 3355
在这里插入图片描述
3344配置没有生效?
这里需要发送POST刷新3355
curl -X POST “http://127.0.0.1:3355/actuator/refresh”
在这里插入图片描述
再访问:http://localhost:3355/configInfo
在这里插入图片描述
不重启,GitHub 再修改 然后发送post
在这里插入图片描述

但是:

  • 但是假设如果我们有多个微服务客户端呢?
  • 每个微服务都需要执行一次post请求,手动刷新?
  • 可否广播,一次通知,处处生效?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值