【mq】從零開始實現 mq-08-配置優化 fluent 前景回顧 fluent Broker 配置 Producer 配置 Consuemr 配置 小結 開源地址 拓展閱讀

前景回顧

【mq】從零開始實現 mq-01-生產者、消費者啓動

【mq】從零開始實現 mq-02-如何實現生產者調用消費者?

【mq】從零開始實現 mq-03-引入 broker 中間人

【mq】從零開始實現 mq-04-啓動檢測與實現優化

【mq】從零開始實現 mq-05-實現優雅停機

【mq】從零開始實現 mq-06-消費者心跳檢測 heartbeat

【mq】從零開始實現 mq-07-負載均衡 load balance

【mq】從零開始實現 mq-08-配置優化 fluent

fluent

大家好,我是老馬。

fluent 的配置方式,是我個人非常喜歡的一種配置方式。

傳統的 java 使用 get/set 方法進行屬性設置。

類似這種:

MqBroker  mqBroker = new MqBroker();
mqBroker.setPort(9999);
mqBroker.setAddress("127.0.0.1");

fluent 寫法可以讓我們寫起來代碼更加流暢:

MqBroker.newInstance()
.port(9999)
.address("127.0.0.1")

寫起來更加絲滑流暢。

Broker 配置

屬性

/**
 * 端口號
 */
private int port = BrokerConst.DEFAULT_PORT;
/**
 * 調用管理類
 *
 * @since 1.0.0
 */
private final IInvokeService invokeService = new InvokeService();
/**
 * 消費者管理
 *
 * @since 0.0.3
 */
private IBrokerConsumerService registerConsumerService = new LocalBrokerConsumerService();
/**
 * 生產者管理
 *
 * @since 0.0.3
 */
private IBrokerProducerService registerProducerService = new LocalBrokerProducerService();
/**
 * 持久化類
 *
 * @since 0.0.3
 */
private IMqBrokerPersist mqBrokerPersist = new LocalMqBrokerPersist();
/**
 * 推送服務
 *
 * @since 0.0.3
 */
private IBrokerPushService brokerPushService = new BrokerPushService();
/**
 * 獲取響應超時時間
 * @since 0.0.3
 */
private long respTimeoutMills = 5000;
/**
 * 負載均衡
 * @since 0.0.7
 */
private ILoadBalance<ConsumerSubscribeBo> loadBalance = LoadBalances.weightRoundRobbin();
/**
 * 推送最大嘗試次數
 * @since 0.0.8
 */
private int pushMaxAttempt = 3;

flent 配置

public MqBroker port(int port) {
    this.port = port;
    return this;
}

public MqBroker registerConsumerService(IBrokerConsumerService registerConsumerService) {
    this.registerConsumerService = registerConsumerService;
    return this;
}

public MqBroker registerProducerService(IBrokerProducerService registerProducerService) {
    this.registerProducerService = registerProducerService;
    return this;
}

public MqBroker mqBrokerPersist(IMqBrokerPersist mqBrokerPersist) {
    this.mqBrokerPersist = mqBrokerPersist;
    return this;
}

public MqBroker brokerPushService(IBrokerPushService brokerPushService) {
    this.brokerPushService = brokerPushService;
    return this;
}

public MqBroker respTimeoutMills(long respTimeoutMills) {
    this.respTimeoutMills = respTimeoutMills;
    return this;
}

public MqBroker loadBalance(ILoadBalance<ConsumerSubscribeBo> loadBalance) {
    this.loadBalance = loadBalance;
    return this;
}

Producer 配置

屬性

/**
 * 分組名稱
 */
private String groupName = ProducerConst.DEFAULT_GROUP_NAME;
/**
 * 中間人地址
 */
private String brokerAddress  = "127.0.0.1:9999";
/**
 * 獲取響應超時時間
 * @since 0.0.2
 */
private long respTimeoutMills = 5000;
/**
 * 檢測 broker 可用性
 * @since 0.0.4
 */
private volatile boolean check = true;
/**
 * 調用管理服務
 * @since 0.0.2
 */
private final IInvokeService invokeService = new InvokeService();
/**
 * 狀態管理類
 * @since 0.0.5
 */
private final IStatusManager statusManager = new StatusManager();
/**
 * 生產者-中間服務端服務類
 * @since 0.0.5
 */
private final IProducerBrokerService producerBrokerService = new ProducerBrokerService();
/**
 * 爲剩餘的請求等待時間
 * @since 0.0.5
 */
private long waitMillsForRemainRequest = 60 * 1000;
/**
 * 負載均衡策略
 * @since 0.0.7
 */
private ILoadBalance<RpcChannelFuture> loadBalance = LoadBalances.weightRoundRobbin();
/**
 * 消息發送最大嘗試次數
 * @since 0.0.8
 */
private int maxAttempt = 3;

fluent 配置

public MqProducer groupName(String groupName) {
    this.groupName = groupName;
    return this;
}

public MqProducer brokerAddress(String brokerAddress) {
    this.brokerAddress = brokerAddress;
    return this;
}

public MqProducer respTimeoutMills(long respTimeoutMills) {
    this.respTimeoutMills = respTimeoutMills;
    return this;
}

public MqProducer check(boolean check) {
    this.check = check;
    return this;
}

public MqProducer waitMillsForRemainRequest(long waitMillsForRemainRequest) {
    this.waitMillsForRemainRequest = waitMillsForRemainRequest;
    return this;
}

public MqProducer loadBalance(ILoadBalance<RpcChannelFuture> loadBalance) {
    this.loadBalance = loadBalance;
    return this;
}

public MqProducer maxAttempt(int maxAttempt) {
    this.maxAttempt = maxAttempt;
    return this;
}

Consuemr 配置

屬性

/**
 * 組名稱
 */
private String groupName = ConsumerConst.DEFAULT_GROUP_NAME;
/**
 * 中間人地址
 */
private String brokerAddress  = "127.0.0.1:9999";
/**
 * 獲取響應超時時間
 * @since 0.0.2
 */
private long respTimeoutMills = 5000;
/**
 * 檢測 broker 可用性
 * @since 0.0.4
 */
private volatile boolean check = true;
/**
 * 爲剩餘的請求等待時間
 * @since 0.0.5
 */
private long waitMillsForRemainRequest = 60 * 1000;
/**
 * 調用管理類
 *
 * @since 1.0.0
 */
private final IInvokeService invokeService = new InvokeService();
/**
 * 消息監聽服務類
 * @since 0.0.5
 */
private final IMqListenerService mqListenerService = new MqListenerService();
/**
 * 狀態管理類
 * @since 0.0.5
 */
private final IStatusManager statusManager = new StatusManager();
/**
 * 生產者-中間服務端服務類
 * @since 0.0.5
 */
private final IConsumerBrokerService consumerBrokerService = new ConsumerBrokerService();
/**
 * 負載均衡策略
 * @since 0.0.7
 */
private ILoadBalance<RpcChannelFuture> loadBalance = LoadBalances.weightRoundRobbin();
/**
 * 訂閱最大嘗試次數
 * @since 0.0.8
 */
private int subscribeMaxAttempt = 3;
/**
 * 取消訂閱最大嘗試次數
 * @since 0.0.8
 */
private int unSubscribeMaxAttempt = 3;

fluent 配置

public MqConsumerPush subscribeMaxAttempt(int subscribeMaxAttempt) {
    this.subscribeMaxAttempt = subscribeMaxAttempt;
    return this;
}

public MqConsumerPush unSubscribeMaxAttempt(int unSubscribeMaxAttempt) {
    this.unSubscribeMaxAttempt = unSubscribeMaxAttempt;
    return this;
}

public MqConsumerPush groupName(String groupName) {
    this.groupName = groupName;
    return this;
}

public MqConsumerPush brokerAddress(String brokerAddress) {
    this.brokerAddress = brokerAddress;
    return this;
}

public MqConsumerPush respTimeoutMills(long respTimeoutMills) {
    this.respTimeoutMills = respTimeoutMills;
    return this;
}

public MqConsumerPush check(boolean check) {
    this.check = check;
    return this;
}

public MqConsumerPush waitMillsForRemainRequest(long waitMillsForRemainRequest) {
    this.waitMillsForRemainRequest = waitMillsForRemainRequest;
    return this;
}

public MqConsumerPush loadBalance(ILoadBalance<RpcChannelFuture> loadBalance) {
    this.loadBalance = loadBalance;
    return this;
}

小結

這一節的實現非常簡單,可以說是沒有啥技術難度。

只是爲了讓使用者更加方便。

希望本文對你有所幫助,如果喜歡,歡迎點贊收藏轉發一波。

我是老馬,期待與你的下次重逢。

開源地址

The message queue in java.(java 簡易版本 mq 實現) https://github.com/houbb/mq

拓展閱讀

rpc-從零開始實現 rpc https://github.com/houbb/rpc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章