SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

一、安装rabbitmq

二、pom父文件

<?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>1.0.0</groupId>
  <artifactId>springcloud-config-bus</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>springcloud-config-bus</name>
  <url>http://maven.apache.org</url>

  <modules>
    <module>springcloud-config-bus-eureka</module>
    <module>springcloud-config-bus-client2</module>
    <module>springcloud-config-bus-server</module>
  </modules>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/>
    </parent>

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
       <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

      <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

三、springcloud-config-bus-eureka

1、pom文件

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>1.0.0</groupId>
    <artifactId>springcloud-config-bus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-config-bus-eureka</artifactId>
  <name>springcloud-config-bus-eureka</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
   <dependencies>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
  </dependencies>
</project>

2、application.properties

spring.application.name=springcloud-config-bus-eureka

server.port=8006
## 表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=false
## 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=false

## 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ConfigBusEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigBusEurekaApplication.class, args);
         System.out.println("config bus 注册中心服务启动...");
    }
}

四、springcloud-config-bus-server

1、pom文件

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>1.0.0</groupId>
        <artifactId>springcloud-config-bus</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>springcloud-config-bus-server</artifactId>
    <name>springcloud-config-bus-server</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、application.properties

spring.application.name=springcloud-config-bus-server

server.port=9005

## 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

## 读取本地文件,开启此属性时配合客户端的spring.cloud.config.discovery.enabled=true会从本地配置文件中读取属性覆盖git属性,但是本地属性文件更改后不会刷新
# spring.profiles.active=native

## 读取git的路径
# git仓库的地址
 spring.cloud.config.server.git.uri = https://github.com/11500667/springcloud/
# git仓库地址下的相对地址 多个用逗号","分割
spring.cloud.config.server.git.search-paths = /springcloud-config-bus/config-repo
# git仓库的账号
 spring.cloud.config.server.git.username =
# git仓库的密码
 spring.cloud.config.server.git.password =

# management.endpoints.web.exposure.include: bus-refresh
management.endpoints.web.exposure.include=bus-refresh
## bus

spring.cloud.bus.enabled = true
#  失败快速响应
spring.cloud.bus.trace.enabled = true

spring.rabbitmq.host:127.0.0.1
spring.rabbitmq.port:5672
spring.rabbitmq.username:guest
spring.rabbitmq.password:guest

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
        System.out.println("配置中心服务端启动成功!");
    }
}

4、本地属性配置文件

word=hello!!!

五、springcloud-config-bus-client2

1、pom.文件

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>1.0.0</groupId>
        <artifactId>springcloud-config-bus</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>springcloud-config-bus-client2</artifactId>
    <name>springcloud-config-bus-client2</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-config</artifactId>
        </dependency>
        <!-- 添加监控,以便可以刷新服务端配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

    </dependencies>

     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、application.properties

spring.application.name=springcloud-config-bus-client2
server.port=9007

# 不启用安全验证  springboot 1.x使用
# management.security.enabled=false
# 暴露refresh接入点 springboot 2.x使用
management.endpoints.web.exposure.include=refresh,health,info

## bus
spring.cloud.config.failFast=true

spring.cloud.bus.trace.enabled = true

spring.rabbitmq.host:127.0.0.1
spring.rabbitmq.port:5672
spring.rabbitmq.username:guest
spring.rabbitmq.password:guest

3、bootstrap.properties

spring.cloud.config.name=configtest
spring.cloud.config.profile=pro
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=springcloud-config-bus-server

eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

4、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication2 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication2.class, args);
        System.out.println("配置中心第二个客户端启动成功!");
    }
}

5、测试contorller

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope//必须有此注解的类才会刷新修改属性
public class ClientController {

    @Value("${word}")
    private String word;

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return name+","+this.word;
    }

}

六、更新属性后用以下两种方式刷新,提交的请求必须是post方式

1、springcloud-config-bus-server

ip:端口/actuator/bus-refresh

2、springcloud-config-bus-client2

