本文共 13471 字,大约阅读时间需要 44 分钟。
在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
添加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
控制器增加@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 ""; }}
将foo参数对应的值更新为“bar-jdbc version 2”,需要提交commit操作。
发现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
重新访问http://localhost:8083/get?key=foo
转载地址:http://bwtab.baihongyu.com/