Config配置中心

Config 配置中心

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的管理设施式必不可少的。Spring Cloud 提供了 Config Server 来解决这个问题,我们每个微服务自己带着一个application.yml 是十分臃肿的,且不宜于修改。Spring Cloud Config 为每个微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

作用

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

构建

git 仓库

新建 git 仓库并 clone。

创建服务

依赖坐标
<!-- config server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
配置
server:
  port: 3344

spring:
  application:
    name: config-center
  cloud:
    config:
      server:
        git:
          username: keyboardWithDream
          password: Hhn004460
          force-pull: true
          skip-ssl-validation: true
          # git 地址
          uri: https://github.com/keyboardWithDream/spring-cloud-config.git
          # 搜索目录
          search-paths:
            - spring-cloud-config
      # 读取分支
      label: main

eureka:
  instance:
    appname: config-center-3344
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url: htt://localhsot:7001/eureka,htt://localhsot:7002/eureka
启动类

使用@EnableConfigServer注解开启配置中心

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigCenterApplication3344 {

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

客户端

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

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

依赖坐标

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

配置文件

bootstrap.yml

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #config 客户端配置
    config:
      # 分支
      label: main
      # 配置文件名
      name: config
      # 文件名后缀 main/config-dev.yml
      profile: dev
      # 配置中心地址
      uri: http://localhost:3344

eureka:
  instance:
    instance-id: config-client
    prefer-ip-address: true
  client:
    fetch-registry: true
    register-with-eureka: true
    defaultZone: htt://localhsot:7001/eureka,htt://localhsot:7002/eureka

启动类

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication3355 {

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

动态刷新

客户端配置与 git 上的配置实现同步更新

客户端

依赖坐标
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置
# 暴露监控端点
management:
  endpoint:
    web:
      exposure:
        include: "*"
controller

添加注解@RefreshScope

@RestController
@RefreshScope
@RequestMapping("/config")
public class ConfigController {

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

    @GetMapping("/info")
    public String info() {
        return configInfo;
    }
}
发送刷新请求
curl -X POST "http://localhost:3355/actuator/refresh"

刷新成功:

避免了重启刷新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值