SpringCloud之配置中心Config(消息总线)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41402200/article/details/92077652

前言

  SpringCloud 是微服务中的翘楚,最佳的落地方案。

  Spring Cloud Config 是一个解决分布式系统的配置管理方案,它包含了 server 和 client 两个部分。

  server 用来获取远程的配置信息(默认为 Git 仓库),并且以接口的形式提供出去;

  client 根据 server 提供的接口读取配置文件,以便于初始化自己的应用。

  在这里存在一个问题,当配置中心(远程 Git 仓库等)中的配置发生了变化,server 是可以获取到最新的配置,

  但是 client 不可以(在下面有验证);

  这时,就用到了 SpringCloud 另一个组件:Spring Cloud Bus。

  Spring Cloud Bus 通过轻量消息代理连接各个分布的节点。简单的说,就是把多个应用之间通过 AMQP 消息

  代理建立联系。

  Spring Cloud Bus 在项目中的作用之一就是利用 MQ 的广播机制在分布式系统中传播消息,目前常用的有 Kafka

  和 RabbitMQ。本博客中使用 Kafka 作为消息中间件。

源码

  GitHub地址:https://github.com/intomylife/SpringCloud

环境

  • JDK 1.8.0 +
  • Maven 3.0 +
  • SpringBoot 2.0.3
  • SpringCloud Finchley.RELEASE

开发工具

  • IntelliJ IDEA

正文

commons 工程

