maven工程springboot-rocketmq-producer
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<groupId>com.itheima.springboot</groupId>
<artifactId>springboot-rocketmq-producer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<rocketmq-spring-boot-starter-version>2.0.3</rocketmq-spring-boot-starter-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>${rocketmq-spring-boot-starter-version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.properties
rocketmq.name-server=192.168.86.135:9876
rocketmq.producer.group=my-group
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MQSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MQSpringBootApplication.class);
}
}
测试类
import cn.itcast.MQSpringBootApplication;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MQSpringBootApplication.class})
public class ProducerTest {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Test
public void test1(){
rocketMQTemplate.convertAndSend("springboot-mq","hello springboot rocketmq");
}
}
springboot-rocketmq-consumer
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima.springboot</groupId>
<artifactId>springboot-rocketmq-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<rocketmq-spring-boot-starter-version>2.0.3</rocketmq-spring-boot-starter-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>${rocketmq-spring-boot-starter-version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.properties
rocketmq.name-server=192.168.86.135:9876
rocketmq.producer.group=my-group
package cn.itcast.mq;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RocketMQMessageListener(topic = "springboot-mq",consumerGroup = "springboot-mq-consumer-1")
public class Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("接收到消息"+message);
log.info("Receive message:"+message);
}
}
package cn.itcast;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MQSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MQSpringBootApplication.class);
}
}
2022-10-04 22:27:21.536 INFO 40552 --- [ main] cn.itcast.MQSpringBootApplication : Started MQSpringBootApplication in 2.97 seconds (JVM running for 3.563)
接收到消息hello springboot rocketmq
2022-10-04 22:29:23.361 INFO 40552 --- [MessageThread_1] cn.itcast.mq.Consumer : Receive message:hello springboot rocketmq
zookeeper集群搭建
1.在/usr/soft/zookeeper-cluster下存放zookeeper-3.4.6.tar.gz
2.解压:tar -zvxf zookeeper-3.4.6.tar.gz
3./usr/soft/zookeeper-cluster/zookeeper-3.4.6/conf
下重命名 zoo_sample.cfg为zoo.cfg
mv zoo_sample.cfg zoo.cfg
4.创建三个文件夹
[root@java zookeeper-cluster]# mkdir zookeeper-1
[root@java zookeeper-cluster]# mkdir zookeeper-2
[root@java zookeeper-cluster]# mkdir zookeeper-3
[root@java zookeeper-cluster]# ls
zookeeper-1 zookeeper-2 zookeeper-3 zookeeper-3.4.6 zookeeper-3.4.6.tar.gz
5.将解压后的Zookeeper复制到以下三个目录
[root@java zookeeper-cluster]# cp -rf zookeeper-3.4.6 zookeeper-1
[root@java zookeeper-cluster]# cp -rf zookeeper-3.4.6 zookeeper-2
[root@java zookeeper-cluster]# cp -rf zookeeper-3.4.6 zookeeper-3
6.
配置每一个 Zookeeper 的 dataDir(zoo.cfg) clientPort 分别为 2181 2182 2183
修改zoo.cfg
clientPort=2181
dataDir=/usr/soft/zookeeper-cluster/zookeeper-1/data
修改zoo.cfg
clientPort=2182
dataDir=/usr/soft/zookeeper-cluster/zookeeper-2/data
修改zoo.cfg文章来源:https://www.toymoban.com/news/detail-426016.html
clientPort=2183
dataDir=/usr/soft/zookeeper-cluster/zookeeper-3/data
········文章来源地址https://www.toymoban.com/news/detail-426016.html
到了这里,关于黑马程序员rocketmq第二章的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!