Stream 概述
使用中间件的组件,可选择的组件Kafka和RabbitMQ。
基础
springcloud
https://blog.csdn.net/qq_41520636/article/details/115848795
rabbitmq
https://blog.csdn.net/qq_41520636/article/details/115473826
hystrix-consumer 子工程
pom
<!-- stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
application.yml
spring:
application:
name: hystrix-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
cloud:
stream:
# 定义绑定器,绑定到哪个消息中间件上
binders:
test_binder: # 自定义的绑定器名称
type: rabbit # 绑定器类型
environment: # 指定mq的环境
spring:
rabbitmq:
host: 192.168.135.143
port: 5672
username: guest
password: guest
virtual-host: /
bindings:
input: # channel名称
content-type: application/json # 传输内容的格式
binder: test_binder #指定使用哪一个binder
destination: test_exchange # 消息目的地
监听类
package com.hikktn.listener;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;
/**
* @ClassName MessageListener
* @Description TODO
* @Author lisonglin
* @Date 2021/4/19 2:17
* @Version 1.0
*/
@EnableBinding({Sink.class})
@Component
public class MessageListener {
@StreamListener(Sink.INPUT)
public void receive(Message message){
System.out.println(message);
System.out.println(message.getPayload());
}
/**
* 消息消费失败的降级处理逻辑)
*
* @param message
*/
// @ServiceActivator(inputChannel = "test-topic.stream-exception-handler.errors")
// public void error(Message<?> message) {
// System.out.println("消费者失败的降级");
// }
}
hystrix-provider 子工程
pom
<!-- stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
application.yml
spring:
application:
name: hystrix-provider # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
cloud:
stream:
# 定义绑定器,绑定到哪个消息中间件上
binders:
test_binder: # 自定义的绑定器名称
type: rabbit # 绑定器类型
environment: # 指定mq的环境
spring:
rabbitmq:
host: 192.168.135.143
port: 5672
username: guest
password: guest
virtual-host: /
bindings:
output: # channel名称
content-type: application/json # 传输内容的格式
binder: test_binder #指定使用哪一个binder
destination: test_exchange # 消息目的地
dao
package com.hikktn.dao;
import com.hikktn.channel.MessageEvent;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName MessageProducer
* @Description TODO
* @Author lisonglin
* @Date 2021/4/19 2:32
* @Version 1.0
*/
@Component
@EnableBinding(Source.class)
public class MessageProducer {
@Autowired
private MessageChannel output;
// @Autowired
// private MessageEvent messageEvent;
public void send(String message){
// Map<String, Object> map = new HashMap<>();
// map.put("routingKey", "add");//设置routingKey
// String msessage = "hello stream~~~";
//发送消息
output.send(MessageBuilder.withPayload(message).build());
// messageEvent.userProducer().send(MessageBuilder.withPayload(message).copyHeaders(map).build());
System.out.println("消息发送成功~~~");
}
}
控制器
@RestController
@RequestMapping("/goods")
@RefreshScope // 开启刷新功能
public class GoodsController {
@RequestMapping("/send")
public String sendMsg(@RequestParam String message) {
producer.send(message);
return "success";
}
}
测试
- eureka-server
- hystrix-consumer
- hystrix-provider