• <noscript id="e0iig"><kbd id="e0iig"></kbd></noscript>
  • <td id="e0iig"></td>
  • <option id="e0iig"></option>
  • <noscript id="e0iig"><source id="e0iig"></source></noscript>
  • SpringCloud 2.x學習筆記:7、Spring Cloud Bus(Greenwich版本)

    1、Spring Cloud Bus介紹

    2、改造config-client

    2.1 pom.xml

    在pom.xml文件添加spring-cloud-starter-bus-amqp,完整的配置文件如下:

    <?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.cntaiping.tpa</groupId>
        <artifactId>config-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>config-client</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>com.cntaiping.tpa</groupId>
            <artifactId>config</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <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-actuator</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    

    2.2 bootstrap.properties

    添加RabbitMq的配置,包括RabbitMq的地址、端口,用戶名、密碼。并需要加上spring.cloud.bus的三個配置

    spring.application.name=config-client
    eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
    spring.profiles.active=dev
    #spring.cloud.config.uri= http://localhost:8300/
    spring.cloud.config.fail-fast=true
    spring.cloud.config.discovery.enabled=true
    spring.cloud.config.discovery.serviceId=config-server
    server.port=8083
    spring.rabbitmq.host=localhost
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest
    spring.cloud.bus.enabled=true
    spring.cloud.bus.trace.enabled=true
    management.endpoints.web.exposure.include=bus-refresh
    

    2.3 控制器

    控制器增加@RefreshScope注解,無該注解的話將無法更新配置參數。

    package com.cntaiping.tpa.configclient.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;
    
    @RestController
    @RefreshScope
    public class ConfigController {
    
        @Value("${server.port}")
        String port;
    
        @Value("${foo}")
        String foo;
    
        @RequestMapping(value = "/get")
        public String get(String key){
            if("foo".equals(key)){
                return foo;
            }
            if("server.port".equals(key)){
                return port;
            }
            return "";
        }
    }
    
    

    3、運行效果

    3.1 更新配置參數

    將foo參數對應的值更新為“bar-jdbc version 2”,需要提交commit操作。
    在這里插入圖片描述

    3.2 發送bus-refresh請求

    http://localhost:8083/actuator/bus-refresh
    記住,不是get請求。
    在這里插入圖片描述
    下面通過postman發送POST請求,
    在這里插入圖片描述

    3.3 更新參數

    發現config-client會重新讀取配置文件

    2019-06-13 15:40:22.332  WARN 11000 --- [nio-8083-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]
    2019-06-13 15:41:10.841  INFO 11000 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
    2019-06-13 15:42:22.858  INFO 11000 --- [nio-8083-exec-9] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
    2019-06-13 15:42:22.877  INFO 11000 --- [nio-8083-exec-9] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory.publisher#1213f55e:0/SimpleConnection@1a2eea8d [delegate=amqp://[email protected]:5672/, localPort= 62509]
    2019-06-13 15:42:22.883  INFO 11000 --- [nio-8083-exec-9] o.s.amqp.rabbit.core.RabbitAdmin         : Auto-declaring a non-durable, auto-delete, or exclusive Queue (springCloudBus.anonymous._2_-oFqATS6rxV5uJl_Baw) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.
    2019-06-13 15:42:23.128  INFO 11000 --- [nio-8083-exec-9] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$40570b93] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2019-06-13 15:42:23.129  INFO 11000 --- [nio-8083-exec-9] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$66ad766b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2019-06-13 15:42:23.204  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
    2019-06-13 15:42:23.212  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
    2019-06-13 15:42:23.220  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
    2019-06-13 15:42:23.221  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
    2019-06-13 15:42:23.221  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
    2019-06-13 15:42:23.221  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
    2019-06-13 15:42:23.332  INFO 11000 --- [nio-8083-exec-9] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
    2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
    2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
    2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
    2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Application is null : false
    2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
    2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
    2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
    2019-06-13 15:42:23.341  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : The response status is 200
    2019-06-13 15:42:23.342  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Not registering with Eureka server per configuration
    2019-06-13 15:42:23.343  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1560411743343 with initial instances count: 3
    2019-06-13 15:42:23.353  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application CONFIG-CLIENT with eureka with status UP
    2019-06-13 15:42:23.446  INFO 11000 --- [nio-8083-exec-9] c.c.c.ConfigServicePropertySourceLocator : Multiple Config Server Urls found listed.
    2019-06-13 15:42:23.446  INFO 11000 --- [nio-8083-exec-9] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://TPA-VDI-OA02-10.tpam.local:8301/
    2019-06-13 15:42:23.511  INFO 11000 --- [nio-8083-exec-9] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=null, version=null, state=null
    2019-06-13 15:42:23.511  INFO 11000 --- [nio-8083-exec-9] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='config-client-dev'}]}
    2019-06-13 15:42:23.513  INFO 11000 --- [nio-8083-exec-9] o.s.boot.SpringApplication               : The following profiles are active: dev
    2019-06-13 15:42:23.519  INFO 11000 --- [nio-8083-exec-9] o.s.boot.SpringApplication               : Started application in 0.602 seconds (JVM running for 383.194)
    2019-06-13 15:42:23.521  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application CONFIG-CLIENT with eureka with status DOWN
    2019-06-13 15:42:23.522  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
    2019-06-13 15:42:23.528  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
    2019-06-13 15:42:23.691  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
    2019-06-13 15:42:26.693  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Unregistering ...
    2019-06-13 15:42:26.701  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/TPA-VDI-OA02-10.tpam.local:config-client:8083 - deregister  status: 200
    2019-06-13 15:42:26.732  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
    2019-06-13 15:42:26.739  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
    2019-06-13 15:42:26.740  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
    2019-06-13 15:42:26.745  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
    2019-06-13 15:42:26.745  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
    2019-06-13 15:42:26.745  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
    2019-06-13 15:42:26.745  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
    2019-06-13 15:42:26.830  INFO 11000 --- [nio-8083-exec-9] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
    2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
    2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
    2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
    2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Application is null : false
    2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
    2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
    2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
    2019-06-13 15:42:26.845  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : The response status is 200
    2019-06-13 15:42:26.846  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
    2019-06-13 15:42:26.847  INFO 11000 --- [nio-8083-exec-9] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
    2019-06-13 15:42:26.847  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1560411746847 with initial instances count: 3
    2019-06-13 15:42:26.853  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application CONFIG-CLIENT with eureka with status DOWN
    2019-06-13 15:42:26.853  WARN 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1560411746853, current=DOWN, previous=STARTING]
    2019-06-13 15:42:26.853  INFO 11000 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/TPA-VDI-OA02-10.tpam.local:config-client:8083: registering service...
    2019-06-13 15:42:26.853  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application CONFIG-CLIENT with eureka with status UP
    2019-06-13 15:42:26.853  WARN 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1560411746853, current=UP, previous=DOWN]
    2019-06-13 15:42:26.854  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application CONFIG-CLIENT with eureka with status DOWN
    2019-06-13 15:42:26.854  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application CONFIG-CLIENT with eureka with status UP
    2019-06-13 15:42:26.855  INFO 11000 --- [nio-8083-exec-9] o.s.cloud.bus.event.RefreshListener      : Received remote refresh request. Keys refreshed [foo]
    2019-06-13 15:42:26.860  INFO 11000 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/TPA-VDI-OA02-10.tpam.local:config-client:8083 - registration status: 204
    
    

    3.4 獲取新值

    重新訪問http://localhost:8083/get?key=foo
    在這里插入圖片描述

    版權聲明:本文為chengyuqiang原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
    本文鏈接:https://blog.csdn.net/chengyuqiang/article/details/91868701

    智能推薦

    SpringCloud學習筆記【十三】Spring Cloud Bus消息總線

    文章目錄 本片要點 Spring Cloud Bus簡介 概述 什么是總線 基本原理 Docker安裝RabbitMQ 演示動態刷新全局廣播前置準備 新建模塊,引入依賴 配置bootstrap.yml 編寫主啟動類 編寫接口 設計思想 開始演示動態刷新全局廣播 為三個模塊都添加消息總線支持 為三個模塊配置yml 為ConfigServer配置yml 為ConfigClient配置yml 測試 原理...

    SpringCloud教程 | 第八篇: 消息總線(Spring Cloud Bus)(Finchley版本)

    轉載請標明出處:  http://blog.csdn.net/forezp/article/details/81041062  本文出自方志朋的博客 轉載請標明出處: Spring Cloud Bus 將分布式的節點用輕量的消息代理連接起來。它可以用于廣播配置文件的更改或者服務之間的通訊,也可以用于監控。本文要講述的是用Spring Cloud Bus實現通知微服務架構的配置文...

    SpringCloud第八篇:消息總線(Spring Cloud Bus)(Finchley版本)

           Spring Cloud Bus將分布式的節點用輕量的消息代理連接起來。它可以用于廣播配置文件的更改或者服務之間的通訊,也可以用于監控。本文要講述的是用Spring Cloud Bus實現通知微服務架構的配置文件的更改。 一、準備工作        首先需要安裝rabbitmq,安裝教程鏈接地址:http...

    6.SpringCloud學習(六)——Spring Cloud Bus 消息總線

    1.簡介 1.1 概述 Spring Cloud Bus links nodes of a distributed system with a lightweight message broker. This can then be used to broadcast state changes (e.g. configuration changes) or other management in...

    spring cloud Greenwich 學習筆記(五)spring cloudconfig + spring cloud bus實現全自動刷新集群配置

    文章目錄 概述 前情提要 集成springcloud bus消息總線 測試 全自動刷新 springcloud系列學習筆記目錄參見博主專欄 spring boot 2.X/spring cloud Greenwich。 由于是一系列文章,所以后面的文章可能會使用到前面文章的項目。文章所有代碼都已上傳GitHub:https://github.com/liubenlong/springcloudGr...

    猜你喜歡

    HTML中常用操作關于:頁面跳轉,空格

    1.頁面跳轉 2.空格的代替符...

    freemarker + ItextRender 根據模板生成PDF文件

    1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...

    電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!

    Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...

    requests實現全自動PPT模板

    http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...

    精品国产乱码久久久久久蜜桃不卡