Config配置中心

在分布式系统中对配置文件的集中管理的必要的,SpringCloud config就可以实现配置管理。Config分为Server端和Client端的,Server端负责管理配置,Client端用来加载配置。我们每一个为服务都要集成一个Client端的。

Config支持配置文件放在远程Git仓库里,当我们进行更改配置的时候,只需要在本地更改,然后推送到Git仓库中就可以了,所有的客户端都可以去配置中心获取配置,当配置文件修改时(本地推送到git),会触发git的webhook回调,最终触发spring cloud bus(消息总线),然后由消息总线通知相关的应用,这样就是实现配置统一管理。

我们进行配置管理,首先需要一个仓库,我们现在git建立一个中心仓库

新建一个config-demo目录,一般都是一个服务的配置放在一个文件夹中
在这里插入图片描述
config-demo里面有两个文件,文件命名是有讲究,到下面就知道了
在这里插入图片描述
两个文件的内容
在这里插入图片描述
在这里插入图片描述

创建配置中心模块:
pom文件

<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>

application.yml中配置:

server:
  port: 8088
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri:  写自己的git路径
          username:  用户名
          password:  密码
          #要搜索的路径,填我们刚刚创建的文件夹
          search-paths: config-demo
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/

启动类加上@EnableConfigServer,声明是一个配置中心

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {

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

接着我们启动程序,访问的时候只需要在后面加上文件名就可以了

在这里插入图片描述

创建两个配置中心客户端(端口:一个是8089,一个是8090):
pom文件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</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-actuator</artifactId>
</dependency>

将application.yml改名成bootstrap.yml。
这里看出来文件的命名方式没?
cloud-config和服务名对应,test和要加载的分支对应

#加载git中的文件cloud-config-test.properties

server:
  port: 8089
spring:
  application:
    name: cloud-config
  cloud:
    config:
      #加载test分支
      profile: test
      discovery:
        enabled: true
        #配置中心
        service-id: config-server

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/

some:
  name: ${name} #用来加载配置中心中 我们配置的属性
  age: ${age}

这里介绍一下这两个文件的区别
bootstrap.yml比application.yml优先级高,优先加载。bootstrap.yml 是被一个父级的 Spring ApplicationContext 加载的。这个父级的 Spring ApplicationContext是先加载的,在加载application.yml 的 ApplicationContext之前。

为什么要改成bootstrap文件呢?
因为一般配置文件都在git中放着,而config client需要取得git的信息,所以需要优先加载。必须先去访问git,从git中获取配置信息,不能直接获取配置信息。比如说你有两个application文件,一个去连接配置中心,一个去加载配置中心的属性,结果加载属性的先加载了,这个时候你还没连接呢上哪加载去?

如果换成application文件,那么会启动报错,显示找不到参数
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘age’ in value “${age}”

启动类加上@EnableEurekaClient,所有服务都要到服务中心注册。

controller

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

    @Value("${some.age}")
    private String age;

    @Value("${some.name}")
    private String name;

    @RequestMapping("/run")
    public String run(){
        return name+"---"+age;
    }
}

访问结果
在这里插入图片描述

那么如果修改了git仓库的配置文件会怎样?
config server端会更新配置信息,而config client端却不会跟新配置信息。
这里就不演示结果了。

怎么样做到让客户端也更新呢?
在bootstrap中加入

management:
  endpoints:
    web:
      exposure:
        #暴露了refresh接口
        include: refresh

接着使用postman访问,使用post方法发送请求
http://localhost:8089/actuator/refresh
这里使用的是spring boot 2.1.3版本,和1.x版本不太一样。
在这里插入图片描述
我们看到这个消息就说明age属性更新成功。
接着访问客户端(8089)会发现age属性更新成功。但是访问8090客户端会发现还是没有更改,因为我们访问8089/actuator/refresh只是更新了8089的客户端,那要是我们的服务多得话,每个服务都要调用一遍不是要累死?这个时候我们就要想到发布-订阅模型,让所有服务订阅一个事件(配置更改),然后统一去更新,spring cloud bus就可以帮我们解决这个问题。

到目前为止,我们的工程目录结构:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ROAOR1/article/details/88305372