commons 工程 - POM 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-bus-commons</artifactId>
    <version>1.0</version>

    <!-- 工程名称和描述 -->
    <name>springcloud-config-bus-commons</name>
    <description>公用工程</description>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>
        <!-- 编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- jdk -->
        <java.version>1.8</java.version>

        <!-- SpringBoot -->
        <platform-bom.version>Cairo-SR3</platform-bom.version>

        <!-- SpringCloud -->
        <spring-cloud-dependencies.version>Finchley.RELEASE</spring-cloud-dependencies.version>
    </properties>

    <!-- 加入依赖 -->
    <dependencies>
        <!-- spring cloud bus & kafka 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
    </dependencies>

    <!-- 依赖 jar 包版本管理的管理器 -->
    <!-- 如果 dependencies 里的 dependency 自己没有声明 version 元素,那么 maven 就此处来找版本声明。 -->
    <!-- 如果有,就会继承它;如果没有就会报错,告诉你没有版本信息 -->
    <!-- 优先级:如果 dependencies 里的 dependency 已经声明了版本信息,就不会生效此处的版本信息了 -->
    <dependencyManagement>
        <dependencies>
            <!-- SpringBoot -->
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>${platform-bom.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringCloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 配置一些共用依赖,其中包括 spring-cloud-starter-bus-kafka 依赖来整合 spring cloud bus & kafka

commons 工程 - system.properties

# kafka 配置

## kafka 服务地址
spring.kafka.bootstrap-servers=127.0.0.1:9092
## producer 提供者
### 如果该值大于零时,表示启用重试失败的发送次数
spring.kafka.producer.retries=0
### 每次批量发送消息的数量
spring.kafka.producer.batch-size=16384
spring.kafka.producer.buffer-memory=33554432
### 指定消息 key 和消息体的编解码方式
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

## consumer 消费者
### 指定默认消费者 group id
spring.kafka.consumer.group-id=springcloud-config-bus-group
### 当 Kafka 中没有初始偏移量或者服务器上不再存在当前偏移量时该怎么办,默认值为 latest,表示自动将偏移重置为最新的偏移量,可选的值为 latest, earliest, none
spring.kafka.consumer.auto-offset-reset=earliest
### 如果为 true,则消费者的偏移量将在后台定期提交,默认值为 true
spring.kafka.consumer.enable-auto-commit=false
### 如果 'enable.auto.commit'为true,则消费者偏移自动提交给 Kafka 的频率(以毫秒为单位),默认值为 5000
spring.kafka.consumer.auto-commit-interval=100
### 指定消息 key 和消息体的编解码方式
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
  • 一些共用配置,不经常修改的,或者是可以统一修改的
  • 比如还可以配置 OSS 的配置信息,Redis 的配置信息,MongoDB 的配置信息等等..

commons 工程 - 初始化配置文件

package com.zwc.core.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/*
 * @ClassName InitConfig
 * @Desc TODO   加载配置文件
 * @Date 2019/6/15 12:58
 * @Version 1.0
 */
@Configuration
@PropertySource("classpath:system.properties")
public class InitConfig {


}
  • 注意这里在注入类的时候,还要加载自定的配置文件,因为 SpringBoot 不会默认加载 system.properties

commons 工程 - 项目结构

配置文件

  创建一个 Git 库,里面存放配置文件,文件夹名称为:config-repo;这里模拟不同的环境,所以分别构建了

  dev(开发)、uat(测试)以及 online(线上)三种不同的配置文件;此文件夹存在此项目的根目录中

- springcloud-config-bus
 
  - config-repo
 
    - system-dev.properties
 
    - system-online.properties
 
    - system-uat.properties
 
  + springcloud-config-bus-commons
  
  + springcloud-config-bus-service

启动本地 Kafka 服务

  下面的 server 和 client 项目启动前都需要启动 zkServer 和 kafka

  zkServer 的使用很简单,下载 zip 后解压双击打开就可以用了

  kafka 的使用可以参考 SpringBoot整合Kafka 博客末尾扩展部分,有写 windows 本地安装和启动 kafka

service 工程

  ① 此工程下有四个模块:一个注册中心,一个 server 以及二个 client

  ② 二个 client 除端口、应用名称以及 group-id 不一致以外,其他代码基本一致

registry-service(注册中心)

registry-service - POM 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 继承父 -->
    <parent>
        <groupId>com.zwc</groupId>
        <artifactId>springcloud-config-bus-service</artifactId>
        <version>1.0</version>
    </parent>

    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-bus-registry-service</artifactId>
    <version>1.0</version>

    <!-- 工程名称描述 -->
    <name>springcloud-config-bus-registry-service</name>
    <description>注册中心</description>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>

    </properties>

    <!-- 加入依赖 -->
    <dependencies>
        <!-- 服务注册中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 主要加入 spring-cloud-starter-netflix-eureka-server 依赖

registry-service - application.yml 配置文件

# 端口
server:
  port: 8761

# 应用名称
spring:
  application:
    name: eureka-server

eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    # 是否向注册中心注册自己
    registerWithEureka: false
    # 是否向注册中心获取注册信息
    fetchRegistry: false
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 这里使用了默认的 8761 端口,当然也可以更改,不过在发现调用服务端的注册中心地址端口要与它一致

registry-service - 启动类

package com.zwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringcloudConfigBusRegistryServiceApplication {

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

}
  • 在启动类中添加 @EnableEurekaServer 注解表示此工程是注册中心

registry-service - 启动项目

  1. 项目启动成功后访问 http://localhost:8761/ 即可看到 eureka-server 主页面

server(获取远程的配置信息)

server - POM 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 继承父 -->
    <parent>
        <groupId>com.zwc</groupId>
        <artifactId>springcloud-config-bus-service</artifactId>
        <version>1.0</version>
    </parent>

    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-bus-server-service</artifactId>
    <version>1.0</version>

    <!-- 工程名称描述 -->
    <name>springcloud-config-bus-server-service</name>
    <description>config server</description>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>

    </properties>

    <!-- 加入依赖 -->
    <dependencies>
        <!-- commons工程 依赖 -->
        <dependency>
            <groupId>com.zwc</groupId>
            <artifactId>springcloud-config-bus-commons</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- 提供者消费者 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- config server 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

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

    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 加入 spring-cloud-config-server 依赖:config server
  • 加入 spring-cloud-starter-netflix-eureka-client 依赖:提供和注册服务
  • 加入 spring-boot-starter-actuator 依赖:开启并配置端点

server - application.yml 配置文件

# 端口
server:
  port: 8000

spring:
  application:
    # 应用名称
    name: config-bus-server
  cloud:
    config:
      server:
        git:
          # 仓库地址
          uri: https://github.com/intomylife/SpringCloud
          # 对应 {label} 部分,即 Git 的分支
          label: master
          # 仓库文件夹名称,多个以逗号分隔
          search-paths: springcloud-config-bus/config-repo
          # git 仓库用户名(公开库可以不用填写)
          username:
          # git 仓库密码(公开库可以不用填写)
          password:
    bus:
      # 开启消息跟踪
      enabled: true
      trace:
        enabled: true
  kafka:
    consumer:
      # 指定默认消费者 group id
      # 如果不设置,将会使用 commons 工程中的 group-id,那时调用 /actuator/bus-refresh 接口只会刷新其中一个 client
      # 因为在同一个组中的 Consumer,同一个主题只会被一个 Consumer 接收
      group-id: config-bus-server-group

eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

management:
  endpoints:
    web:
      exposure:
        # 开启刷新端点
        include: bus-refresh
  • 注意这里 uri 只能写到仓库名
  • 如果配置文件在仓库中多层目录下,那么 search-paths 就从根目录开始写起
  • 注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口
  • 在 2.0 后需要主动开启端点 bus-refresh,否则出错

server - 启动类

package com.zwc;

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

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class SpringcloudConfigBusServerServiceApplication {

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

}
  • 添加 @EnableConfigServer 注解表示开启配置中心
  • 添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务

server -  启动项目

  1. 项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

  2. 访问地址:http://localhost:8000/config-repo/dev(获取完整配置信息)

  3. 输出内容:

{
    "name":"config-repo",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"742276b4c0d821af224cb104590a5d93d4e5e71a",
    "state":null,
    "propertySources":[

    ]
}

  4. 访问地址:http://localhost:8000/system/dev(获取完整配置信息)

  5. 输出内容:

{
    "name":"system",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"742276b4c0d821af224cb104590a5d93d4e5e71a",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-bus/config-repo/system-dev.properties",
            "source":{
                "hello":"I'm dev."
            }
        }
    ]
}

  6. 访问地址:http://localhost:8000/system-dev.properties(获取指定配置文件内容)

  7. 输出内容:'hello: I'm dev.'

  8. 证明 server 已经成功从远程仓库中获取到了配置信息

  注:接口访问有如下规则:

