小院 3.26 18:22

master
msb_221930 8 months ago
parent aadaa17108
commit 0b854ceb7d

@ -23,6 +23,7 @@ import java.time.format.DateTimeFormatter;
* @author zjw
* @description
*/
/*
@Configuration
@ -77,4 +78,5 @@ public class RedisConfig {
}
}*/
}
*/

@ -3,6 +3,7 @@ package com.mashibing.cache.controller;
import com.msb.framework.redis.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;
@ -19,6 +20,9 @@ public class CacheController {
@Autowired
private RedisClient redisClient;
@Autowired
private RedisTemplate redisTemplate;
@PostMapping(value = "/cache/hmset/{key}")
public void hmset(@PathVariable(value = "key") String key, @RequestBody Map<String, Object> map) {
@ -146,4 +150,13 @@ public class CacheController {
return result;
}
@PostMapping(value = "/cache/keys/{pattern}")
public Set<String> keys(@PathVariable(value = "pattern") String pattern) {
log.info("【缓存模块】 keys方法根据pattren查询key的信息 pattern = {}", pattern);
Set<String> keys = redisTemplate.keys(pattern);
log.info("【缓存模块】 keys方法根据pattren查询key的信息 pattern = {},查询出全部的key信息 keys = {}", pattern, keys);
return keys;
}
}

@ -0,0 +1,62 @@
<?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">
<parent>
<artifactId>beacon-cloud</artifactId>
<groupId>com.mashibing</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>beacon-monitor</artifactId>
<dependencies>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos注册-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- 公共模块-->
<dependency>
<groupId>com.mashibing</groupId>
<artifactId>beacon-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- xxl-job的依赖-->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.1</version>
</dependency>
<!-- RabbitMQ依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- openFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- JavaMail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,21 @@
package com.mashibing.monitor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author dch
* @create 2024-03-28 10:20
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class MonitorStarterApp {
public static void main(String[] args) {
SpringApplication.run(MonitorStarterApp.class,args);
}
}

@ -0,0 +1,23 @@
package com.mashibing.monitor.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.Map;
import java.util.Set;
/**
* @author dch
* @create 2024-03-20 11:54
*/
@FeignClient("beacon-cache")
public interface CacheClient {
@PostMapping(value = "/cache/keys/{pattern}")
Set<String> keys(@PathVariable(value = "pattern") String pattern);
@GetMapping("/cache/hgetall/{key}")
Map hGetAll(@PathVariable(value = "key") String key);
}

@ -0,0 +1,56 @@
package com.mashibing.monitor.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author dch
* @create 2024-03-28 11:08
*/
@Configuration
@Slf4j
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}

@ -0,0 +1,54 @@
package com.mashibing.monitor.task;
import com.mashibing.monitor.client.CacheClient;
import com.mashibing.monitor.util.MailUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import javax.mail.MessagingException;
import java.util.Map;
import java.util.Set;
/**
* @author dch
* @create 2024-03-28 17:41
*/
public class MonitorClientBalanceTask {
//客户余额限制 小于500直接发送信息
private final long balanceLimit = 500000;
private final String CLIENT_BALANCE_PATTERN = "client_balance:*";
private final String BALANCE = "balance";
private final String EMAIL = "extend1";
private String text = "客户您好,您在【风火云短信平台】的短信余额仅剩%s元";
@Autowired
private CacheClient cacheClient;
@Autowired
private MailUtil mailUtil;
@XxlJob("monitorClientBalanceTask")
public void monitor() throws MessagingException {
//1.查询用户余额
Set<String> keys = cacheClient.keys(CLIENT_BALANCE_PATTERN);
for (String key : keys) {
Map map = cacheClient.hGetAll(key);
Long balance = Long.parseLong(map.get(BALANCE)+"");
String email = (String) map.get(EMAIL);
//2.判断是否小于500发送邮件
if (balance < balanceLimit) {
mailUtil.sendEmail(email,"【风火云短信平台】提醒您余额不足",String.format(text,balance/1000));
}
}
}
}

