springcloud Config配置中心

本文介绍了SpringCloudConfigServer在微服务架构中的角色,如何作为分布式配置中心管理服务端和客户端的配置,以及如何通过Git实现版本管理和动态刷新配置。同时讨论了bootstrap.yml和Actuator在配置管理和刷新中的作用。
摘要由CSDN通过智能技术生成

简介

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

SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理.…
在这里插入图片描述
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

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

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

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

服务端使用:

将配置专门做成一个微服务与github连接,能够实时获取github上的配置信息。

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

application.yml


spring:
  application:
    name: cloud-config-center
  #############################config server配置###########################
  cloud:
    config:
      server:
        git:
          uri: https://github.com/q1052780722/springcloud-config.git
          search-paths:
            - springcloud-config #仓库名
          username: xxx
          password: xxx
      label: master #分支名

eureka:
  instance:
    hostname: cloud-gateway-service
  client: #服务提供者provider注册进eureka服务列表内
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://localhost:7001/eureka

主启动类:

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

git仓库所对应的文件
在这里插入图片描述

配置成功后,访问http://localhost:3344/master/config-test.yml即可读取到文件的内容。
也可以这样访问:http://localhost:3344/config-dev.yml 因为已经配置了master分支
在这里插入图片描述

客户端使用

客户端的yaml区别于服务端为:

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

创建bootstrap.yml

bootstrap.yml与application.yml的区别
applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高
Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。
初始化的时候,BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。
Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。

server:
  port: 3355

spring:
  application:
    name: config-client
  #############################新增网关配置###########################
  cloud:
    config:
      label: master #分支名称
      name: config #配置文件的名称
      profile: dev #读取后缀的名称    读取:http://localhost:3344/master/config-dev
      uri: http://localhost:3344


####################################################################

eureka:
  instance:
  client: #服务提供者provider注册进eureka服务列表内
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://localhost:7001/eureka

controller:读取来自configServer的config.info

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

访问localhost:3355/configInfo可以观察到configInfo被成功注入。
在这里插入图片描述

动态刷新

当github上的配置文件改动后,客户端需要重启才能加载新的配置,这样会相当的麻烦。
需要搭配spring-boot-starter-actuator监测中心进行使用。

  1. 在controller类上加上@RefreshScope注解
@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}
  1. 在bootstrap.yml中暴露端口
# 暴露监控端口
management:
  endpoints:
    web:
      exposure:
        include: "*"

这样配置之后,只要在github上的配置文件改变后,向config client发送post请求就能加载新的配置文件。
curl -X POST “http://localhost:3355/actuator/refresh”

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值