/{application}/{profile}[/{label}]
 
/{application}-{profile}.yml
 
/{label}/{application}-{profile}.yml
 
/{application}-{profile}.properties
 
/{label}/{application}-{profile}.properties

client(读取注册中心的 server 中的配置信息)

client - POM 文件

  注:一共有两个 client,除端口、应用名称以及 group-id 以外的代码基本上一致,所有这里就列举其中一个

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 继承父 -->
    <parent>
        <groupId>com.zwc</groupId>
        <artifactId>springcloud-config-bus-service</artifactId>
        <version>1.0</version>
    </parent>

    <!-- 三坐标 -->
    <groupId>com.zwc</groupId>
    <artifactId>springcloud-config-bus-clientfirst-service</artifactId>
    <version>1.0</version>

    <!-- 工程名称描述 -->
    <name>springcloud-config-bus-clientfirst-service</name>
    <description>config client</description>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
    <properties>

    </properties>

    <!-- 加入依赖 -->
    <dependencies>
        <!-- commons工程 依赖 -->
        <dependency>
            <groupId>com.zwc</groupId>
            <artifactId>springcloud-config-bus-commons</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- config 依赖 -->
        <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>

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

    <!-- 插件依赖 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 加入 spring-cloud-starter-config 依赖:config client
  • 加入 spring-cloud-starter-netflix-eureka-client 依赖:提供和注册服务
  • 加入 spring-boot-starter-actuator 依赖:开启并配置端点

client - application.yml 配置文件

# 端口
server:
  port: 9000

spring:
  application:
    # 应用名称
    name: config-bus-client-first

client - bootstrap.yml 配置文件(注意)

spring:
  cloud:
    config:
      # 对应 {label} 部分,即 Git 的分支
      label: master
      # 对应 {application} 部分
      name: system
      # 对应 {profile} 部分
      profile: dev
      discovery:
        # 开启 Config 服务发现与注册
        enabled: true
        # 指定 server
        service-id: config-bus-server
    bus:
      # 开启消息跟踪
      enabled: true
      trace:
        enabled: true
  kafka:
    consumer:
      # 指定默认消费者 group id
      # 如果不设置,将会使用 commons 工程中的 group-id,那时调用 /actuator/bus-refresh 接口只会刷新其中一个 client
      # 因为在同一个组中的 Consumer,同一个主题只会被一个 Consumer 接收
      group-id: config-bus-client-first-group


eureka:
  instance:
    # 使用 ip 代替实例名
    prefer-ip-address: true
    # 实例的主机名
    hostname: ${spring.cloud.client.ip-address}
    # 实例的 ID 规则
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  client:
    serviceUrl:
      # 注册中心地址
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

management:
  endpoints:
    web:
      exposure:
        # 开启刷新端点
        include: bus-refresh
  • bootstrap.yml 配置文件加载先于 application.yml 配置文件
  • 与 Spring Cloud Config 相关的属性必须配置在 bootstrap.yml 中
  • 这里就不是直接指定 server 地址了,而是 server 的应用名(spring.application.name)
  • 从注册中心的 server 中读取 dev(开发)环境的配置文件信息
  • 注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口
  • 在 2.0 后需要主动开启端点 bus-refresh,否则出错

client - controller 前端控制器

