初始代码
This commit is contained in:
40
yunzhupaas-cloud-common/yunzhupaas-common-mq/pom.xml
Normal file
40
yunzhupaas-cloud-common/yunzhupaas-common-mq/pom.xml
Normal file
@@ -0,0 +1,40 @@
|
||||
<?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.yunzhupaas</groupId>
|
||||
<artifactId>yunzhupaas-cloud-common</artifactId>
|
||||
<version>5.2.0-RELEASE</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>yunzhupaas-common-mq</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<artifactId>yunzhupaas-common-event</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--RocketMQ-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--RabbitMQ-->
|
||||
<!--<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
|
||||
</dependency>-->
|
||||
|
||||
<!--Kafka-->
|
||||
<!--<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
|
||||
</dependency>-->
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.yunzhupaas.config;
|
||||
|
||||
import com.baomidou.lock.LockTemplate;
|
||||
import com.yunzhupaas.consts.ProjectEventConst;
|
||||
import com.yunzhupaas.handler.ProjectEventMQMessageHandler;
|
||||
import com.yunzhupaas.handler.ProjectEventMQSender;
|
||||
import com.yunzhupaas.properties.EventProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.stream.function.StreamBridge;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class MqAutoConfiguration {
|
||||
|
||||
@Bean(ProjectEventConst.DEFAULT_TOPIC_NAME)
|
||||
@ConditionalOnProperty(prefix = "event", name = "event-publish-type", havingValue = ProjectEventConst.EVENT_PUBLISH_TYPE_QUEUE)
|
||||
public Consumer<Message<?>> getDefaultMqConsumer(LockTemplate lockTemplate) {
|
||||
return new ProjectEventMQMessageHandler(lockTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义事件发布渠道为QUEUE
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "event", name = "event-publish-type", havingValue = ProjectEventConst.EVENT_PUBLISH_TYPE_QUEUE)
|
||||
public ProjectEventMQSender getProjectEventMQSender(StreamBridge streamBridge, EventProperty eventProperties) {
|
||||
return new ProjectEventMQSender(streamBridge, eventProperties);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.yunzhupaas.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.lock.LockTemplate;
|
||||
import com.yunzhupaas.consts.ProjectEventConst;
|
||||
import com.yunzhupaas.module.ProjectEventInstance;
|
||||
import com.yunzhupaas.util.JsonUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 自定义事件监听, MQ渠道, 收到通知后发送Spring事件(RedisEventInstance)
|
||||
*/
|
||||
@Slf4j
|
||||
public class ProjectEventMQMessageHandler implements Consumer<Message<?>>, ApplicationEventPublisherAware {
|
||||
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
private final LockTemplate lockTemplate;
|
||||
|
||||
public ProjectEventMQMessageHandler(LockTemplate lockTemplate) {
|
||||
this.lockTemplate = lockTemplate;
|
||||
log.info("初始化自定义事件MQ监听器");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Message<?> o) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("事件监听收到MQ消息:{}", JSON.toJSONString(o));
|
||||
}
|
||||
// 是否存在自定义事件的标识
|
||||
if (o.getHeaders().get(ProjectEventConst.DEFAULT_CHANNEL_PREFIX) == null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("事件监听忽略MQ消息:{}", JSON.toJSONString(o));
|
||||
}
|
||||
return;
|
||||
}
|
||||
Object payload = o.getPayload();
|
||||
ProjectEventInstance instance;
|
||||
if (payload instanceof byte[]) {
|
||||
instance = JSON.parseObject((byte[]) payload, ProjectEventInstance.class);
|
||||
} else if (payload instanceof String) {
|
||||
instance = JSON.parseObject((String) payload, ProjectEventInstance.class);
|
||||
} else {
|
||||
instance = JsonUtil.getJsonToBean(payload, ProjectEventInstance.class);
|
||||
}
|
||||
applicationEventPublisher.publishEvent(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.yunzhupaas.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.yunzhupaas.consts.ProjectEventConst;
|
||||
import com.yunzhupaas.module.ProjectEvent;
|
||||
import com.yunzhupaas.event.ProjectEventSender;
|
||||
import com.yunzhupaas.properties.EventProperty;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.stream.function.StreamBridge;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
/**
|
||||
* 自定义事件发布 MQ渠道
|
||||
*/
|
||||
@Slf4j
|
||||
public class ProjectEventMQSender implements ProjectEventSender {
|
||||
|
||||
private StreamBridge streamBridge;
|
||||
private EventProperty eventProperty;
|
||||
|
||||
public ProjectEventMQSender(StreamBridge streamBridge, EventProperty eventProperty) {
|
||||
this.streamBridge = streamBridge;
|
||||
this.eventProperty = eventProperty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(ProjectEvent event) {
|
||||
streamBridge.send(ProjectEventConst.DEFAULT_TOPIC_NAME + "-out-0", MessageBuilder.withPayload(event)
|
||||
// 添加自定义事件标识
|
||||
.setHeader(ProjectEventConst.DEFAULT_CHANNEL_PREFIX, event.getChannel())
|
||||
.build());
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("发送MQ自定义事件: {}", JSON.toJSONString(event));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user