消息總線

SpringCloudBus:事件、消息總線,用於在集羣(例如,配置變化事件)中傳播狀態變化,可與Spring Cloud Config聯合實現熱部署。java

在上一篇寫出了springcloud對微服務的集中配置,那麼就出現了一個問題,若是修改配置了怎麼實現不需重啓服務來實現配置的更新,下面有集中解決方法。git

1.使用/refresh手動刷新配置web

缺點:單點刷新,若是集羣服務多的話,不管是工做量仍是維護上都十分麻煩。spring

使用上一篇的config-client服務,加入依賴,apache

spring-boot-starter-actuator
複製代碼

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>com.dalaoyang</groupId>
	<artifactId>springcloud_config_client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springcloud_config_client</name>
	<description>springcloud_config_client</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</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>Edgware.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</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>

	</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>
複製代碼

在Controller類上加入@RefreshScope註解,因爲上一篇controller寫在了啓動類上,因此直接加在啓動類上,代碼以下:bash

package com.dalaoyang;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RefreshScope
public class SpringcloudConfigClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudConfigClientApplication.class, args);
	}


	@Value("${title}")
	String title;

	@RequestMapping("/getTitle")
	public String getTitle(){
		return title;
	}

}
複製代碼

配置文件新增配置management.security.enabled=false,在刷新時關閉安全驗證。app

代碼以下:maven

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=test
spring.cloud.config.uri= http://localhost:9000/

##註冊中心地址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/

## 刷新時,關閉安全驗證
management.security.enabled=false
複製代碼

分別啓動項目config-server,config-client。訪問http://localhost:8080/getTitle,結果以下圖spring-boot

修改git上配置,修改成dalaoyang_test_change,在次請求,結果沒有改變,使用postman或者其餘工具post請求http://localhost:8080/getTitle看到返回以下結果。

在次訪問http://localhost:8080/getTitle,以下圖

2.使用springcloudbus刷新配置

springcloudbus須要使用輕量消息代理,本文使用rabbitmq,啓動rabbitmq以下圖:

訪問http://localhost:15672/#/以下圖

新建項目springcloud_bus,同時改造config-client,pom文件加入bus依賴,代碼以下:

<?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.dalaoyang</groupId>
	<artifactId>springcloud_bus</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springcloud_bus</name>
	<description>springcloud_bus</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</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>Edgware.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</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>
複製代碼

加入rabbitmq配置,配置文件以下:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

##端口號
server.port=8881

## 刷新時,關閉安全驗證
management.security.enabled=false

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=test
spring.cloud.config.uri= http://localhost:9000/

##註冊中心地址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/
複製代碼

啓動類加入註解@RefreshScope,代碼以下:

package com.dalaoyang;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RefreshScope
public class SpringcloudBusApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudBusApplication.class, args);
	}


	@Value("${title}")
	String title;

	@RequestMapping("/getTitle")
	public String getTitle(){
		return title;
	}
}
複製代碼

config-client加入一樣依賴和配置,重啓config-client,啓動springcloud_bus,先去eureka.dalaoyang.cn/,能夠看到

先將git上配置改回dalaoyang_test,分別請求http://localhost:8881/getTitle和http://localhost:8080/getTitle結果以下:

而後用postman使用post請求訪問http://localhost:8881/bus/refresh

再次分別請求http://localhost:8881/getTitle和http://localhost:8080/getTitle結果以下:

從圖中能夠看出刷新配置成功。

3.局部刷新配置,配置與第2種方法同樣,只是在使用postman刷新時略加改變,在本文中使用http://localhost:8881/bus/refresh?destination=config-client:8881能夠刷新服務名爲config-client端口爲8881的服務,若是想要刷新服務名爲config-client的全部服務能夠寫成http://localhost:8881/bus/refresh?destination=config-client:**

源碼下載 :大老楊碼雲

我的網站:www.dalaoyang.cn

相關文章
相關標籤/搜索