# RocketMQ单机环境

## 单机环境搭建

### 依赖环境

1. 64 位操作系统，**Linux/ Unix/Mac OS**
2. **64位JDK 1.8+**
3. **Maven 3.2.x**
4. **Git**

### 代码下载

从github上clone代码 [**rocketmq**](https://github.com/apache/rocketmq)

```bash
> git clone https://github.com/apache/rocketmq.git
```

### **项目构建**

```bash
> cd rocketmq
# meavn打包
> mvn -Prelease-all -DskipTests clean install -U
# 这里的4.5.x是版本号，可能不同，请注意自己的版本
> cd distribution/target/rocketmq-4.5.x/rocketmq-4.5.x
```

### 启动 NameServer

```bash
# 启动NameServer
> nohup sh bin/mqnamesrv &
# 查看日志，确认是否成功
> tail -f ~/logs/rocketmqlogs/namesrv.log

2019-9-27 21:04:10 INFO NSScheduledThread1 - ----
...
#出现下面信息成功
  The Name Server boot success...

```

### 启动 Broker

```bash
# 启动Broker
> nohup sh bin/mqbroker -n localhost:9876 &
# 查看日志，确认是否成功
> tail -f ~/logs/rocketmqlogs/broker.log 
  The broker[%s, 172.30.30.233:10911] boot success...
```

{% hint style="warning" %}
**无法正常启动时，尝试下面命令可看到详细错误信息**

> sh bin/mqnamesrv&#x20;

> sh bin/mqbroker -n localhost:9876
> {% endhint %}

### 发送和接收消息

#### 生产者发送消息

```bash
 > export NAMESRV_ADDR=localhost:9876
 > sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
 # 出现下面信息成功发送
  SendResult [sendStatus=SEND_OK, msgId= ...
```

#### &#x20;消费者消费消息

```bash
> sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
# 出现下面信息成功消费
 ConsumeMessageThread_%d Receive New Messages: [MessageExt...
```

## 关闭服务

```bash
# 关闭 broker
> sh bin/mqshutdown broker
The mqbroker(36695) is running...
Send shutdown request to mqbroker(36695) OK

# 关闭 namesrv
> sh bin/mqshutdown namesrv
The mqnamesrv(36664) is running...
Send shutdown request to mqnamesrv(36664) OK
```

###

## 参考文档：

1. [RocketMQ中文文档](http://rocketmq.cloud/zh-cn/docs/concept.html)
2. [RocketMQ English Documents](https://rocketmq.apache.org/docs/quick-start/)
3. [集群搭建](http://rocketmq.cloud/zh-cn/docs/operation-deployment.html)


---

# 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/quick-start/qian-qi-zhun-bei/dan-ji-huan-jing.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.
