Spring Cloud學(xué)習(xí)day106:消息總線Bus和消息驅(qū)動Stream

一溃卡、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ù)變更卵洗,及時同步的問題请唱。

Clien刷新當(dāng)Service-A獲取到配置信息后會向RabbitMq發(fā)送消息弥咪,mq在通知其他兩個客戶端,獲取最新的配置信息

Server刷新配置中心會向RabbitMQ發(fā)送刷新的通知十绑,RabbitMQ會向所有的客戶端發(fā)送更新的消息聚至,客戶端再從服務(wù)中獲取最新的配置信息

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;
    }
}
  • 測試:


    遠(yuǎn)程倉庫
示例
  • 拷貝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請求:


    刷新任意客戶端發(fā)送post請求

    任意一個客戶端發(fā)送post請求

    服務(wù)端會更新本地倉庫
  • 測試:


    修改遠(yuǎn)程倉庫中的配置信息
示例

示例

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請求:


    示例
使用配置中心的服務(wù)端發(fā)送post請求
  • 測試:

客戶端使用上面的兩個項(xiàng)目亏狰。

修改git中配置的信息

9021

9022

4.實(shí)現(xiàn)局部刷新服務(wù):

使用上面的Server-bus2服務(wù)和兩個客戶端測試役纹。

1.刷新指定服務(wù):

使用httpClientUtil工具類發(fā)送post請求。
http://Config-Server/bus/refresh?destination=需要刷新的服務(wù)名稱:端口 ”暇唾。

  • 修改遠(yuǎn)程倉庫中的配置信息:


    示例
  • 使用httpClientUtil工具類:


    示例
  • 測試:


    9021

    9022

2.刷新指定集群:

使用工具類發(fā)送post請求促脉;
"http://Config-Server/bus/refresh?destination=需要刷新的服務(wù)名稱:**"。

  • 修改遠(yuǎn)程倉庫中配置信息:


    示例
  • 使用工具類發(fā)送post請求:


    示例
  • 測試:


    9022

    9021

二策州、消息驅(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);
    }
}
示例

示例
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末旁仿,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子孽糖,更是在濱河造成了極大的恐慌枯冈,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梭姓,死亡現(xiàn)場離奇詭異霜幼,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)誉尖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铸题,“玉大人铡恕,你說我怎么就攤上這事《洌” “怎么了探熔?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長烘挫。 經(jīng)常有香客問我诀艰,道長柬甥,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任其垄,我火速辦了婚禮苛蒲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘绿满。我一直安慰自己臂外,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布喇颁。 她就那樣靜靜地躺著漏健,像睡著了一般。 火紅的嫁衣襯著肌膚如雪橘霎。 梳的紋絲不亂的頭發(fā)上蔫浆,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機(jī)與錄音姐叁,去河邊找鬼克懊。 笑死,一個胖子當(dāng)著我的面吹牛七蜘,可吹牛的內(nèi)容都是我干的谭溉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼橡卤,長吁一口氣:“原來是場噩夢啊……” “哼扮念!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起碧库,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤柜与,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后嵌灰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體弄匕,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年沽瞭,在試婚紗的時候發(fā)現(xiàn)自己被綠了迁匠。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡驹溃,死狀恐怖城丧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情豌鹤,我是刑警寧澤亡哄,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站布疙,受9級特大地震影響蚊惯,放射性物質(zhì)發(fā)生泄漏愿卸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一截型、第九天 我趴在偏房一處隱蔽的房頂上張望趴荸。 院中可真熱鬧,春花似錦菠劝、人聲如沸赊舶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽笼平。三九已至,卻和暖如春舔痪,著一層夾襖步出監(jiān)牢的瞬間寓调,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工锄码, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留夺英,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓滋捶,卻偏偏與公主長得像痛悯,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子重窟,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評論 2 353