• spring cloud 快速上手系列 -> 03-消息队列 Stream -> 033-使用spring cloud bus实现配置中心热刷新-Client


    spring cloud 快速上手系列

    系列说明:快速上手,一切从简,搭建一个简单的微服务框架,让新手可以在这个基础框架上做各种学习、研究。

    03-消息队列 Stream

    033-使用spring cloud bus实现配置中心热刷新-Client

    1,调用序列图

    画的比较简陋,大致能表达意思
    在这里插入图片描述

    2,ConfigClientHot
    1) 代码目录

    在这里插入图片描述
    为了能启动两个节点(不同的端口号),这里利用了maven的环境配置,配置了两个application.yml文件。

    2) 代码内容
    • pom.xml
    
    <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.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.3version>
            <relativePath/>
        parent>
        <groupId>com.hui.study.cloudgroupId>
        <artifactId>StudyConfigClientHotartifactId>
        <version>1.0.0-SNAPSHOTversion>
        <properties>
            <java.version>1.8java.version>
            <spring-cloud.version>2021.0.4spring-cloud.version>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>${spring-cloud.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-configartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-bootstrapartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-bus-amqpartifactId>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • application-dev6102.yml
    server:
      port: 6102  #Config-Client的端口号
    demo:
      name: "demo default"
    
    • 1
    • 2
    • 3
    • 4
    • application-dev6103.yml
    server:
      port: 6103  #Config-Client的端口号
    demo:
      name: "demo default"
    
    • 1
    • 2
    • 3
    • 4
    • bootstrap.yml
    spring:
      profiles:
        active: dev        #激活开发环境的配置文件
      application:
        name: config-client
      cloud:
        config:
          label: master
          #开发环境
          profile: dev
          #从Eureka中发现配置中心
          discovery:
            enabled: true
            #指定配置中心的application.name
            service-id: config-server-hot
        stream:
          binders: # 在此处配置要绑定的rabbitmq的服务信息;
            defaultRabbit: # 表示定义的名称,用于于binding整合
              type: rabbit # 消息组件类型
              environment: # 设置rabbitmq的相关的环境配置
                spring:
                  rabbitmq:
                    host: localhost
                    port: 5672
                    username: guest
                    password: guest
    eureka:
      client:
        #表示是否将自己注册进EurekaServer
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。
        fetchRegistry: true
        service-url:
          #服务中心地址
          defaultZone: http://localhost:7001/eureka
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    增加了stream和management部分

    • CloudConfigClientHotApplication.java
    package com.hui.study.cloud.config;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    /**
     * 配置中心客户端-Hot
     */
    public class CloudConfigClientHotApplication {
        public static void main(String[] args) {
            SpringApplication.run(CloudConfigClientHotApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • DemoController.java
    package com.hui.study.cloud.config.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;
    
    /**
     * 被调用的demo
     */
    @RestController
    @RefreshScope
    @RequestMapping("/demo")
    public class DemoController {
        @Value("${demo.name}")
        private String name;
    
        @RequestMapping("/name")
        public String name() {
            return name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    增加了RefreshScope注解

    3) 启动

    注册中心、配置中心都正常启动
    分别利用application-dev6102.yml和application-dev6103.yml配置,启动两个微服务节点。
    启动成功后,访问 http://localhost:7001
    在这里插入图片描述
    CONFIG-CLIENT已经注册上来了

    4)调用

    用apifox或postman访问:http://localhost:6103/demo/name
    在这里插入图片描述
    更改config-client-dev.yml内容:

    demo:
      name: "demo for dev update22" 
    
    • 1
    • 2

    并push到git服务器。
    再访问http://localhost:6103/demo/name,发现没有变化,还是旧的内容。

    调用配置中心的刷新总线接口:http://localhost:6101/actuator/busrefresh
    在这里插入图片描述
    再访问http://localhost:6103/demo/name,发现有变化了,是新的内容。
    在这里插入图片描述

    3,代码及作业

    链接:https://pan.baidu.com/s/1JLIMyQiWD7v29UKdCUiOpg?pwd=7glb
    提取码:7glb

    链接:https://pan.baidu.com/s/1I59K62nrLwOHf24oT2FyBg?pwd=bvbb
    提取码:bvbb

    作业:
    1、增加一个节点(用6104端口再起一个节点)
    2、增加一个demo.from 配置,并在 config-client 中用接口获取

    这两章,都是spring cloud发送、消费消息,我们只需要配置即可。
    下面一章,我们单独编写发送消息、消费消息的操作代码。

  • 相关阅读:
    Springboot之Bean懒加载的实现详解
    猿创征文|瑞吉外卖——移动端_地址管理
    用DIV+CSS技术设计的个人电影网站(web前端网页制作课作业)
    【甄选靶场】Vulnhub百个项目渗透——项目二十六:Pinky‘s-Palace-1(代理访问,缓冲区溢出)
    Clickhouse:clickhouse切换目录
    kubernetes namespace pod label deployment介绍与命令
    卷积网络识别猴痘
    docker 命令
    (三)linux文件与目录管理
    在ubuntu20通过docker部署zabbix6
  • 原文地址:https://blog.csdn.net/yihui823/article/details/126831624