Outline
RocketMQ is an open source distributed messaging system, distributed cluster-based high availability technologies to provide low-latency, highly reliable publish and subscribe messaging services.
Because this tutorial entire case based on Spring Cloud, so we use the Spring Cloud Stream complete a publish and subscribe
#Spring Cloud Stream
Spring Cloud Stream is used to build a micro-frame message-based service applications. Spring it to create stand-alone applications with production levels based on Spring Boot, and use Spring Integration
to connect to the Broker.
Spring Cloud Stream provides a unified messaging middleware abstraction configured, launched publish-subscribe
, consumer groups
, partition
these unified concept.
Internal Spring Cloud Stream There are two concepts:
- Binder: with external messaging middleware integrated components for creating Binding, each has its own messaging middleware Binder realized.
- Binding: 包括 Input Binding 和 Output Binding。
Binding provides a bridge between an application and the messaging middleware provided Provider and Consumer, the developer only needs to use to achieve the Provider or the Consumer application to production or consumption data, the developer shield underlying messaging middleware contact.
# Resolve connection timeout problem
Before the basis of Docker installation RocketMQ chapter, we use the Docker deployed RocketMQ service, this time RocketMQ Broker exposed address and port (10909,10911) is a container, it will lead us to develop machine can not connect, causing org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException: sendDefaultImpl call timeout
abnormal
Note that the IP address in the following figure, this is IP, container and container development machine is not connected to a local area network so I can not.
The solution is to broker.conf
increase the profile brokerIP1=宿主机IP
to
#POM
The main increases org.springframework.cloud:spring-cloud-starter-stream-rocketmq
dependence
<?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>com.snake</groupId>
<artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-alibaba-rocketmq-provider</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-alibaba-rocketmq-provider</name>
<url>http://www.snake.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.snake.hello.spring.cloud.alibaba.rocketmq.provider.RocketMQProviderApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
# News producer services
package com.snake.hello.spring.cloud.alibaba.rocketmq.provider.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
@Service
public class ProviderService {
@Autowired
private MessageChannel output;
public void send(String message) {
output.send(MessageBuilder.withPayload(message).build());
}
}
#Application
Configuring the Output ( Source.class
) Binding of information and with the @EnableBinding
notes to take effect
package com.funtl.hello.spring.cloud.alibaba.rocketmq.provider;
import com.funtl.hello.spring.cloud.alibaba.rocketmq.provider.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
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
@EnableBinding({Source.class})
public class RocketMQProviderApplication implements CommandLineRunner {
@Autowired
private ProviderService providerService;
public static void main(String[] args) {
SpringApplication.run(RocketMQProviderApplication.class, args);
}
/**
* 实现了 CommandLineRunner 接口,只是为了 Spring Boot 启动时执行任务,不必特别在意
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
providerService.send("Hello RocketMQ");
}
}
# application.yml
spring:
application:
name: rocketmq-provider
cloud:
stream:
rocketmq:
binder:
# RocketMQ 服务器地址
namesrv-addr: 192.168.10.149:9876
bindings:
# 这里是个 Map 类型参数,{} 为 YAML 中 Map 的行内写法
output: {destination: test-topic, content-type: application/json}
server:
port: 9093
management:
endpoints:
web:
exposure:
include: '*'
After a successful run in RocketMQ can console 消息
selection list test-topic
topic to see the message sent