fix(): 事件基础修改

dev.event
刘武 2024-10-06 10:39:59 +08:00
parent fe90a341ff
commit 87d104cf9b
24 changed files with 132 additions and 218 deletions

View File

@ -1,7 +1,5 @@
package com.muyu.common.kafka.config; package com.muyu.common.kafka.config;
import com.muyu.common.core.constant.KafkaConstant; import com.muyu.common.core.constant.KafkaConstant;
import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer; import org.apache.kafka.common.serialization.StringDeserializer;

View File

@ -49,8 +49,6 @@ public class KafkaProducerConfig {
private String acks; private String acks;
@Bean @Bean
public KafkaProducer kafkaProducer() { public KafkaProducer kafkaProducer() {
Map<String, Object> configs = new HashMap<>(); Map<String, Object> configs = new HashMap<>();

View File

@ -34,6 +34,7 @@
<groupId>com.muyu</groupId> <groupId>com.muyu</groupId>
<artifactId>cloud-common-redis</artifactId> <artifactId>cloud-common-redis</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,83 +0,0 @@
package com.muyu.common.rabbit.config;
import com.muyu.common.rabbit.constants.RabbitmqConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.util.HashMap;
/**
* @ClassName: DelayedQueueConfig
* @Description:
*/
@Configuration
public class DelayedQueueConfig {
@Resource
private RabbitAdmin rabbitAdmin;
/**
*
* @return
*/
@Bean
public Queue delayedQueue() {
Queue queue = new Queue(RabbitmqConstants.DELAYED_QUEUE_NAME);
rabbitAdmin.declareQueue(queue);
return queue;
}
/**
*
* @return
*/
@Bean
public Exchange delayedExchange() {
HashMap<String, Object> arguments = new HashMap<>(3);
arguments.put("x-delayed-type", "direct");
/**
*
*
*
*
*
*
*/
CustomExchange customExchange = new CustomExchange(
RabbitmqConstants.DELAYED_EXCHANGE_NAME,
"x-delayed-message",
true,
false,
arguments);
rabbitAdmin.declareExchange(customExchange);
return customExchange;
}
/**
*
* @param delayedQueue
* @param delayedExchange
*/
@Bean
public Binding delayedQueueBindingDelayedExchange(
@Qualifier("delayedQueue") Queue delayedQueue,
@Qualifier("delayedExchange") Exchange delayedExchange) {
Binding noargs = BindingBuilder.bind(delayedQueue)
.to(delayedExchange)
.with(RabbitmqConstants.DELAYED_ROUTING_KEY)
.noargs();
rabbitAdmin.declareBinding(noargs);
return noargs;
}
}

View File

@ -1,75 +0,0 @@
package com.muyu.common.rabbit.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: WangXin
* @Time: 2024/4/22 11:55
* @Description:
*/
@Configuration
public class TopicConfig {
/**
*
* @return exchange
*/
@Bean(name = "topicExchange")
public Exchange getTopicExchange(){
return ExchangeBuilder
.topicExchange("exchange_topic")
.build();
}
/**
* 01
* @return queue
*/
@Bean(name = "topicQueue01")
public Queue getTopicQueue01(){
return QueueBuilder
.durable("queue_topic_01")
.build();
}
/**
* 02
* @return queue
*/
@Bean(name = "topicQueue02")
public Queue getTopicQueue02(){
return QueueBuilder
.durable("queue_topic_02")
.build();
}
/**
* 01
* @return binding
*/
@Bean
public Binding getTopicBinding01(){
return BindingBuilder
.bind(getTopicQueue01())
.to(getTopicExchange())
//路由键 队列1接收debug级别的消息
.with("front.#")
.noargs();
}
/**
* 02
* @return binding
*/
@Bean
public Binding getTopicBinding02(){
return BindingBuilder
.bind(getTopicQueue02())
.to(getTopicExchange())
// 路由键 队列2接收info级别的消息
.with("back.order.*")
.noargs();
}
}

View File

@ -1,4 +1,4 @@
package com.muyu.common.rabbit.config; package com.muyu.rabbitmq.config;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.connection.CorrelationData;

View File

@ -1,4 +1,4 @@
package com.muyu.common.rabbit.config; package com.muyu.rabbitmq.config;
@ -21,16 +21,16 @@ public class RabbitAdminConfig {
private String username; private String username;
@Value("${spring.rabbitmq.password}") @Value("${spring.rabbitmq.password}")
private String password; private String password;
@Value("${spring.rabbitmq.virtualhost}") @Value("${spring.rabbitmq.port}")
private String virtualHost; private Integer port;
@Bean @Bean
public ConnectionFactory connectionFactory() { public ConnectionFactory connectionFactory() {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setHost(host); cachingConnectionFactory.setHost(host);
cachingConnectionFactory.setPort(port);
cachingConnectionFactory.setUsername(username); cachingConnectionFactory.setUsername(username);
cachingConnectionFactory.setPassword(password); cachingConnectionFactory.setPassword(password);
cachingConnectionFactory.setVirtualHost(virtualHost);
return cachingConnectionFactory; return cachingConnectionFactory;
} }

View File

@ -1,4 +1,4 @@
package com.muyu.common.rabbit.config; package com.muyu.rabbitmq.config;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter; import org.springframework.amqp.support.converter.MessageConverter;

View File

@ -1,4 +1,4 @@
package com.muyu.common.rabbit.config; package com.muyu.rabbitmq.config;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.amqp.core.ReturnedMessage; import org.springframework.amqp.core.ReturnedMessage;

View File

@ -1,11 +1,15 @@
package com.muyu.common.rabbit.consumer; package com.muyu.rabbitmq.consumer;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.muyu.common.redis.service.RedisService; import com.muyu.common.redis.service.RedisService;
import com.muyu.rabbitmq.util.CacheUtil;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message; import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
@ -22,6 +26,9 @@ public class RabbitMQConsumerUtil {
private final RedisService redisService; private final RedisService redisService;
@Autowired
private CacheUtil cacheUtil;
/** /**
* *
@ -29,7 +36,8 @@ public class RabbitMQConsumerUtil {
* @param message * @param message
* @param channel * @param channel
*/ */
public void rabbitMQBasicConsumer(Object data ,Message message , Channel channel) { @RabbitListener(queuesToDeclare = @Queue(name = "basic"))
public void rabbitMQBasicConsumer(String data ,Message message , Channel channel) {
log.info("当前时间:{} RabbitMQConsumerUtil : {}", new Date(), message); log.info("当前时间:{} RabbitMQConsumerUtil : {}", new Date(), message);
try { try {
// 获取到消息 开始消费 // 获取到消息 开始消费
@ -42,10 +50,12 @@ public class RabbitMQConsumerUtil {
return; return;
} }
/** /**
* --------------------------------------------------------------- * ---------------------------------------------------------------
*/ */
String carList = (String) redisService.redisTemplate.opsForValue().get("carList");
cacheUtil.put("carList",carList);
/** /**
* ------------------------------------------------------------------------------ * ------------------------------------------------------------------------------

View File

@ -1,7 +1,7 @@
package com.muyu.common.rabbit.producer; package com.muyu.rabbitmq.producer;
import com.muyu.common.core.domain.Result; import com.muyu.common.core.domain.Result;
import com.muyu.common.rabbit.constants.RabbitmqConstants; import com.muyu.rabbitmq.constants.RabbitmqConstants;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.core.MessageProperties;
@ -31,9 +31,9 @@ public class RabbitMQProducerUtil {
* @return * @return
* *
*/ */
public Result<?> basicSendMessage(String queueName, Object param, String msg) { public void basicSendMessage(String queueName, String param) {
log.info("【简单模型mq】 : method: 【 basicSendMessage 】 - ages: 【 String : {}, Object : {}, String : {} 】 ---> 【 消息发送中。。。 】", RabbitmqConstants.BASIC_QUEUE_NAME, param, msg); log.info("【简单模型mq】 : method: 【 basicSendMessage 】 - ages: 【 String : {}, Object : {}】 ---> 【 消息发送中。。。 】", RabbitmqConstants.BASIC_QUEUE_NAME, param);
// 发送简单模型消息 // 发送简单模型消息
// 第一个参数: 绑定规则 相当于 队列名称 // 第一个参数: 绑定规则 相当于 队列名称
// 第二个参数:消息内容 // 第二个参数:消息内容
@ -44,7 +44,7 @@ public class RabbitMQProducerUtil {
log.info("【简单模型mq】 : method: 【 basicSendMessage 】- queue: 【 {} 】 ---> 【 消息发送成功 】", RabbitmqConstants.BASIC_QUEUE_NAME); log.info("【简单模型mq】 : method: 【 basicSendMessage 】- queue: 【 {} 】 ---> 【 消息发送成功 】", RabbitmqConstants.BASIC_QUEUE_NAME);
return Result.success(msg!=null?msg:"消息发送成功");
} }
/** /**
@ -156,19 +156,19 @@ public class RabbitMQProducerUtil {
* @param delayTime * @param delayTime
* @return * @return
*/ */
public Result<?> delayedSendMessage(Long delayTime, Object param) { // public Result<?> delayedSendMessage(Long delayTime, Object param) {
log.info("【延迟队列模型】 : method: 【 delayedSendMessage 】 消息内容:{}---> 【 消息发送中。。。 】",param); // log.info("【延迟队列模型】 : method: 【 delayedSendMessage 】 消息内容:{}---> 【 消息发送中。。。 】",param);
//
rabbitTemplate.convertAndSend(RabbitmqConstants.DELAYED_EXCHANGE_NAME, RabbitmqConstants.DELAYED_ROUTING_KEY,param, message -> { // rabbitTemplate.convertAndSend(RabbitmqConstants.DELAYED_EXCHANGE_NAME, RabbitmqConstants.DELAYED_ROUTING_KEY,param, message -> {
MessageProperties messageProperties = message.getMessageProperties(); // MessageProperties messageProperties = message.getMessageProperties();
messageProperties.setMessageId(UUID.randomUUID().toString()); // messageProperties.setMessageId(UUID.randomUUID().toString());
messageProperties.setDelayLong(delayTime); // messageProperties.setDelayLong(delayTime);
return message; // return message;
}); // });
log.info("【延迟队列模型】 : method: 【 delayedSendMessage 】 消息内容:{}---> 【 消息发送成功 】",param); // log.info("【延迟队列模型】 : method: 【 delayedSendMessage 】 消息内容:{}---> 【 消息发送成功 】",param);
//
return Result.success(param,"消息发送成功"); // return Result.success(param,"消息发送成功");
//
} // }
} }

View File

@ -0,0 +1,37 @@
package com.muyu.rabbitmq.util;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.stereotype.Component;
/**
*
*
* @program: cloud-server
* @author:
* @create: 2024-09-30 10:08
**/
@Component
public class CacheUtil<T> {
private final Cache<String, T> cache;
public CacheUtil() {
this.cache = Caffeine.newBuilder()
.maximumSize(500L)
.build();
}
public T get(String key) {
return cache.getIfPresent(key);
}
public void put(String key, T value) {
cache.put(key, value);
}
public void remove(String key) {
cache.invalidate(key);
}
}

View File

@ -2,6 +2,5 @@ com.muyu.rabbitmq.producer.RabbitMQProducerUtil
com.muyu.rabbitmq.consumer.RabbitMQConsumerUtil com.muyu.rabbitmq.consumer.RabbitMQConsumerUtil
com.muyu.rabbitmq.config.RabbitmqConfig com.muyu.rabbitmq.config.RabbitmqConfig
com.muyu.rabbitmq.config.MyConfirmCallback com.muyu.rabbitmq.config.MyConfirmCallback
com.muyu.rabbitmq.config.DelayedQueueConfig
com.muyu.rabbitmq.config.RabbitAdminConfig com.muyu.rabbitmq.config.RabbitAdminConfig
com.muyu.rabbitmq.config.ReturnCallbackConfig com.muyu.rabbitmq.config.ReturnCallbackConfig

View File

@ -40,7 +40,7 @@ import java.util.Map;
public class ManyDataSource implements ApplicationRunner{ public class ManyDataSource implements ApplicationRunner{
private List<EntInfo> dataSourceInfoList(){ private List<EntInfo> dataSourceInfoList(){
RemoteSaaSService remoteSaaSService = SpringUtils.getBean(RemoteSaaSService.class); RemoteSaaSService remoteSaaSService = SpringUtils.getBean(RemoteSaaSService.class);
Result<List<Datasource>> tableDataInfoResult = remoteSaaSService.findDatabaseList(); Result<List<Datasource>> tableDataInfoResult = remoteSaaSService.findDatabaseList();
if (tableDataInfoResult==null){ if (tableDataInfoResult==null){
throw new SaaSException("saas远调数据源错误"); throw new SaaSException("saas远调数据源错误");
} }

View File

@ -23,6 +23,7 @@
<module>cloud-common-saas</module> <module>cloud-common-saas</module>
<module>cloud-common-swagger</module> <module>cloud-common-swagger</module>
<module>cloud-common-cache</module> <module>cloud-common-cache</module>
<module>cloud-common-kafka</module>
</modules> </modules>
<artifactId>cloud-common</artifactId> <artifactId>cloud-common</artifactId>

View File

@ -66,9 +66,7 @@ public class IoTDBConfig {
measurements.add("car_vin"); measurements.add("car_vin");
measurements.add("information"); measurements.add("information");
session.insertRecord(TABLENAME,System.currentTimeMillis(),measurements,list); session.insertRecord(TABLENAME,System.currentTimeMillis(),measurements,list);
//关闭连接 //关闭连接
session.close(); session.close();
} catch (IoTDBConnectionException e) { } catch (IoTDBConnectionException e) {

View File

@ -1,7 +1,5 @@
package com.muyu.event.consumer; package com.muyu.event.consumer;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.muyu.event.basic.EventPublisher; import com.muyu.event.basic.EventPublisher;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
@ -36,7 +34,6 @@ public class MessageConsumer implements ApplicationRunner {
private final String topic="four_car"; private final String topic="four_car";
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
List<String> list = Collections.singletonList(topic); List<String> list = Collections.singletonList(topic);
@ -48,10 +45,8 @@ public class MessageConsumer implements ApplicationRunner {
String value = record.value(); String value = record.value();
JSONObject jsonObject = JSONObject.parseObject(value); JSONObject jsonObject = JSONObject.parseObject(value);
log.info("value:{}",value); log.info("value:{}",value);
// eventPublisher.publishEvent(jsonObject); eventPublisher.publishEvent(jsonObject);
}); });
} }
} }
} }

View File

@ -0,0 +1,21 @@
package com.muyu.event.consumer;
import com.muyu.rabbitmq.consumer.RabbitMQConsumerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* rabbitmq
* @author
* @packagecom.muyu.event.consumer
* @nameMqConsumer
* @date2024/10/2 14:17
*/
@Component
public class MqConsumer {
}

View File

@ -18,4 +18,6 @@ public class OnlineConsumer {
} }

View File

@ -4,6 +4,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
*
* @author * @author
* @packagecom.muyu.event.controller * @packagecom.muyu.event.controller
* @nameDataController * @nameDataController

View File

@ -19,13 +19,12 @@ import java.util.List;
* @nameItodbController * @nameItodbController
* @date2024/9/28 19:17 * @date2024/9/28 19:17
*/ */
@RestController() @RestController
public class IoTDBController { public class IoTDBController {
@Autowired @Autowired
private IoTDBService tdbService; private IoTDBService tdbService;
/** /**
* *
* @return list * @return list
@ -47,7 +46,6 @@ public class IoTDBController {
return Result.success(carInformation); return Result.success(carInformation);
}; };
/** /**
* *
* @param addCarInformation * @param addCarInformation
@ -71,4 +69,6 @@ public class IoTDBController {
} }

View File

@ -2,20 +2,17 @@ package com.muyu.event.controller;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.muyu.event.service.TestService; import com.muyu.rabbitmq.producer.RabbitMQProducerUtil;
import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
/** /**
*
* @author * @author
* @packagecom.muyu.event.controller * @packagecom.muyu.event.controller
* @nameTestController * @nameTestController
@ -26,25 +23,33 @@ public class TestController {
@Resource @Resource
private KafkaProducer kafkaProducer; private KafkaProducer kafkaProducer;
@Resource
private RabbitMQProducerUtil rabbitMQProducerUtil;
private static final String topic="four_car"; private static final String topic="four_car";
@GetMapping("send") @GetMapping("sendKafka")
public String sendKafka(){ public String sendKafka(){
String message="发送一条信息"; String message="发送一条信息";
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("cj","sb"); jsonObject.put("cj","hh");
ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(topic,jsonObject.toString());
ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(topic,jsonObject.toJSONString());
kafkaProducer.send(producerRecord); kafkaProducer.send(producerRecord);
return "success"; return "success";
} }
@GetMapping("sendMq")
public String sendMq(){
String message="发送一条信息-mq";
rabbitMQProducerUtil.basicSendMessage("basic",message);
return "success-mq";
};

View File

@ -13,7 +13,13 @@ import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date; import java.util.Date;
/**
*
* @author
* @packagecom.muyu.event.domain
* @nameEvent
* @date2024/9/28 23:10
*/
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor