config配置中心初级篇(12)

x33g5p2x  于2021-08-23 转载在 Spring  
字(4.9k)|赞(0)|评价(0)|浏览(254)

一 前言

本篇文章将介绍什么是配置中心;配置中心使用git远程仓库实现的基本方式;使用bus自动刷新配置等;

二 配置中心介绍

由于微服务的应用过多,每个模块都有一个配置文件,当业务变多的时候,在配置文件中读取或者修改自定义的属性就非常不方便,springcloud官方推出了config配置中心,用于统一管理配置文件;配置中心实现的大体流场如下:

  1. 客户端从服务端获取配置文件,通常服务端需要负载均衡和高可用;
  2. 服务端从远程仓库获取配置文件

三 config-server搭建

3.1 pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3.2 application.yml

git服务器的配置如下;其中的search-paths目录可以配置多个,使用逗号隔开;

spring:
  cloud:
    config:
      server:
        git:
          # git 服务器地址
          uri: https://github.com/zszxz/spring-cloud-config.git
          # git仓库 账号
          username: zszxz
          # git仓库 密码
          password: 
          # 搜索zszxz-config目录下的所有配置
          search-paths: zszxz-config
  application:
    name: zszxz-config # 应用名称
server:
  port: 9000 # 暴露端口

3.1 启动类

其中@EnableConfigServer表示开启config配置

/**
 * @Author lsc
 * <p> springloud  server 配置 </p>
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigApp {

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

3.4 git服务器仓库配置

应用名称尽量与配置文件名称一致;

  1. 在服务器上创建仓库spring-cloud-config
  2. 在仓库下创建文件夹zszxz-config
  3. 在zszxz-config文件夹下创建文件zszxz-config-dev.yml, 内容为 zszxz.springcloud.config: from dev
  4. 在zszxz-config文件夹下创建文件zszxz-config-pro.yml, 内容为 zszxz.springcloud.config: from pro
  5. 在zszxz-config文件夹下创建文件zszxz-config-test.yml, 内容为 zszxz.springcloud.config: from test

3.5 映射关系说明

启动项目后可以按照如下方式访问配置信息,当然控制台窗口也有打印下面信息;

  1. / {application} / {profile} [ / {label} ]
  2. / {application} - {profile} . yml
  3. / {label} / {application} - {profile} . yml
  4. / {application} - {profile} . properties
  5. / {label} / {application} - {profile} . properties

浏览器访问地址 http://localhost:9000/zszxz-config/dev/master

内容如下

{
	"name": "zszxz-config",
	"profiles": ["dev"],
	"label": "master",
	"version": "63cd882a449b97b89123595d70c852afd5e31f01",
	"state": null,
	"propertySources": [{
		"name": "https://github.com/zszxz/spring-cloud-config.git/zszxz-config/zszxz-config-dev.yml",
		"source": {
			"zszxz.springcloud.config": "from dev"
		}
	}]
}

其中 label 表示 分支;application表示应用名称;profile 表示配置文件版本;

其中控制台打印信息file:/C:/Users/林/AppData/Local/Temp/config-repo-7195875990564313473/zszxz-config/zszxz-config-dev.yml 可知会缓存临时的配置文件

四 config-client搭建

搭建client的目的就是从server端获取想要的配置文件

4.1 pom.xml

   <dependencies>
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

4.2 配置文件

bootstrap.yml 配置如下

# cloud属性相关配置
spring:
  cloud:
    config:
      # 远程分支
      label: master
      # server地址
      uri: http://localhost:9000
      # 请求的远程文件名称
      name: zszxz-config
      # 激活的配置文件
      profile: dev

application.yml配置如下

server:
  port: 9001
spring:
  application:
    name: config-client

有关springcloud的配置文件必须配置在bootstrap.yml中,bootstrap.yml 会优先于application.yml启动从config-server中获取到配置;

4.3 controller

controller定义一个字段value,采用 @Value注解获取从server端传送过来的配置内容;使用浏览器的方式更方便于测试;

@RestController
public class ConfigController {

    @Value("${zszxz.springcloud.config}")
    private String value;

    @GetMapping("/zszxz/config")
    public String getConfig(){
        System.out.println(value);
        return value;
    }
}

4.4 启动类

@SpringBootApplication
public class ConfigClientApp {

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

启动工程后访问http://localhost:9001/zszxz/config 会输出from dev

五 springcloud bus 自动刷新配置

以上的内容学习完就已经可以使用springcloud的配置中心功能,但缺点是每次在git远程仓库修改配置文件都需要重启应用,这很不方便,官方推出了使用消息中间件的方式进行自动刷新配置,相当于项目中的热部署;以下是对上面的工程的一个改造,使用的是actuator的方式。

5.1 自动刷新流程介绍

  1. 修改了git远程仓库配置文件内容
  2. server端会收到一个仓库变动内容,通过消息总线bus发送给客户端;
  3. 客户端接收到消息后会向服务端拉取最新配置内容

5.3 pom.xml

在 原有的工程的client端和server端都加上如下配置

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

5.4 application.yml

在client暴露端点;

management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"

在server端需要连接rabbitmq,故在原有即配置上加上rabbitmq配置即可

spring:
  cloud:
    config:
      server:
        git:
          # git 服务器地址
          uri: https://github.com/zszxz/spring-cloud-config.git
          # git仓库 账号
          username: 
          # git仓库 密码
          password: 
          # 搜索zszxz-config目录下的所有配置
          search-paths: zszxz-config
  application:
    name: zszxz-config # 应用名称
  ## 配置rabbitMQ 信息
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: zszxz
    password: 
server:
  port: 9000 # 暴露端口



5.5 controller

在client端需要加上@RefreshScope注解用于端点刷新

@RestController
@RefreshScope
public class ConfigController {


    @Value("${zszxz.springcloud.config}")
    private String value;

    @GetMapping("/zszxz/config")
    public String getConfig(){
        return value;
    }
}

5.5 测试说明

首先先修改git远程仓库配置文件内容,比如from dev 修改 为 from dev the author

其次发送请求post 请求至客户端 http://localhost:9001/actuator/refresh 会的到如下结果

[
    "zszxz.springcloud.config",
    "config.client.version"
]

最后重新访问 localhost:9001/zszxz/config 会得到修改后的配置

from dev the author

相关文章