博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud 2.x学习笔记:7、Spring Cloud Bus(Greenwich版本)
阅读量:2389 次
发布时间:2019-05-10

本文共 13471 字,大约阅读时间需要 44 分钟。

1、Spring Cloud Bus介绍

2、改造config-client

2.1 pom.xml

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

4.0.0
com.cntaiping.tpa
config-client
0.0.1-SNAPSHOT
jar
config-client
Demo project for Spring Boot
com.cntaiping.tpa
config
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.boot
spring-boot-starter-actuator

2.2 bootstrap.properties

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

spring.application.name=config-clienteureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/spring.profiles.active=dev#spring.cloud.config.uri= http://localhost:8300/spring.cloud.config.fail-fast=truespring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=config-serverserver.port=8083spring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guestspring.cloud.bus.enabled=truespring.cloud.bus.trace.enabled=truemanagement.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@RefreshScopepublic 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请求

记住,不是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 configuration2019-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://guest@127.0.0.1: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: STARTING2019-06-13 15:42:23.212  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-12019-06-13 15:42:23.220  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson2019-06-13 15:42:23.221  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson2019-06-13 15:42:23.221  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml2019-06-13 15:42:23.221  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml2019-06-13 15:42:23.332  INFO 11000 --- [nio-8083-exec-9] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Disable delta property : false2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Application is null : false2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Application version is -1: true2019-06-13 15:42:23.333  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server2019-06-13 15:42:23.341  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : The response status is 2002019-06-13 15:42:23.342  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Not registering with Eureka server per configuration2019-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: 32019-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 UP2019-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=null2019-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: dev2019-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 DOWN2019-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 DiscoveryClient2019-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: 2002019-06-13 15:42:26.732  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient2019-06-13 15:42:26.739  INFO 11000 --- [nio-8083-exec-9] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING2019-06-13 15:42:26.740  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-12019-06-13 15:42:26.745  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson2019-06-13 15:42:26.745  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson2019-06-13 15:42:26.745  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml2019-06-13 15:42:26.745  INFO 11000 --- [nio-8083-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml2019-06-13 15:42:26.830  INFO 11000 --- [nio-8083-exec-9] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Disable delta property : false2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Application is null : false2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Application version is -1: true2019-06-13 15:42:26.840  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server2019-06-13 15:42:26.845  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : The response status is 2002019-06-13 15:42:26.846  INFO 11000 --- [nio-8083-exec-9] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 302019-06-13 15:42:26.847  INFO 11000 --- [nio-8083-exec-9] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 42019-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: 32019-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 DOWN2019-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 UP2019-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 DOWN2019-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 UP2019-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

在这里插入图片描述

转载地址:http://bwtab.baihongyu.com/

你可能感兴趣的文章
非科班的Java学习路线
查看>>
Android Scroller简单用法
查看>>
ffmpeg系列:使用ffmpeg转换为RGB数据并缩放视频
查看>>
能Ping通外网但就是不能打开所有网页的解决办法
查看>>
windows7配置虚拟AP的脚本
查看>>
北京开放政府信息资源 “大数据”供社会化利用
查看>>
大数据挖掘变革 美赛达软硬云引领车联网商业蓝海
查看>>
停车费上涨需要公开“大数据”
查看>>
DirectFB代码导读
查看>>
Cocos2dx3.2从零开始【四】继续。
查看>>
开发人员应具备的产品设计意识
查看>>
sphinx教程2__安装、配置和使用
查看>>
ttserver 缓存使用和过期设置
查看>>
php pconnect 长连接原理
查看>>
php memcached使用中的坑
查看>>
Discuz!$_G变量的使用方法
查看>>
mysql 编码字符集配置
查看>>
php转换html格式为文本格式
查看>>
mysql-proxy主从服务架构下读写分离和负载均衡实现及原理
查看>>
基于nginx的FastCGI的缓存配置
查看>>