Hello Spring Cloud Alibaba(七)之使用RocketMq
RocketMq介绍
RocketMQ 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的、高可靠的消息发布与订阅服务。官网地址:https://rocketmq.apache.org/
RocketMq设置
添加topic
rocketMq提供者
导入包
主要导入消息队列依赖,完整pom如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.musuixin</groupId>
<artifactId>hello-alibaba</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>top.musuixin</groupId>
<artifactId>hello-alibaba-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-alibaba-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 表明这是一个spring-cloud应用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 消息队列依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
</dependency>
<!-- dubbo-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>top.musuixin</groupId>
<artifactId>hello-alibaba-common-api</artifactId>
<!-- 版本可以在通用依赖中设置-->
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
设置bootstrap.yml
添加Rocketmq的设置
spring:
cloud:
stream:
rocketmq:
binder:
# RocketMq的地址
name-server: 192.168.0.100:9876
bindings:
# 设置输出到的topic与消息格式
output: {destination: test-topic, content-type: application/json}
nacos:
config:
server-addr: 192.168.0.100:8848
file-extension: yaml
discovery:
server-addr: 192.168.0.100:8848
application:
name: hello-alibaba-provider
server:
port: 8090
dubbo:
protocol:
# 协议名称
name: dubbo
#服务端口
port: 7100
registry:
# nacos注册中心
address: nacos://192.168.0.100:8848
application:
name: hello-alibaba-provider
设置发送消息接口
注入接口
package top.musuixin.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
@SpringBootApplication
@EnableDubbo
@EnableBinding({Source.class})
public class HelloAlibabaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(HelloAlibabaProviderApplication.class, args);
}
}
接口类
package top.musuixin.provider.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author musuixin
* @date 2020-09-11 20:57
*/
@RestController
@RequestMapping("/rocketmq")
public class RocketMqController {
@Autowired
private MessageChannel output;
@GetMapping("/send")
public String send() {
Message message = MessageBuilder.withPayload("hello world").build();
output.send(message);
return "success";
}
}
RocketMq消费者
导入包
与提供者导入的包一样
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.musuixin</groupId>
<artifactId>hello-alibaba</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>top.musuixin</groupId>
<artifactId>hello-alibaba-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-alibaba-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
</dependency>
<dependency>
<groupId>top.musuixin</groupId>
<artifactId>hello-alibaba-common-api</artifactId>
<!-- 版本可以在通用依赖中设置-->
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
设置bootstrap.yml
spring:
cloud:
nacos:
# 远程配置文件
config:
server-addr: 192.168.0.100:8848
file-extension: yaml
# 服务发现
discovery:
server-addr: 192.168.0.100:8848
sentinel:
transport:
dashboard: 192.168.0.100:8858
client-ip: 192.168.0.1
stream:
rocketmq:
binder:
# 设置RocketMq地址
name-server: 192.168.0.100:9876
bindings:
# 设置topic、消息格式、group
input: {destination: test-topic, content-type: application/json,group: test-group}
application:
name: hello-alibaba-consumer
server:
port: 8000
dubbo:
protocol:
name: dubbo
port: 7010
registry:
# nacos注册中心
address: nacos://192.168.0.100:8848
application:
name: hello-alibaba-consumer
设置消息消费类
注入接口
package top.musuixin.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;
@SpringBootApplication
@EnableBinding({Sink.class})
public class HelloAlibabaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(HelloAlibabaConsumerApplication.class, args);
}
}
消费类
package top.musuixin.consumer.service;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* @author musuixin
* @date 2020-09-22 21:31
*/
@Component
public class RocketmqConsumer {
@StreamListener("input")
public void helloRock(String receiveMsg) {
System.err.println(receiveMsg);
}
}
启动
依次启动服务提供者与消费者,访问发送消息接口