@ -0,0 +1,90 @@
package com.mashibing.monitor.task;
import com.mashibing.common.constant.RabbitMQConstants;
import com.mashibing.monitor.client.CacheClient;
import com.mashibing.monitor.util.MailUtil;
import com.rabbitmq.client.Channel;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import java.io.IOException;
import java.util.Set;
/**
* 10000
*
* @author dch
* @create 2024-03-28 16:17
*/
@Component
@Slf4j
public class MonitorQueueMessageCountTask {
String text = "<h1>您的队列消息队列堆积了,队名为%s消息个数为%s</1>";
// 队列消息限制
private final long MESSAGE_COUNT_LIMIT = 10000;
// 查询队列名称的固定pattern
private final String QUEUE_PATTERN = "channel:*";
// 得到需要截取channelID的索引地址
private final int CHANNEL_ID_INDEX = QUEUE_PATTERN.indexOf("*");
// 注入RabbitMQ的ConnectionFactory
@Autowired
private ConnectionFactory connectionFactory;
@Autowired
private CacheClient cacheClient;
@Autowired
private MailUtil mailUtil;
@XxlJob("monitorQueueMessageCountTask")
public void monitor() throws MessagingException {
//1、拿到所有的队列名称
Set<String> keys = cacheClient.keys(QUEUE_PATTERN);
//2、需要基于channel去操作
Connection connection = connectionFactory.createConnection();
Channel channel = connection.createChannel(false);
listenQueueAndSendEmail(channel, RabbitMQConstants.SMS_PRE_SEND);
for (String key : keys) {
// 封装队列名称
String queueName = RabbitMQConstants.SMS_GATEWAY + key.substring(CHANNEL_ID_INDEX);
listenQueueAndSendEmail(channel, queueName);
}
}
private void listenQueueAndSendEmail(Channel channel, String queueName) throws MessagingException {
// 队列不存在,直接构建,如果已经存在,直接忽略
try {
channel.queueDeclare(queueName, true, false, false, null);
} catch (IOException e) {
e.printStackTrace();
}
//3、拿到对应队列的消息确认消息数量超过限制及时通知
long count = 0;
try {
count = channel.messageCount(queueName);
} catch (IOException e) {
e.printStackTrace();
}
if (count > MESSAGE_COUNT_LIMIT) {
//4、通知的方式就是发送短信。
mailUtil.sendEmail(queueName + "队列消息堆积", String.format(text, queueName, count));
}
}
}

@ -0,0 +1,20 @@
package com.mashibing.monitor.task;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author dch
* @create 2024-03-28 15:57
*/
@Component
@Slf4j
public class TestTask {
@XxlJob("test")
public void test(){
// 编写任务逻辑
log.info("Hello World!");
}
}

@ -0,0 +1,63 @@
package com.mashibing.monitor.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* @author dch
* @create 2024-03-28 17:05
*/
@Component
@RefreshScope
public class MailUtil {
@Value("${spring.mail.username}")
private String from;
@Value("${spring.mail.tos}")
private String tos;
// @Autowired
@Resource
private JavaMailSender javaMailSender;
public void sendEmail(String subject,String text) throws MessagingException {
// 构建MimeMessage对象
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
// 给邮件指定信息
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(from);
helper.setTo(tos.split(","));
helper.setSubject(subject);
helper.setText(text);
// 发送邮件
javaMailSender.send(mimeMessage);
}
public void sendEmail(String to, String subject, String text) throws MessagingException {
// 构建MimeMessage对象
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
// 给邮件指定信息
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text);
// 发送邮件
javaMailSender.send(mimeMessage);
}
}

@ -0,0 +1,17 @@
# 服务名称
spring:
application:
name: beacon-monitor
# 多环境
profiles:
active: dev
# nacos注册中心地址
cloud:
nacos:
discovery:
server-addr: 192.168.1.127:8848
# nacos配置中心地址:
config:
server-addr: 192.168.1.127:8848
file-extension: yml
# beacon-monitor-dev.yml
Loading…
Cancel
Save