ip:端口/actuator/refresh

原文地址:https://www.cnblogs.com/xiaofengfree/p/10776866.html

时间: 2024-08-29 01:17:22

SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)的相关文章

史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

最新Finchley版本请访问:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f8-bus/或者http://blog.csdn.net/forezp/article/details/81041062 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改. 本文还是基于

SpringCloud教程 | 第十一篇: docker部署spring cloud项目

版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/article/details/70198649 目录(?)[+] 转载请标明出处: http://blog.csdn.net/forezp/article/details/70198649 本文出自方志朋的博客 一.docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可

(十六)JAVA springcloud ssm b2b2c多用户商城系统-使用spring cloud Bus刷新配置

我们使用spring cloud分布式微服务云架构做了b2b2c的电子商务系统,除了架构本身自带的系统服务外,我们将b2b2c的业务服务进行了细粒度拆分,做成了不同的业务微服务. 当我们的业务系统越来越庞大复杂的时候,各种配置也会随之增多.配置文件只要一修改,会对commonservice-config配置中心先停止服务,然后再重新启动,最后使配置生效. 如果服务少,我们可以手动方式来启动,但是对业务和系统的稳定性肯定有一定的影响. 如果是成百上千的服务都靠手动操作,我估计运维人员或技术人员会疯

Spring Cloud 入门教程(十):和RabbitMQ的整合 -- 消息总线Spring Cloud Netflix Bus

在本教程第三讲Spring Cloud 入门教程(三): 配置自动刷新中,通过POST方式向客户端发送/refresh请求, 可以让客户端获取到配置的最新变化.但试想一下, 在分布式系统中,如果存在很多个客户端都需要刷新改配置,通过这种方式去刷新也是一种非常痛苦的事情.那有没有什么办法让系统自动完成呢? 之前我们提到用githook或者jenkins等外部工具来触发.现在说另外一种思路, 如果refresh命令可以发送给config server,然后config server自动通知所有con

第八篇:消息总线(Spring Cloud Bus)

前面几篇文章我们聊了Spring Cloud Config配置中心,当我们在更新Git上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新客户端,客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用Spring Cloud Bus可以完美解决这一问题. 1.消息总线 Spring cloud bus通过轻量消息代理连接各个分布的节点.这会用在广播状态的变化(例如配置变化)或者其他的消息指令.Spring bus的一个核

跟我学SpringCloud | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪

SpringCloud系列教程 | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如无特殊说明,本系列教程全采用以上版本 在分布式服务架构中,需要对分布式服务进行治理--在分布式服务协同向用户提供服务时,每个请求都被哪些服务处理?在遇到问题时,在调用哪个服务上发生了问题?在分析性能时,调用各个服务都花了多长时间?哪些调用可以并行执行?-- 为此,分布式

通过总线机制实现自动刷新客户端配置(Consul,Spring Cloud Config,Spring Cloud Bus)

通过总线机制实现自动刷新客户端配置 方案示意图 利用Git服务的webhook通知功能,在每次更新配置之后,Git服务器会用POST方式调用配置中心的/actuator/bus-refresh接口,配置中心的总线服务会将此事件广播给加入总线的所有客户端,客户端收到事件后会从新读取配置中心的内容. 增加POM依赖 配置中心的服务端(spring-cloud-config-server)和客户端(spring-cloud-config-client)都加入Spring Cloud Bus引用包: <

史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)

最新Finchley版本请访问:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/或者http://blog.csdn.net/forezp/article/details/81040925 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环境简单,可以在开发人员的电脑上跑.另外说明spring cl

跟我学习SpringCloud 教程第三篇:注册中心集群篇-b2b2c小程序电子商务

集群环境搭建?了解springcloud架构可以加求求:三五三六二四七二五九第一步:我们新建两个注册中心工程一个叫eureka_register_service_master.另外一个叫eureka_register_service_backup eureka_register_service_master的application.properties配置如下?server.port=7998 eureka.client.register-with-eureka=false eureka.cli