package com.zwc.first.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName HelloController
 * @Desc TODO   读取 server 配置文件
 * @Date 2019/6/2 16:54
 * @Version 1.0
 */
@RestController
@RefreshScope
public class HelloController {

    @Value("${hello}")
    private String hello;

    /*
     * @ClassName HelloController
     * @Desc TODO   读取 server 配置文件
     * @Date 2019/6/2 16:56
     * @Version 1.0
     */
    @RequestMapping("/hello")
    public String hello() {
        return this.hello;
    }

}
  • 使用 @RefreshScope 注解才能读取刷新后的配置
  • 使用 @Value 注解就可以从注册中心的 server 中读取到对应的配置信息

client - 启动类

package com.zwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class SpringcloudConfigBusClientfirstServiceApplication {

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

}
  • 添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务

client -  启动项目

  1. 项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

  2. 访问地址:http://localhost:9000/hello

  3. 输出内容:'I'm dev.'

  4. 启动另一个 client(springcloud-config-bus-clientsecond-service)

  5. 项目启动成功后访问:http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

  6. 访问地址:http://localhost:9001/hello

  7. 输出内容:'I'm dev.'

  8. 证明两个 client 都已经成功从注册中心的 server 中读取到了配置信息

  9. 全局刷新

 10. 修改远程 Git 仓库 system-dev.properties 配置文件中的内容为 'hello=I'm dev. update!!!'

 11. 此时刷新地址:http://localhost:8000/system/dev(获取完整配置信息)

 12. 输出内容:

{
    "name":"system",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"4c89d3baf3a67e56f5afe3f97914a41c16f46e7d",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-bus/config-repo/system-dev.properties",
            "source":{
                "hello":"I'm dev. update!!!"
            }
        }
    ]
}

 13. 与前言中说的一致,server 可以及时获取到最新的配置信息

 14. 这时再刷新地址:http://localhost:9000/hello 和 http://localhost:9000/hello

 15. 发现依旧还是: 'I'm dev.'

 16. 打开 cmd,输入命令: curl -X POST http://localhost:8000/actuator/bus-refresh

 17. 此时 cmd 中正常情况下是不会有任何输出内容的

 18. 再次刷新地址:http://localhost:9000/hello 和 http://localhost:9000/hello

 19. 发现内容都更改为:'I'm dev. update!!!'

 20. 局部刷新

 21. 再次修改远程 Git 仓库 system-dev.properties 配置文件中的内容为 'hello=I'm dev. update 222!!!'

 22. 此时刷新地址:http://localhost:8000/system/dev(获取完整配置信息)

 23. 输出内容:

{
    "name":"system",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"96e58a513e38c96e22361e0bf97a1735f448bf59",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/intomylife/SpringCloud/springcloud-config-bus/config-repo/system-dev.properties",
            "source":{
                "hello":"I'm dev. update 222!!!"
            }
        }
    ]
}

 24. server 依旧及时获取到最新的配置信息

 25. 这时再刷新地址:http://localhost:9000/hello 和 http://localhost:9000/hello

 26. 发现依旧还是: 'I'm dev. update!!!'

 27. 打开 cmd,输入命令: curl -X POST http://localhost:8000/actuator/bus-refresh/config-bus-client-first:9000

 28. 此时 cmd 中正常情况下是不会有任何输出内容的

 29. 再次刷新地址:http://localhost:9000/hello

 30. 发现内容更改为:'I'm dev. update 222!!!'

 31. 再次刷新地址:http://localhost:9001/hello

 32. 发现内容还是:'I'm dev. update!!!'

 33. 所以全局刷新就使用 /actuator/bus-refresh 接口,局部刷新就使用 /actuator/bus-refresh/应用名称:端口

service 工程 - 项目结构

把多工程项目使用 IntelliJ IDEA  打开

  1. 把项目从 GitHub 中下载到你的本地
  2. 打开 IntelliJ IDEA 
  3. 点击 File -> Open
  4. 打开你下载到本地的项目目录
  5. springcloud-config-bus -> springcloud-config-bus-service(选择打开此工程)
  6. 打开 service 工程后
  7. 再次点击 File -> Project Structrue
  8. 选择 Modules,点击 '+' 符号
  9. 点击 Import  Module
  10. 还是打开你下载到本地的项目目录
  11. springcloud-config-bus -> springcloud-config-bus-commons -> pom.xml
  12. 点击 OK
  13. 点击 Next,Finish
  14. 点击 Apply,OK

希望能够帮助到你

over

猜你喜欢

转载自blog.csdn.net/qq_41402200/article/details/92077652