当前位置: 首页 > news >正文

wordpress 批量文章/手机网站怎么优化

wordpress 批量文章,手机网站怎么优化,58推广网站建设有用吗,建设一个电子商务网站的基本步骤概览 日志服务是项目中必不可少的的一个,通过面向切面的编程,我们可以统一处理日志逻辑,现在市面上有很多优秀的日志框架,但是者不妨碍我们通过自己编写的日志框架了解其底层原理。 思路 aop采集日志把日志存入一个有序的、堵塞的…

概览

日志服务是项目中必不可少的的一个,通过面向切面的编程,我们可以统一处理日志逻辑,现在市面上有很多优秀的日志框架,但是者不妨碍我们通过自己编写的日志框架了解其底层原理。

思路

在这里插入图片描述

  1. aop采集日志
  2. 把日志存入一个有序的、堵塞的队列中
  3. 单独开一个线程通过异步方式,把队列中的日志写入到文件中。

核心代码

  • pom配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>log-manager</artifactId><version>0.0.1-SNAPSHOT</version><name>log-manager</name><description>log-manager</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--   引入aop插件包     --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><version>2.7.2</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.7</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
  • 日志写入处理类
package com.example.logmanager.utils;import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;@Component
public class LogManager {private static BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>();private static final String filePath = "C:\\Users\\Administrator\\Desktop\\123.log";public LogManager() {new LogThread().start();}public static void addLog(String msg){blockingQueue.add(msg);}class LogThread extends Thread {@Overridepublic void run() {while (true) {String log = blockingQueue.poll();if(StringUtils.hasLength(log)) {// 写入本地FileUtils.writeText(filePath, log, true);}}}}
}
  • 切面逻辑

package com.example.logmanager.aop;import com.example.logmanager.utils.LogManager;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;@Component
@Aspect
public class AopLog {@Pointcut("execution(public * com.example.logmanager.controller..*(..))")public void log(){};@Before("log()")public void before(JoinPoint joinPoint){ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = Objects.requireNonNull(requestAttributes).getRequest();LogManager.addLog("[请求的url]:" + request.getRequestURL());LogManager.addLog("[请求的Ip]:" + request.getRemoteAddr());LogManager.addLog("[类名 Class]:" + joinPoint.getSignature().getDeclaringTypeName());}
}
  • 文件操作工具类

package com.example.logmanager.utils;import java.io.*;
import java.nio.channels.FileChannel;public class FileUtils {/*判断文件是否存在*/public static boolean isExists(String filePath) {File file = new File(filePath);return file.exists();}/*判断是否是文件夹*/public static boolean isDir(String path) {File file = new File(path);if (file.exists()) {return file.isDirectory();} else {return false;}}/*** 文件或者目录重命名** @param oldFilePath 旧文件路径* @param newName     新的文件名,可以是单个文件名和绝对路径* @return*/public static boolean renameTo(String oldFilePath, String newName) {try {File oldFile = new File(oldFilePath);//若文件存在if (oldFile.exists()) {//判断是全路径还是文件名if (newName.indexOf("/") < 0 && newName.indexOf("\\") < 0) {//单文件名,判断是windows还是Linux系统String absolutePath = oldFile.getAbsolutePath();if (newName.indexOf("/") > 0) {//Linux系统newName = absolutePath.substring(0, absolutePath.lastIndexOf("/") + 1) + newName;} else {newName = absolutePath.substring(0, absolutePath.lastIndexOf("\\") + 1) + newName;}}File file = new File(newName);//判断重命名后的文件是否存在if (file.exists()) {System.out.println("该文件已存在,不能重命名");} else {//不存在,重命名return oldFile.renameTo(file);}} else {System.out.println("原该文件不存在,不能重命名");}} catch (Exception e) {e.printStackTrace();}return false;}/*文件拷贝操作*/public static void copy(String sourceFile, String targetFile) {File source = new File(sourceFile);File target = new File(targetFile);target.getParentFile().mkdirs();FileInputStream fis = null;FileOutputStream fos = null;FileChannel in = null;FileChannel out = null;try {fis = new FileInputStream(source);fos = new FileOutputStream(target);in = fis.getChannel();//得到对应的文件通道out = fos.getChannel();//得到对应的文件通道in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道} catch (IOException e) {e.printStackTrace();} finally {try {if (out != null) {out.close();}if (in != null) {in.close();}if (fos != null) {fos.close();}if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}}}/*读取Text文件操作*/public static String readText(String filePath) {String lines = "";try {FileReader fileReader = new FileReader(filePath);BufferedReader bufferedReader = new BufferedReader(fileReader);String line = null;while ((line = bufferedReader.readLine()) != null) {lines += line + "\n";}} catch (Exception e) {e.printStackTrace();}return lines;}/*写入Text文件操作*/public static void writeText(String filePath, String content, boolean isAppend) {FileOutputStream outputStream = null;OutputStreamWriter outputStreamWriter = null;BufferedWriter bufferedWriter = null;try {outputStream = new FileOutputStream(filePath, isAppend);outputStreamWriter = new OutputStreamWriter(outputStream);bufferedWriter = new BufferedWriter(outputStreamWriter);bufferedWriter.write(content);bufferedWriter.newLine();} catch (IOException e) {e.printStackTrace();} finally {try {if (bufferedWriter != null) {bufferedWriter.close();}if (outputStreamWriter != null) {outputStreamWriter.close();}if (outputStream != null) {outputStream.close();}} catch (Exception e) {e.printStackTrace();}}}}
  • 测试controller
package com.example.logmanager.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test")
public class TestController {@GetMappingpublic String test(){return "succ";}
}

结果展示

在这里插入图片描述

http://www.jmfq.cn/news/4905937.html

相关文章:

  • 优秀北京网站建设/百度推广电话客服24小时
  • 专用车网站建设/2019年 2022疫情爆发
  • 网站开发回访话术/营销推广运营
  • 网站注册页面怎么做/全渠道营销管理平台
  • 网站的记住密码功能怎么做/百度seo刷排名网址
  • 商丘在线商城/网站怎么优化seo
  • 免费设计装修网站/大连百度关键词排名
  • 东莞北京网站建设价格低/如何宣传推广自己的产品
  • 大型网站空间费用/短视频精准获客
  • 国外网站在国内做镜像站点/张掖seo
  • flex 做网站/友情视频
  • 漯河市住房和城乡建设局网站/seo关键词布局技巧
  • jsp动态网站开发与实例/如何自己做一个软件
  • 网站收录怎么删/全球网络营销公司排行榜
  • wordpress 移动支付/网站推广与优化平台
  • 深圳营销型网站需要多少钱/前端seo是什么
  • 网站首页的名字通常是/公司建网站需要多少钱
  • 制作网站公司网址/游戏推广员上班靠谱吗
  • 国外 网站页面/百度收录提交网站后多久收录
  • 二七区做网站/厨师培训学校
  • 重庆沙坪坝房价/西安seo外包行者seo
  • 分析网站建设前期的seo准备工作/网上推广app怎么做
  • 光谷做网站推广公司/seo上首页排名
  • wordpress全站/seo资料网
  • 网站new图标/一般网络推广应该怎么做
  • 专业柳州网站建设/新闻实时报道
  • 如何用wordpress插件/怎么seo快速排名
  • 哪家网站做的好/有必要买优化大师会员吗
  • 看男女做那个真实视频网站/互联网推广怎么找渠道
  • 河南郑州百度网站建设/18款禁用看奶app入口