SpringCloud-Config

SpringCloud Config

出现之前的问题:

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

简介:

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

image-20200909163303991

作用:

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

demo

创建模块,添加相关依赖,修改配置文件。

  <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <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>
server:
  port: 3344

spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/15933759214/zzyybs-springcloud-config.git #GitHub上面的git仓库名字
          search-paths:
            - springcloud-config
          ####读取分支
      label: master
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

之后,启动Eureka7001,然后启动config3344,访问http://127.0.0.1:3344/master/config-dev.yml,可以直接得到https://github.com/15933759214/zzyybs-springcloud-config.git仓库下的config-dev.yml中的内容。

image-20200909211216354

image-20200909211231586

配置读取规则

第一种:

/{lable}/{application}-{profile}.yml

例如:

  • http://127.0.0.1:3344/master/config-dev.yml
  • http://localhost:3344/master/config-test.yml
  • http://localhost:3344/dev/config-test.yml

dev、master这些分支对应的是lable,application对应的是config,-后面的是profile。

/{application}-{profile}.yml;如果省略lable,默认分支就是master。

/{application}/{profile}[/{lable}]:这种访问格式的分支在后面,应用名和profile在前面。返回文件中数据格式是json。

{"name":"config",
 "profiles":["dev"],"label":"master","version":"37c68154de79ccc5cd558b24c7724fc7a74fb5a7","state":null,"propertySources":[{"name":"https://github.com/15933759214/zzyybs-springcloud-config.git/config-dev.yml","source":{"config.info":"master branch,springcloud-config/config-dev.yml version=7"}}]}

如果访问的路径中没有该文件就是一个空字符串{}或者json串。

{"name":"config","profiles":["pro"],"label":"master","version":"37c68154de79ccc5cd558b24c7724fc7a74fb5a7","state":null,"propertySources":[]}

动态刷新配置

当运维人员从远程长裤更新配置时,和本地仓库相连的配置服务器config server3344可以刷新,直接获取更新的配置,但是和配置服务器相连的客户端无法及时更新成最新的配置文件(每次都要重启服务才能获取最新的),那要如何改变这种情景呢?

在client config中导入actuator

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

bootstrap.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 #配置中心地址k
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

添加暴露断点配置

在主启动类上添加@RefreshScope注解。

完成之后,启动Eureka,启动config-server3344服务,启动config-client3355服务,在命令行窗口或者postman中向3355发送post请求,才能更新最近的配置文件。

curl "http://localhost:3355/actuator/refresh"

原文地址:https://www.cnblogs.com/dataoblogs/p/14121869.html