一溃卡、Spring Cloud Bus介紹
1.什么是Spring Cloud Bus沮焕?
Spring Cloud Bus集成了市面上常用的消息代理等对室,連接微服務(wù)系統(tǒng)中所有的節(jié)點(diǎn)参滴,當(dāng)有數(shù)據(jù)變更時强岸,可以通過消息廣播通知服務(wù)及時變更數(shù)據(jù)。
- 解決了什么問題:
解決了微服務(wù)數(shù)據(jù)變更卵洗,及時同步的問題请唱。
2.使用Bus實(shí)現(xiàn)自動刷新配置信息-Client刷新:
需要安裝RabbitMq;http://www.reibang.com/p/da5b3b224db2
-
創(chuàng)建配置中心Server服務(wù):
- 修改POM文件:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
- 修改配置文件:
spring.application.name=config-server
server.port=9020
#設(shè)置服務(wù)注冊中心地址
eureka.client.serviceUrl.defaultZone=http://admin:123456@eureka1:8761/eureka/,http://admin:123456@eureka2:8761/eureka/
#Git 配置
spring.cloud.config.server.git.uri=https://gitee.com/zwzy/config
#Git私有倉庫的用戶名和密碼
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
- 修改啟動類:
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
-
創(chuàng)建客戶端項(xiàng)目:
- 修改POM文件:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 修改配置文件:
spring.application.name=config-client
server.port=9022
#設(shè)置服務(wù)注冊中心地址
eureka.client.serviceUrl.defaultZone=http://admin:123456@eureka1:8761/eureka/,http://admin:123456@eureka2:8761/eureka/
#默認(rèn) false,這里設(shè)置 true,表示開啟讀取配置中心的配置
spring.cloud.config.discovery.enabled=true
#對應(yīng) eureka 中的配置中心 serviceId本橙,默認(rèn)是configserver
spring.cloud.config.discovery.serviceId=config-server
#指定環(huán)境
spring.cloud.config.profile=dev
#git標(biāo)簽 分支/主干
spring.cloud.config.label=master
#springboot 默認(rèn)開啟了權(quán)限攔截 會導(dǎo)致 /refresh 出現(xiàn) 401扳躬,拒絕訪問
management.security.enabled=false
#消息隊列的鏈接配置
spring.rabbitmq.host=192.168.226.129
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtualHost=/
- 修改啟動類:
@EnableEurekaClient
@SpringBootApplication
public class RefreshApplication {
public static void main(String[] args) {
SpringApplication.run(RefreshApplication.class, args);
}
}
- 創(chuàng)建Controller:
@RestController
@RefreshScope//刷新作用域
public class ConfigClientController {
@Value("${e-book}")
private String msg;
@RequestMapping("/show")
public String ShowMsg() {
return this.msg;
}
}
-
測試:
-
拷貝Bus客戶端項(xiàng)目:
- 修改配置文件,更改端口號:
spring.application.name=config-client
server.port=9021
#設(shè)置服務(wù)注冊中心地址
eureka.client.serviceUrl.defaultZone=http://admin:123456@eureka1:8761/eureka/,http://admin:123456@eureka2:8761/eureka/
#默認(rèn) false,這里設(shè)置 true,表示開啟讀取配置中心的配置
spring.cloud.config.discovery.enabled=true
#對應(yīng) eureka 中的配置中心 serviceId甚亭,默認(rèn)是configserver
spring.cloud.config.discovery.serviceId=config-server
#指定環(huán)境
spring.cloud.config.profile=dev
#git標(biāo)簽 分支/主干
spring.cloud.config.label=master
#springboot 默認(rèn)開啟了權(quán)限攔截 會導(dǎo)致 /refresh 出現(xiàn) 401贷币,拒絕訪問
management.security.enabled=false
#消息隊列的鏈接配置
spring.rabbitmq.host=192.168.226.129
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtualHost=/
-
使用HttpClientUtil工具發(fā)送post請求:
-
測試:
3.使用Bus實(shí)現(xiàn)自動刷新配置信息-Server 刷新:
-
創(chuàng)建服務(wù)端項(xiàng)目:
- 修改POM文件:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
- 修改配置文件:
spring.application.name=config-server
server.port=9020
#設(shè)置服務(wù)注冊中心地址
eureka.client.serviceUrl.defaultZone=http://admin:123456@eureka1:8761/eureka/,http://admin:123456@eureka2:8761/eureka/
#Git 配置
spring.cloud.config.server.git.uri=https://gitee.com/zwzy/config
#Git私有倉庫的用戶名和密碼
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
#springboot 默認(rèn)開啟了權(quán)限攔截 會導(dǎo)致 /refresh 出現(xiàn) 401,拒絕訪問
management.security.enabled=false
spring.rabbitmq.host=192.168.226.129
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtualHost=/
- 修改啟動類:
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
-
使用httpClientUtil工具發(fā)送post請求:
- 測試:
客戶端使用上面的兩個項(xiàng)目亏狰。
4.實(shí)現(xiàn)局部刷新服務(wù):
使用上面的Server-bus2服務(wù)和兩個客戶端測試役纹。
1.刷新指定服務(wù):
使用httpClientUtil工具類發(fā)送post請求。
“http://Config-Server/bus/refresh?destination=需要刷新的服務(wù)名稱:端口 ”暇唾。
-
修改遠(yuǎn)程倉庫中的配置信息:
-
使用httpClientUtil工具類:
-
測試:
2.刷新指定集群:
使用工具類發(fā)送post請求促脉;
"http://Config-Server/bus/refresh?destination=需要刷新的服務(wù)名稱:**"。
-
修改遠(yuǎn)程倉庫中配置信息:
-
使用工具類發(fā)送post請求:
-
測試:
二策州、消息驅(qū)動入門案例
1.創(chuàng)建消息的發(fā)送者:
- 修改POM文件:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 修改配置文件:
spring.application.name=config-server
server.port=9020
#設(shè)置服務(wù)注冊中心地址
eureka.client.serviceUrl.defaultZone=http://admin:123456@eureka1:8761/eureka/,http://admin:123456@eureka2:8761/eureka/
#消息隊列的鏈接配置
spring.rabbitmq.host=192.168.226.129
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtualHost=/
- 創(chuàng)建發(fā)送消息的接口:
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.SubscribableChannel;
public interface ISendeService {
@Output("user-exchange")
SubscribableChannel send();
}
- 修改啟動類:
@EnableBinding(value = ISendeService.class)
@EnableEurekaClient
@SpringBootApplication
public class ISendeApplication {
public static void main(String[] args) {
SpringApplication.run(ISendeApplication.class, args);
}
}
2.創(chuàng)建消息的接受者:
- 修改POM文件:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
</dependencies>
- 修改配置文件:
spring.application.name=config-server
server.port=9021
#設(shè)置服務(wù)注冊中心地址
eureka.client.serviceUrl.defaultZone=http://admin:123456@eureka1:8761/eureka/,http://admin:123456@eureka2:8761/eureka/
#消息隊列的鏈接配置
spring.rabbitmq.host=192.168.226.129
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtualHost=/
- 創(chuàng)建接收消息的接口:
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;
public interface IReceiveService {
@Input("user-exchange")
SubscribableChannel receiver();
}
- 創(chuàng)建處理消息的類:
@Service
@EnableBinding({IReceiveService.class})
public class ReceiverService {
@StreamListener("user-exchange")
public void onReciver(byte[] msg) {
//處理消息
System.out.println("Receiver:"+new String(msg));
}
}
- 修改啟動類:
@EnableBinding(value = IReceiveService.class)
@EnableEurekaClient
@SpringBootApplication
public class IReceiverApplication {
public static void main(String[] args) {
SpringApplication.run(IReceiverApplication.class, args);
}
}
- 測試:
在消息發(fā)送的項(xiàng)目中創(chuàng)建測試類瘸味;依次啟動項(xiàng)目和執(zhí)行測試類。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ISendeApplication.class)
public class Test {
@Autowired
private ISendeService isendeService;
@org.junit.Test
public void testSend() {
String msg ="Sende發(fā)送的消息够挂!";
//將消息封裝成Message
Message message = MessageBuilder.withPayload(msg.getBytes()).build();
isendeService.send().send(message);
}
}