public abstract class EventMessageBroker {
/\*\*
\* 消息多播
\*
\* 将集群间消息在自己服务内部广播,是的所有订阅消息都可以收到
\*
\* @param message
\* @return
\*/
public abstract void multicastEvent(AdapterMessage message) throws IllegalAccessException, InstantiationException, InvocationTargetException, IOException;
public void doMulticastEvent(AdapterMessage message, Map<TopicMatcher, Map<String, MessageAdapter>> subscribesPool, ApplicationContext applicationContext) throws InvocationTargetException, IllegalAccessException, InstantiationException, IOException {
// 查找相匹配的Adapter
List<MessageAdapter> adapterList = TopicUtil.getAllMatchedAdapter(message.getTopic(), subscribesPool);
for (MessageAdapter adapter : adapterList) {
if (adapter != null && adapter.isActive()) {
// 手动订阅消息
if (adapter.getCustomer() != null) {
adapter.getCustomer().consume(message);
} else {
// 取得对象
Object instance = applicationContext.getBean(adapter.getClazz());
if (instance != null) {
// 将消息转化为所需要的类型
if (adapter.getMethod().getParameterCount() > 0) {
Class<?> param = adapter.getMethod().getParameterTypes()[0];
adapter.getMethod().invoke(instance, JSON.parseObject(message.getPayload(), param));
} else {
adapter.getMethod().invoke(instance);
}
}
}
}
}
}
}
使用redis实现消费者
/\*\*
\* 消息总线-集群间消息redis实现
\*
\* 实现规则
\* 根据不同的MQ机制实现消息的分组消费,将原始消息转化为AdapterMessage,然后使用继承于EventMessageBroker进行消息多播
\*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "stream.broker", name = "type", havingValue = "redis", matchIfMissing = true)
public class RedisMessageBroker extends EventMessageBroker implements StreamListener<String, MapRecord<String, String, String>> {
@Value("${stream.topic:/stream}")
String topic;
@Value("${stream.consumer.id}")
String consumerId;
@Value("${stream.timeout:10}")
Integer timeout;
@Value("${stream.consumer.group}")
String groupName = "stream.redis";
private final RedisTemplate<String, String> streamRedisTemplate;
private final ApplicationContext applicationContext;
private final EventBus eventBus;
public RedisMessageBroker(RedisTemplate<String, String> streamRedisTemplate, ApplicationContext applicationContext, EventBus eventBus) {
this.streamRedisTemplate = streamRedisTemplate;
this.applicationContext = applicationContext;
this.eventBus = eventBus;
}
@SneakyThrows
@Override
public void onMessage(MapRecord<String, String, String> message) {
log.info("消息内容-->{}", message.getValue());
StreamOperations<String, String, String> streamOperations = streamRedisTemplate.opsForStream();
// 服务内消息多播
AdapterMessage adapterMessage = new AdapterMessage();
adapterMessage.setTopic(message.getValue().get("topic"));
adapterMessage.setPayload(message.getValue().get("payload"));
try {
this.multicastEvent(adapterMessage);
} catch (Exception e) {
log.info("消息多播失败:" + e.getLocalizedMessage());
}
//消息应答
streamOperations.acknowledge(topic, groupName, message.getId());
}
@Bean
public StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, MapRecord<String, String, String>> emailListenerContainerOptions() {
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
return StreamMessageListenerContainer.StreamMessageListenerContainerOptions
.builder()
//block读取超时时间
.pollTimeout(Duration.ofSeconds(timeout))
//count 数量(一次只获取一条消息)
.batchSize(1)
//序列化规则
.serializer(stringRedisSerializer)
.build();
}
/\*\*
\* 开启监听器接收消息
\*/
@Bean
public StreamMessageListenerContainer<String, MapRecord<String, String, String>> emailListenerContainer(RedisConnectionFactory factory,
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, MapRecord<String, String, String>> streamMessageListenerContainerOptions) {
StreamMessageListenerContainer<String, MapRecord<String, String, String>> listenerContainer = StreamMessageListenerContainer.create(factory,
streamMessageListenerContainerOptions);
//如果 流不存在 创建 stream 流
if (!streamRedisTemplate.hasKey(topic)) {
streamRedisTemplate.opsForStream().add(topic, Collections.singletonMap("", ""));
log.info("初始化集群间通信Topic{} success", topic);
}
//创建消费者组
try {
streamRedisTemplate.opsForStream().createGroup(topic, groupName);
} catch (Exception e) {
log.info("消费者组 {} 已存在", groupName);
}
//注册消费者 消费者名称,从哪条消息开始消费,消费者类
// > 表示没消费过的消息
// $ 表示最新的消息
listenerContainer.receive(
Consumer.from(groupName, consumerId),
StreamOffset.create(topic, ReadOffset.lastConsumed()),
this
);
listenerContainer.start();
return listenerContainer;
}
@Override
public void multicastEvent(AdapterMessage message) throws IllegalAccessException, InstantiationException, InvocationTargetException, IOException {
super.doMulticastEvent(message, eventBus.getSubscribesPool(), applicationContext);
}
}
配套@Subscribe注解
/\*\*
\* 自定义消息订阅
\*
\* @author liuhl
\*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@SuppressWarnings("unused")
public @interface Subscribe {
/\*\*
\* 订阅topic
\* @return
\*/
@AliasFor("value")
String topic() default "";
@AliasFor("topic")
String value() default "";
}
实现BeanPostProcessor监听所有的bean创建
@Component
@Slf4j
public class SpringMessageSubscribe implements BeanPostProcessor {
private final EventBus eventBus;
private final MessageAdapterCrater messageAdapterCrater = new DefaultMessageAdapterCrater();
public SpringMessageSubscribe(EventBus eventBus) {
this.eventBus = eventBus;
}
/\*\*
\* 为注解添加监听处理
\*
\* @param bean
\* @param beanName
\* @return
\* @throws BeansException
\*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class<?> type = ClassUtils.getUserClass(bean);
ReflectionUtils.doWithMethods(type, method -> {
// 取得所有订阅方法
AnnotationAttributes subscribes = AnnotatedElementUtils.getMergedAnnotationAttributes(method, Subscribe.class);
if (CollectionUtils.isEmpty(subscribes)) {
return;
}
// 生成订阅Adapter
MessageAdapter sub = messageAdapterCrater.createMessageAdapter(type, method, subscribes.getString("topic"));
// 注册订阅Adapter
eventBus.addAdapter(sub, subscribes.getString("topic"));
});
return bean;
}
}
EventBus对象
EventBus实体,主要负责三个功能,发布、订阅的入口,以及管理维护MessageAdapter
/\*\*
\* 消息总线
\*/
@Component
public class EventBus {
private final MessagePublisher messagePublisher;
private Map<TopicMatcher, Map<String, MessageAdapter>> subscribesPool = new ConcurrentHashMap();
public EventBus(MessagePublisher messagePublisher) {
this.messagePublisher = messagePublisher;
}
/\*\*
\* 集群间消息发布
\*
\* @param message 消息体
\* @return 发布结果
\*/
public Boolean publish(AdapterMessage message) {
return messagePublisher.publish(message);
}
/\*\*
\* 将Adapter注册到EventBus
\*
\* @param adapter
\* @param topic
\*/
public void addAdapter(MessageAdapter adapter, String topic) {
if (subscribesPool.get(topic) != null) {
subscribesPool.get(topic).put(adapter.getId(), adapter);
} else {
Map<String, MessageAdapter> adapterMap = new HashMap<>();
adapterMap.put(adapter.getId(), adapter);
subscribesPool.put(new TopicMatcher(topic), adapterMap);
}
}
/\*\*
\* 将Adapter注册到EventBus
\*
\* @param adapter
\* @param topic
\*/
public void removeAdapter(MessageAdapter adapter, String topic) {
if (subscribesPool.get(topic) != null) {
subscribesPool.get(topic).remove(adapter.getId());
## 最后
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**
**因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/7f4d61fff54df420ae301ae98c6d242f.png)
![img](https://img-blog.csdnimg.cn/img_convert/9b818099c9e4561a0e3f84fbe4debea8.jpeg)
![img](https://img-blog.csdnimg.cn/img_convert/967c21f1bcc3c69e5dc42491626a6584.png)
![img](https://img-blog.csdnimg.cn/img_convert/ef1a08f443c2ee297a9715a9035e3c7b.png)
![img](https://img-blog.csdnimg.cn/img_convert/1a515f436b6b4029907874e390752506.png)
![img](https://img-blog.csdnimg.cn/img_convert/c4959b0e642a791e4aa45b2861b67119.png)
![](https://img-blog.csdnimg.cn/img_convert/c21b264e8feb134f39100ed3d8d3aa82.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!
存中...(img-aybsYoP1-1715676846502)]
[外链图片转存中...(img-BFGaQ9PQ-1715676846503)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!