一、生产者:
1、引入相关依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cmj</groupId>
<artifactId>CmjMQ1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CmjMQ1</name>
<description>CmjMQ1</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
</project>
2、配置文件:
server.port=9003
## rocketmq
rocketmq.namesrv.addr=127.0.0.1:9876
rocketmq.producer.groupName=PRODUCER_GROUP_CMJ
rocketmq.topic=CSDN
rocketmq.order.topic=CSDN_ORDER
#topic
cmjcmj=cmj--doyoukonw
aaaa=cmjcmjaaa
3、配置生产者Bean
package com.cmj.config;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CmjMQConfig {
@Value("${rocketmq.producer.groupName}")
private String producerGroupName;
@Value("${rocketmq.namesrv.addr}")
private String nameServerAddress;
@Bean(initMethod = "start", destroyMethod = "shutdown")
public DefaultMQProducer producer() {
DefaultMQProducer producer = new DefaultMQProducer(producerGroupName);
//VipChannel阿里内部使用版本才用,开源版本没有,默认为true,占用10909端口,此时虚拟机需要开放10909端口,否则会报 :connect to <:10909> failed异常,可以直接设置为false
//producer.setVipChannelEnabled(false);
producer.setNamesrvAddr(nameServerAddress);
return producer;
}
}
4、发送消息,在需要发送至MQ的位置,调用或新增下方代码
package com.cmj.service;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class CmjMQService {
@Value("${cmjcmj}")//group组
private String cmjcmj;
@Autowired
private DefaultMQProducer producer;
public void send(String a) {
SendResult result=null;
try {
Message message = new Message();
message.setTopic(cmjcmj);
message.setBody(a.getBytes());
result=producer.send(message);
} catch (Exception e) {
// TODO: handle exception
}
}
}
二、消费者:
1、相关pom文件依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cmj</groupId>
<artifactId>CmjMQ2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CmjMQ2</name>
<description>CmjMQ2</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
</project>
2、