9:记录佛山项目两个应用服务器做负载均衡,一直报错邮件的服务连接拒绝问题:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OA8PnzTn-1675933428271)(C:\Users\ourhem\Desktop\ycl\奥凯实习\noteImage\image-20230209163510140.png)]
分析了一早上,发现原因,两个应用都用同样的定时器去获取邮件资源,共同去操作表,产生了读写冲突,信创云平台采用注释掉一个的定时任务,虽好,但是一旦该一服务器cpu高时,定时任务请求仍然打到这里,容易宕机
10:解决测试反馈上传70m的文件失败问题:
是由于nginx没有配置客户端文件大小问题,
https://blog.csdn.net/ffzhihua/article/details/89339015
nginx的conf的http模块添加如下配置即可成功:
http{
#解决大文件上传服务器失败问题:
keepalive_timeout 100; #这个参数表示http连接超时时间,默认是65s。要是上传文件比较大,在规定时间内没有上传完成,就会自动断开连接!所以适当调大这个时间。
client_header_timeout 120s; #调大点
client_body_timeout 120s; #调大点
client_max_body_size 100m; #主要是这个参数,限制了上传文件大大小
client_body_buffer_size 256k;
}
11.记第9点的问题,拓展思路,做分布式锁解决重复调度问题:
采用redission做分布式锁:
@Transactional
// 每小时
// @Scheduled(cron = "0 0 0/1 * * ? ")
// 每分钟
// @Scheduled(cron = "30 * * * * ? ")
@Scheduled(cron = "* * * * * ?")
public void sendEmail() throws MessagingException {
//分布式锁
RLock lock = redissonClient.getLock("seheduled_lock");
try {
// 抢锁,5:等待5s,自旋5秒,最长持锁60s
boolean b = lock.tryLock(5, 60, TimeUnit.SECONDS);
if(b){
log.info(DateUtils.getNowDate()+"成功抢到锁------------");
Thread.sleep(10000);
}else{
log.info(DateUtils.getNowDate()+"没抢到");
}
MsgEmail msgEmail = new MsgEmail();
msgEmail.setStatus(0);
List<MsgEmail> msgEmails = emailService.selectMsgEmailList(msgEmail);
if (msgEmails.size() == 0) {
System.out.println("没有需要发送邮件");
return;
}
/**
* todo:后期sql里做batch批量更新
* */
for (MsgEmail email : msgEmails) {
String[] strArray = genearteWord(email.getType(), email.getbId());
File[] files = new File[strArray.length];
for (int i = 0; i < strArray.length; i++) {
if (i == 0) {
email.setFilePath(strArray[i]);
email.setStatus(1);
}
files[i] = new File(strArray[i]);
}
emailService.updateMsgEmail(email);
//发送邮件
StringBuilder subject =new StringBuilder(email.getCreateBy())
.append("于")
.append(DateFormatUtils.format(email.getCreateTime(),"yyyy-MM-dd HH:mm:ss"))
.append("申请了")
.append(getType(Integer.valueOf(email.getType())));
SimpleEmailEntity simpleEmailEntity = new SimpleEmailEntity();
simpleEmailEntity.setTos(getSetTo(email.getType()));
simpleEmailEntity.setContent("请查收");
simpleEmailEntity.setSubject(subject.toString());
mailService.sendAttachMail1(simpleEmailEntity, files);
}
} catch (NumberFormatException e) {
throw new RuntimeException(e);
} catch (MessagingException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (lock.isHeldByCurrentThread()) {
log.info("解锁,执行finally");
lock.unlock();
}else{
log.info("没有获取到锁执行finally");
}
}
}
解决:
同时启动两个服务模拟测试:
1号机46秒抢到锁,执行10s等待,第二次无论那台机子抢到锁时间都是第56秒[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
二号机子46秒时没抢到,自旋5s,在52秒时仍然没抢到,因为1号机没释放,在56秒时成功抢到,