快速使用
先写一个8801消息的提供者。
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.meng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-stream-rabbitmq-provider8801</artifactId>
<dependencies>
<!--web-->
<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>
<!--devtool-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring cloud stream rabbit-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
</dependencies>
</project>
然后是application.yml配置
server:
port: 8801
spring:
application:
name: cloud-stream-provider
cloud:
stream:
bindings: #服务整合处理
output: #名字是通道名称
destination: studyExchange #表示要使用的Exchange名称定义
content-type: application/json #配置消息类型
default-binder: defaultRabbit #配置要绑定的消息服务的具体设置
binders: #配置要绑定的rabbitmq服务信息
defaultRabbit: #定义的名称,用于binding整合
type: rabbit #消息组件类型
environment: #配置rabbitmq相关环境
spring:
rabbitmq:
host: 120.98.127.59
port: 5672
username: guest
password: guest
virtual-host: /
rabbitmq:
host: 120.98.127.59
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
instance:
instance-id: send-8801 #修改主机名
prefer-ip-address: true #访问路径可以显示IP地址
注意,这里有两个rabbitmq标签,但是后面一个rabbitmq其实可以省略,省略后会报Rabbit health check failed异常,但是该异常不影响使用。
然后,简单写一个业务逻辑IMessageProvider接口的服务实现IMessageProviderImpl.java
package com.meng.service.impl;
import com.meng.service.IMessageProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import javax.annotation.Resource;
import java.util.UUID;
/**
* @author Administrator
*/
@Slf4j
@EnableBinding(Source.class) //定义消息的推送管道
public class IMessageProviderImpl implements IMessageProvider {
/**
* 消息发送管道
*/
@Resource
private MessageChannel output;
@Override
public String send() {
String serial = UUID.randomUUID().toString();
output.send(MessageBuilder.withPayload(serial).build());
log.info("****serial:"+serial);
return null;
}
}
注意这里使用的注解,不需要在业务上写@Service注解,这与普通的业务不同。
然后是主启动类
package com.meng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author Administrator
*/
@SpringBootApplication
@EnableEurekaClient
public class StreamMQMain8801 {
public static void main(String[] args) {
SpringApplication.run(StreamMQMain8801.class,args);
}
}
最后加一个controller层
package com.meng.controller;
import com.meng.service.IMessageProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author Administrator
*/
@RestController
public class SendMassageController {
@Resource
private IMessageProvider messageProvider;
@GetMapping(value = "/sendMessage")
public String sendMessage(){
return messageProvider.send();
}
}
启动8801端口后浏览器访问http://localhost:8801/sendMessage即可。
复杂一点,搭建一个消息的消费者。
消费者
消费者8802端口的搭建。
首先,是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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.meng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-stream-rabbitmq-consumer8802</artifactId>
<dependencies>
<!--web-->
<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>
<!--devtool-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring cloud stream rabbit-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
</dependencies>
</project>
然后,application.yml
server:
port: 8802
spring:
application:
name: cloud-stream-consumer
cloud:
stream:
bindings: #服务整合处理
input: #名字是通道名称
destination: studyExchange #表示要使用的Exchange名称定义
content-type: application/json #配置消息类型
default-binder: defaultRabbit #配置要绑定的消息服务的具体设置
binders: #配置要绑定的rabbitmq服务信息
defaultRabbit: #定义的名称,用于binding整合
type: rabbit #消息组件类型
environment: #配置rabbitmq相关环境
spring:
rabbitmq:
host: 120.98.127.59
port: 5672
username: guest
password: guest
rabbitmq:
host: 120.98.127.59
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
instance:
instance-id: receive-8801 #修改主机名
prefer-ip-address: true #访问路径可以显示IP地址
然后是主启动类
package com.meng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author Administrator
*/
@SpringBootApplication
@EnableEurekaClient
public class StreamMQMain8802 {
public static void main(String[] args) {
SpringApplication.run(StreamMQMain8802.class,args);
}
}
这里,消费者不需要业务逻辑,直接上controller层
package com.meng.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
/**
* @author Administrator
*/
@Component
@EnableBinding(Sink.class)
public class ReceiveMessageListenerController {
@Value("${server.port}")
private String serverPort;
@StreamListener(Sink.INPUT)
public void input(Message<String> message){
System.out.println("消费者1号,接收到信息:"+message.getPayload()+"\tport:"+serverPort);
}
}
启动8802端口即可。
在浏览器访问http://localhost:8801/sendMessage,8801端口产生消息时8802也会收到消息。
不同组是可以全面消费(重复消费)的;但是同一组内会发生竞争关系,只有其中一个可以消费,这就需要引入group分组了。
group
yml修改,增加group字段即可。
同组的会轮询收到信息,不同组的都会收到信息。
spring:
application:
name: cloud-stream-consumer
cloud:
stream:
bindings: #服务整合处理
input: #名字是通道名称
destination: studyExchange #表示要使用的Exchange名称定义
content-type: application/json #配置消息类型
default-binder: defaultRabbit #配置要绑定的消息服务的具体设置
group: group1
binders: #配置要绑定的rabbitmq服务信息
defaultRabbit: #定义的名称,用于binding整合
type: rabbit #消息组件类型
environment: #配置rabbitmq相关环境
spring:
rabbitmq:
host: 120.98.127.59
port: 5672
username: guest
password: guest
消息持久化
在加入group之后,即使8802、8803端口没启动时8801消息提供者发送了消息,消费者端口再次打开后也能接收到消息,不会错过。