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
SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)的更多相关文章
- 原 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f8-bus/ 本文出自方志朋的博客 转载请标明出处: Spr ...
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc08-bus/ 本文出自方志朋的博客 最新Finchley版本请 ...
- 【SpringCloud 】第八篇: 消息总线(Spring Cloud Bus)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- SpringCloud学习(八)消息总线(Spring Cloud Bus)(Finchley版本)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...
- SpringCloud 教程 (一) 消息总线(Spring Cloud Bus)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...
- 第七篇: 消息总线(Spring Cloud Bus)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...
- springCloud学习-消息总线(Spring Cloud Bus)
1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...
- 通过消息总线Spring Cloud Bus实现配置文件刷新(使用Kafka或RocketMQ)
如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用 ...
- 一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)
上一章节,我们讲解了分布式配置中心spring cloud config,我们把配置项存放在git或者本地,当我们修改配置时,需要重新启动服务才能生效.但是在生产上,一个服务部署了多台机器,重新启动比 ...
随机推荐
- python setuptools
在安装python依赖库时,我们使用pip install 或者python setup.py install. pip 会自己搜索适合的版本,python setup.py 需要下载源码本地安装.但 ...
- IDEA配置github并上传项目
https://www.cnblogs.com/jinjiyese153/p/6796668.html
- GitHub:本地项目上传与团队协作
第一部分:我的本次作业成果 我自己个人的github地址是:colintz的个人仓库 我们开发团队小组的github地址是:小组3集中营 第二部分:强烈推荐的github资源 对于和我一样,初次接触g ...
- 解决微信小程序wepy真机预览跟本地表现不一样,数据变化了视图没变化
当时搜了很多相关问题都没找到相似的 只看到有这个相似的描述wepy在onLoad里修改data-object的值页面不渲染 ,通过setData解决的. 但是这个还不是根本的解决办法,有些地方用set ...
- element-UI使用中:el-input type为textarea时@change无法触发?
自己瞎尝试解决了的.官方文档上居然没写@input事件,醉了.
- STL--hashtable
hashtable使用开链的方式,解决元素个数大于array容量的问题. 当两个不同元素hash得到相同的hash值时,此时我们使用bucket list来链接连个元素. hashtable迭代器必须 ...
- linux 安装配置 sublime 进行 python 开发
1. 下载sublime 地址:http://www.sublimetext.com/3 2. 解压出来,将sublime_text_3 文件夹的名字改为 sublime_text , 然后将 sub ...
- css悬浮提示框
效果图: code: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 【转载 | 笔记】IIS无法删除应该程序池 因为它包含X个应用程序
IIS无法删除应该程序池 因为它包含X个应用程序 今天代码主分支在vs2015创建了虚拟目录http://localhost/webapp指向的物理路径是E:\webapp 之后新开了一个分支把代码放 ...
- MySQL-mysql 8.0.11安装教程
网上的教程有很多,基本上大同小异.但是安装软件有时就可能因为一个细节安装失败.我也是综合了很多个教程才安装好的,所以本教程可能也不是普遍适合的. 安装环境:win7 1.下载zip安装包: MySQL ...