# Broker 到底是什麼？

根據POSA\[1]一書中給的Borker的定義：

> The Broker architectural pattern can be used to structure distributed software systems with decoupled components that interact by remote service invocations. **A broker component Is responsible for coordinating communication**, such as forwarding requests. as well as for transmitting results and exceptions.

**Broker 其實就是一種專門設計用來傳遞、聯絡各component 的一種軟體架構**。主要分成三個主體client、Broker、servers，有別於client-server架構，**Client觸發服務實質上是透過Broker(中間人)。**

Broker 誕生是為了應付一個持續增長的系統，然而為了維護性、安全性、彈性等原因才誕生出來的。而主要遇到的困難是：**1. 系統內的component能夠互相的溝通、2.系統運行時，component可以動態地被調整(上架、下架)、3.對client端隱藏後端的service.**

> Broker 特性 hint: 紀錄client與server的狀態，並且對client隱藏server 所具有的服務

**靜態視角(proxy可直接併入client、server ; Bridge可併入Broker )** 從Class diagram 來看Broker，大致上切分為三個角色，client、Broker、servers。彼此之間傳遞透過proxy，而broker 與其他broker則是透過bridge傳遞。

![Broker architectural class diagram](https://cdn-images-1.medium.com/max/1200/1*Xp1gnt6N83vaV99XXAXDYA.png)

**動態視角(下圖將proxy合併進client、server)** 首先Server 先將自己註冊進入Broker ，而Broker會將 service存取方式保留repo.中。

![img](https://cdn-images-1.medium.com/max/800/1*ymNZflz3WNspCfq_26I03g.png)

當Client透過proxy向Broker觸發某一個服務，Broker 再向Server取要結果後回傳給Client。

![](https://cdn-images-1.medium.com/max/800/1*iZS8nCsnkVBNwgspD8J2sg.png)

**總結** 市面上有很多種broker，而且有很多的變型。但總而言之，Broker 是用來幫忙client觸發服務的，並具有「紀錄client與server的狀態，且對client隱藏server 所具有的服務」的特性！。

**參考資料** \
\[1] Frank, Buschmann, et al. “Pattern-oriented software architecture: a system of patterns.” *Wiley, ISBN 0* 471.95889 (1996)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiang753017.gitbook.io/zixiang-blog/broker/broker-dao-di-shi-shen-me.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
