前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud 2.x学习笔记:7、Spring Cloud Bus(Greenwich版本)

SpringCloud 2.x学习笔记:7、Spring Cloud Bus(Greenwich版本)

作者头像
程裕强
发布2019-07-02 10:41:55
6670
发布2019-07-02 10:41:55
举报

1、Spring Cloud Bus介绍

2、改造config-client

2.1 pom.xml

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

代码语言:javascript
复制
<?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的三个配置

代码语言:javascript
复制
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注解,无该注解的话将无法更新配置参数。

代码语言:javascript
复制
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会重新读取配置文件

代码语言:javascript
复制
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://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: 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

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年06月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、Spring Cloud Bus介绍
  • 2、改造config-client
    • 2.1 pom.xml
      • 2.2 bootstrap.properties
        • 2.3 控制器
        • 3、运行效果
          • 3.1 更新配置参数
            • 3.2 发送bus-refresh请求
              • 3.3 更新参数
                • 3.4 获取新值
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档