首页 > 其他分享 >Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新

Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新

时间:2022-11-09 22:39:52浏览次数:72  
标签:spring Spring boot springframework Bus org import Config cloud

推荐:微服务汇总

Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新

首先创建一个Spring Boot项目作为注册中心。

pom.xml如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kaven</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.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>

application.yml如下:

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: false
  server:
    enable-self-preservation: true

spring:
  application:
    name: eureka

server:
  port: 8761

启动类:

package com.kaven.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

注解@EnableEurekaServer不要忘记加上。

再来创建一个Spring Boot项目作为Config Server配置中心。

pom.xml如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <groupId>com.kaven</groupId>
    <artifactId>config</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>config</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.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>

这个依赖很重要:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>

之前遇到过问题:Spring Cloud Config使用Github的Webhooks功能出现400状态码

要实现配置动态更新,还需要使用Bus组件,这个组件使用起来非常简单,只需要引入依赖,配置相关消息队列的信息即可,其他的就不用管了,因为Config组件内部已经封装好了,当配置文件发生更改后,Config Server会收到消息(我这里是使用Github的Webhooks功能),然后Config Server通过Bus组件将配置更改消息告诉Config Client。这里就先用一下Bus组件,不过多介绍,并且消息队列是RabbitMQ。

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

application.yml如下:

server:
  port: 8083

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ITKaven/config   # 配置文件仓库,之后会进行创建
          username: username
          password: password
          basedir: E:\workspace\IDEA\spring cloud\config\basedir
  rabbitmq:
    addresses: 127.0.0.1
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: "*"

GitHub会通过你设置的Webhooks来请求你Config Server的monitor接口,所以这里暴露全部端点(接口):

management:
  endpoints:
    web:
      exposure:
        include: "*"

启动类:

package com.kaven.config;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {

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

注解@EnableDiscoveryClient@EnableConfigServer不要忘记加上。

先启动注册中心和Config Server配置中心。

GitHub会通过你设置的Webhooks来请求你Config Server的monitor接口,而我们是在本地进行学习开发的,GitHub是不能请求到我们本地的接口,所以这里需要使用到内网穿透工具。

我使用的是这个(不是广告):内网穿透

创建免费隧道,代理到本地8083端口,也就是我Config Server的运行端口。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_spring

再根据natapp提供的教程来实现内网穿透,其实很简单,下载一个exe文件,然后输入命令即可。

NATAPP1分钟快速新手图文教程

这样就弄好了。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_后端_02

再来创建一个Github仓库,用来存放配置文件。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_spring boot_03

设置该GitHub仓库的Webhooks。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_spring_04
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_spring_05
可以发现此时Github已经请求了一次,并且请求成功。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_xml_06

再来创建一个Spring Boot项目作为获取配置的Config Client。

pom.xml如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.kaven</groupId>
    <artifactId>getconfig</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>getconfig</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <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.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.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>

bootstrap.yml如下:

spring:
  application:
    name: getconfig
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config
      profile: dev
  rabbitmq:
    addresses: 127.0.0.1
    port: 5672
    username: guest
    password: guest


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

注意这里配置文件是bootstrap.yml,而不是application.yml,因为它需要先去获取配置文件才能运行,而不能运行后再去获取配置文件(端口都没有配置,需要去Config Server获取)。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_spring boot_07
启动类:

package com.kaven.getconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class GetconfigApplication {

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

注解@EnableDiscoveryClient不要忘记加上。

再在Github的配置中心仓库创建文件getconfig.yml(文件命名要按要求定义):
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_spring_08

创建一个接口:

package com.kaven.getconfig.controller;

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

@RestController
@RefreshScope
public class GetConfig {

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

    @GetMapping("/getkaven")
    public String getKaven(){
        return kaven;
    }
}

注解@RefreshScope不能少,这是实现配置动态更新的关键一步。
启动项目。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_xml_09
可以看到项目会自动去配置中心拉取配置文件。

项目成功运行在8000端口。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_java_10
访问http://localhost:8000/getkaven,可以得到如下结果:
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_xml_11
我们来改变一下配置参数kaven
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_后端_12

配置更新后,Github会请求Config Server配置中心的monitor接口,通过内网穿透工具可以看见Github的请求成功了。
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_java_13
再来访问http://localhost:8000/getkaven,可以得到如下结果:
Spring Cloud 之Config配置中心-使用Bus组件实现配置动态更新_spring_14

这就实现了配置动态更新。

写博客是博主记录自己的学习过程,如果有错误,请指正,谢谢!

标签:spring,Spring,boot,springframework,Bus,org,import,Config,cloud
From: https://blog.51cto.com/u_15870611/5838866

相关文章

  • ✳驱动之ic_bus_type框架
      DTS中的i2c设备节点(子节点)(例如:AT24C02)被转化为i2c_client结构体,其所在的i2c控制器节点(父节点)转化为platform_device结构体,匹配到对应的platform_driver结......
  • Spring Boot:文件下载
    测试代码​​pom.xml​​:<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst......
  • SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource
    springboot的前置知识:通过注解创建对象和读取配置文件1.JavaConfig设计思想使用java类作为xml配置文件的替代,是配置spring容器的纯java的方式可以创建java对象并把......
  • SpringCloud(八) - 自定义token令牌,鉴权(注解+拦截器),参数解析(注解+解析器)
    1、项目结构介绍项目有使用到,redis和swagger,不在具体介绍;2、手动鉴权和用户信息参数获取(繁杂,冗余)2.1用户实体类/***CreatedOn:4/11/2022.*<p>*Author......
  • SpringMVC概述
    1.SpringMVC是一种基于Java实现MVC模型的轻量级Web框架优点:使用简单,开发便捷(相比于Servlet)灵活性强2.SpringMVC是一种表现层框架技术,用于进行表现层功能开发3. Java......
  • SpringCloud(七) - 微信支付
    1、开发文档微信开发文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1安全规范:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_31、......
  • Spring Cloud Alibaba:Gateway之路由过滤器工厂(三)
    前两篇博客已经介绍了十一种路由过滤器工厂:​​SpringCloudAlibaba:Gateway之路由过滤器工厂(一)​​​​SpringCloudAlibaba:Gateway之路由过滤器工厂(二)​​随着​​Gatewa......
  • Spring Boot:自定义SpringApplication
    自定义SpringApplication如果​​SpringApplication​​​的默认值不满足我们的需求,可以创建​​SpringApplication​​实例并对其进行自定义设置,例如,要关闭横幅:packagecom......
  • Spring Boot:热部署
    配置IDEA勾选​​Buildprojectautomatically​​选项。搜索​​Registry​​​(双击​​Shift​​​键),找到并勾选​​compiler.automake.allow.when.app.running​​选项。......
  • Spring Boot:替换Web服务器
    ​​SpringBoot​​​应用包含默认的嵌入式​​Web​​容器。对于​​servlet​​​应用,​​spring-boot-starter-web​​​通过依赖​​spring-boot-starter-tomcat​​​......