SpringCloud Config分布式配置中心

一、Config分布式配置中心介绍

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因为系统中会出现大量的服务。由于每个服务都需要的必要的配置信息才能运行,所以一套集中式、动态的配置管理设施是必不可少的。
SpringCloud提供了ConfigServer来解决这个问题。
官网地址:点击访问
1、是什么
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

2、怎么做
SpringCloud Config分为服务端和客户端两部分
服务端也称为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器,默认采用Git来存储配置信息,这样有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

3、能干嘛

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

二、Config配置总控中心(服务端)搭建

1、创建子模块cloud-config-center-3344
2、编写pom

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!-- web -->
    <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>
    <!-- eureka-client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- 常规jar -->
    <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>

3、编写application.yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center   #诸注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/chenguangwei123/springcloud-config.git   #GitHub上面的git仓库名字
          ###搜索目录
          search-paths:
            -springcloud-config
      ###读取分支
      label: master
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

4、编写启动类

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

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

5、测试
启动eureka7001和config3344子模块,访问http://config-3344.com:3344/master/config-dev.yml在这里插入图片描述
在这里插入图片描述
内容一致,配置成功!至此实现了用SpringCloud Config通过GitHub获取配置信息

6、GitHub上被读取配置文件命名规则
在这里插入图片描述
(1)/{label}/{application}-{profile}.yml
在这里插入图片描述
(2)/{application}-{profile}.yml
在这里插入图片描述
(3)/ {application} / {profile} [/ {label}]
在这里插入图片描述

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

三、Config客户端配置与测试

1、创建子模块cloud-config-client-3355
2、编写pom

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- web -->
        <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>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 常规jar -->
        <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>

3、编写bootstrap.yml
1、bootstrap.yml是什么’?

  • application.yml是用户级的资源配置项;bootstrap.yml是系统的,优先级更高
  • Spring Cloud会创建一个“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先加载
server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    #Config客户端配制
	config:
	  label: master   #分支名称
	  name: config  #配置文件名称
      profile: dev  #读取后缀名称=>master分支上config-dev.yml
	  uri: http://localhost:3344    #配置中心地址
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

4、编写主启动类

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);
    }
}

5、业务类测试

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("/config")
    public String getConfigInfo(){
        return configInfo;
    }
}

访问http://localhost:3355/config,成功访问到GitHub上配置文件中内容:在这里插入图片描述
6、分布式配置动态刷新问题
(1)当前存在的问题
当我们修改GitHub上配置文件内容之后:
在这里插入图片描述
在这里插入图片描述
刷新3344,发现ConfigServer配置中心立刻响应:l
刷新3355,发现ConfigClient客户端没有任何响应(除非重启或重新加载):在这里插入图片描述
在这里插入图片描述
存在的问题:每次改动GitHub远程资源配置文件,客户端都需要重启才能获得修改后的配置?

(2)Config动态刷新之手动版
A、客户端需要引入监控依赖

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

B、修改yml,暴露监控端口

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

C、再业务类上面添加注解@RefreshScope实现配置和实例刷新
在这里插入图片描述
D、再向3355发送一个用于刷新的Post请求:curl -X POST "http://localhost:3355/actuator/refresh"
在这里插入图片描述
这样就可以避免重启而使3355能够获得修改后的配置!
仍然存在问题

  • 假如有多个微服务客户端3355/336/3377…,则需要发n多次Post请求刷新,很麻烦
  • 能不能通过广播,一次通知,处处生效?
  • 有没有大范围自动刷新的方法?
    应运而生的解决方法就是运用SpringCloud消息总线。请参考消息总线SpringCloud Bus篇!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值