您当前的位置: 首页 >  spring

星夜孤帆

暂无认证

  • 2浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringCloud Config分布式配置中心

星夜孤帆 发布时间:2020-06-18 23:02:28 ,浏览量:2

一、概述 1.1 分布式系统面临的配置问题

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

SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理...

1.2 SpringCloud Config是什么

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

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

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

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

1.3 能干嘛
  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息。
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露
1.4 与Github整合配置

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

二、Config服务端配置与测试 2.1 新建Git仓库 2.1.1 在Github Or 码云上新建一个名为springcloud-config的新Repository

2.1.2 新建三个文件

2.1.3 Clone到本地

 

2.2 新建Module模块cloud-config-center3344

它即是Cloud的配置中心模块cloudConfig Center

2.2.1 POM


    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-center-3344

    


        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    




2.2.2 YML
server:
  port: 3344
spring:
  application:
    name: cloud-config-center # 注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri:  https://gitee.com/jakhyd/springcloud-config.git # 填写你自己的github路径
          # 搜索目录
          search-paths:
            - springcloud-config # 仓库名称
      label: master  # 分支名称
# 服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

2.2.3 主启动类
package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * @author by XXX
 * @descriptaion #
 * @date 2020/6/18
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class, args);
    }
}
2.2.4 windows下修改hosts文件,增加映射

C:\Windows\System32\drivers\etc,hosts文件加上127.0.0.1 config-3344.com

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

启动微服务7001,微服务3344

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

成功获取到下面内容

2.2.6 配置读取规则

官网

/{label}/{application}-{profile}.yml(最推荐使用这种方式)

master分支

  • http://config-3344.com:3344/master/config-dev.yml
  • http://config-3344.com:3344/master/config-test.yml
  • http://config-3344.com:3344/master/config-prod.yml

dev分支

  • http://config-3344.com:3344/dev/config-dev.yml
  • http://config-3344.com:3344/dev/config-test.yml
  • http://config-3344.com:3344/dev/config-prod.yml

/{application}-{profile}.yml

  • http://config-3344.com:3344/config-dev.yml
  • http://config-3344.com:3344/config-test.yml
  • http://config-3344.com:3344/config-prod.yml
  • http://config-3344.com:3344/config-xxxx.yml(不存在的配置) 报错

/{application}-{profile}[/{label}]

  • http://config-3344.com:3344/config/dev/master
  • http://config-3344.com:3344/config/test/master
  • http://config-3344.com:3344/config/prod/master

重要配置细节总结

/{name}-{profiles}.yml

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

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

成功实现了用SpringCloud Config通过GitHub获取配置信息

三、Config客户端配置与测试 3.1 新建cloud-config-client-3355

3.2 POM


    
        com.atguigu.springcloud
        cloud2020
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-client-3355

    

        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    



3.3 bootstrap.yml 3.3.1 是什么

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

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

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

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

server:
  port: 3355

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

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

3.3.2 说明

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

3.4 主启动
package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientMain3355 {
    public static void main(String[] args) {
            SpringApplication.run( ConfigClientMain3355.class,args);
        }
}
 
 
3.5 业务类
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;
    }
}
 
 
3.6 测试 3.6.1 启动Config配置中心3344微服务并自测

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

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

3.6.2 启动3355作为Client准备访问

http://localhost:3355/configInfo

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

3.6.3 问题随之而来,分布式配置的动态刷新

Linux运维修改Github上的配置文件内容做调整。

刷新服务端3344,发现ConfigServer配置中心立刻响应

刷新客户端3355,发现ConfigServer客户端没有任何响应

3355没有变化除非自己重启或者重新加载,难道每次运维修改配置文件,客户端都需要重启?噩梦。

四、Config客户端之动态刷新

为避免每次更新配置都要重启客户端微服务3355,修改3355模块

4.1 动态刷新 4.1.1 POM引入actuator监控

    org.springframework.boot
    spring-boot-starter-actuator

 
 
4.1.2 修改YML,暴露监控端口
server:
  port: 3355

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

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

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
4.1.3 @RefreshScope业务类Controller修改
package com.atguigu.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author by XXX
 * @descriptaion #
 * @date 2020/6/19
 */
@RefreshScope
@RestController
public class ConfigClientController {

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

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

此时修改github--3344-->3355,访问http://localhost:3355/configInfo,发现还是没有改变

4.1.4 怎么办

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

必须是Post请求

curl -X POST "http://localhost:3355/actuator/refresh"

此时再次尝试访问,成功实现了客户端3355刷新到最新配置内容,避免了服务的重启

4.1.5 还有什么问题

假如有多个微服务客户端3355/3366/3377.....

每个微服务都要执行一次post请求,手动刷新?

可否广播,一次通知,处处生效?

如何大范围的自动刷新,下节再续。

 

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.1413s