代码详细演示设计一个中台监控,中台接口要能记录服务调用次数,服务调用来源,自定义注解,发送消息到MQ,数据交互平台从MQ消费消息,插入到数据库
时间: 2024-03-05 22:52:11 浏览: 25
好的,下面是一个简单的中台监控的设计方案:
1. 使用Spring AOP实现自定义注解,用于标识需要进行监控的接口方法。
2. 在接口方法执行前,记录服务调用次数和调用来源。可以使用Redis等缓存工具进行存储。
3. 在接口方法执行后,发送消息到MQ,消息中包含调用次数、调用来源、接口名称等信息。
4. 数据交互平台从MQ消费消息,并将消息中的信息插入到数据库中。
下面是一个简单的代码演示:
1. 定义自定义注解:Monitor
```java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Monitor {
}
```
2. 使用AOP实现监控逻辑
```java
@Aspect
@Component
public class MonitorAspect {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Before("@annotation(cn.example.Monitor)")
public void doBefore(JoinPoint joinPoint) {
// 获取注解标识的方法名
String methodName = joinPoint.getSignature().getName();
// 获取调用来源
String source = getRequestSource();
// 记录调用次数
String key = methodName + ":" + source;
redisTemplate.opsForValue().increment(key);
}
@AfterReturning("@annotation(cn.example.Monitor)")
public void doAfterReturning(JoinPoint joinPoint) {
// 获取注解标识的方法名
String methodName = joinPoint.getSignature().getName();
// 获取调用来源
String source = getRequestSource();
// 发送消息到MQ
Map<String, Object> message = new HashMap<>();
message.put("methodName", methodName);
message.put("source", source);
message.put("count", redisTemplate.opsForValue().get(methodName + ":" + source));
rabbitTemplate.convertAndSend("monitor.exchange", "monitor.routingKey", message);
}
/**
* 获取请求来源
*/
private String getRequestSource() {
// 从请求头或者请求参数中获取来源信息
// ...
return "source";
}
}
```
3. 数据交互平台从MQ中消费消息并插入到数据库
```java
@Component
public class MonitorConsumer {
@Autowired
private MonitorRepository monitorRepository;
@RabbitListener(queues = "monitor.queue")
public void handleMessage(Map<String, Object> message) {
MonitorEntity entity = new MonitorEntity();
entity.setMethodName(message.get("methodName").toString());
entity.setSource(message.get("source").toString());
entity.setCount(Integer.parseInt(message.get("count").toString()));
monitorRepository.save(entity);
}
}
```
以上就是一个简单的中台监控的设计方案和代码演示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)