# 负载均衡

## 负载均衡

RocketMQ Connect负载均衡跟RocketMQ Consumer和Queue之间的负载均衡类似，都是每个节点运行相同的负责均衡算法计算本节点应该处理的Task。

RocketMQ Connect集群中如果有节点挂掉，集群信息会发生变化，每个节点检查到集群信息发生了变化就会出发负载均衡，对集群中运行的Connector， Task重新分配，从而保证故障节点的任务分配到其它接单处理。

### 一致性hash负载均衡算法

RocketMQ Runtime的一致性Hash算法作用是负载均衡是不会对运行中的connector任务再次影响。

RocketMQ Runtime实现了通过设置JVM来设置一致性hash负载均衡算法，具体参数请参考

{% content-ref url="/pages/-LtPpprmC7uYQOZtEan-" %}
[Runtime 参数配置](/rocketmq-connector/rocketmq-connect/rocketmq-runtime/can-shu-pei-zhi.md)
{% endcontent-ref %}

## 在runtime中配置负载均衡算法

### 使用说明

1\.  在`rocketmq-connect-runtime/target/distribution/conf/connect.conf`中配置 `allocTaskStrategy`参数

```
## Http prot for user to access REST API
httpPort=8081

# Rocketmq namesrvAddr
# namesrv地址相同即为一个runtime集群
namesrvAddr=4xx.1xx.2xx.1xx:9876

# Source or sink connector jar file dir,The default value is rocketmq-connect-sample
pluginPaths=/usr/local/connector-plugins/

# 本地存储路径(如果多个Worker在一个上，需要文件夹不同)
storePathRootDir=/Users/yongqifeng/connectorStore/

# 负载均衡策略
allocTaskStrategy=org.apache.rocketmq.connect.runtime.service.strategy.AllocateConnAndTaskStrategyByConsistentHash

```

2\.  在`run_worker.sh` 添加jvm参数 `virtualNode`

```
#!/bin/bash
echo "run rumtime worker"
cd target/distribution/ && java -DvirtualNode=2 -cp .:./conf/:./lib/* org.apache.rocketmq.connect.runtime.ConnectStartup -c conf/connect.conf
```

3\. 重复上面的1和2步骤多个Worker，然后启动

```
The worker [192.168.1.115@34974] boot success.
The worker [192.168.1.115@34915] boot success.
```

4\. 看到多个Worker正常运行，请求<http://localhost:8081/getClusterInfo>，看到如下信息说明集群启动成功。

```
# 结果返回
["192.168.1.115@34974","192.168.1.115@34915"]
```

### 集群搭建部署方式

* 一个机器上运行多个runtime（方便测试使用）
* 多个机器，每个机器部署一个runtime


---

# 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://rocketmq-1.gitbook.io/rocketmq-connector/rocketmq-connect/rocketmq-runtime/fu-zai-jun-heng.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.
