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

给网站做网站中山快速建站合作

给网站做网站,中山快速建站合作,网商之窗官网,游戏制作公司开发前情提要 #x1f4cc; 张三对于公司的日志处理系统不满意#xff0c;认为其性能不佳且功能有限。为了展示自己的能力和技术实力#xff0c;他决定利用Spring AOP#xff08;面向切面编程#xff09;开发一个更高效的日志处理系统#xff0c;并将其存储在Redis中。 首先… 前情提要 张三对于公司的日志处理系统不满意认为其性能不佳且功能有限。为了展示自己的能力和技术实力他决定利用Spring AOP面向切面编程开发一个更高效的日志处理系统并将其存储在Redis中。 首先张三分析了现有日志处理系统的不足之处如性能瓶颈、日志格式不统一、存储容量有限等。然后他开始着手设计和实现一个新的日志处理系统。 使用Spring AOP进行日志拦截张三利用Spring AOP的切面功能为需要记录日志的方法添加了一个切面。在这个切面中他可以捕获方法的调用信息如方法名、参数、返回值等并将这些信息作为日志内容。 日志格式化为了确保日志的一致性和可读性张三设计了一种统一的日志格式。他将日志分为不同的级别如DEBUG、INFO、WARN和ERROR并为每个级别设置了不同的颜色和标签。 Redis存储张三选择将日志存储在Redis中因为Redis是一个高性能的键值存储系统适合存储大量的日志数据。他为每个日志级别创建了一个Redis列表用于存储相应级别的日志。同时他还设置了一个定时任务定期清理过期的日志数据以保持存储空间的整洁。 监控与告警为了方便监控日志系统的运行状况张三还开发了一个简单的监控界面可以实时查看各个日志级别的数量、存储空间使用情况等信息。此外他还设置了一些告警规则当某个日志级别的数量超过阈值时会自动发送告警通知给相关人员。 经过一段时间的努力张三成功地完成了这个基于Spring AOP的日志处理系统并将其部署到了生产环境。公司同事对他的工作表示赞赏认为这个新的日志处理系统不仅提高了性能还提供了更多有用的功能。这无疑突显了张三的技术能力和对公司的贡献。 以下是一个简化的代码实现示例展示了如何使用Spring AOP和Redis来实现日志处理系统。 场景实现 创建一个日志切面类**LoggingAspect** 在此过程中我们创建了一个日志切面类LoggingAspect它会拦截指定包路径下的所有方法调用。在方法调用完成后它会将方法的调用信息如方法名、参数、返回值等作为日志内容并将这些信息传递给LogService进行处理。 import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;Aspect Component public class LoggingAspect {Autowiredprivate LogService logService;Pointcut(execution(* com.example.service.*.*(..)))public void logPointcut() {}AfterReturning(pointcut logPointcut(), returning result)public void logAfterReturning(JoinPoint joinPoint, Object result) {logService.log(joinPoint, result);} }创建一个日志服务类**LogService****** LogService负责将日志内容存储到Redis中。在这个示例中我们使用了RedisTemplate来操作Redis。我们将日志内容存储在名为log的Redis列表中。 import org.aspectj.lang.JoinPoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service;Service public class LogService {Autowiredprivate RedisTemplateString, Object redisTemplate;public void log(JoinPoint joinPoint, Object result) {String methodName joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();String logMessage String.format(Method: %s, Args: %s, Result: %s, methodName, Arrays.toString(args), result);// 将日志存储到RedisredisTemplate.opsForList().rightPush(log, logMessage);} }还可以为不同级别的日志创建不同的Redis列表 public class LogService {// ...public void log(JoinPoint joinPoint, Object result, LogLevel logLevel) {String methodName joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();String logMessage String.format(Method: %s, Args: %s, Result: %s, methodName, Arrays.toString(args), result);// 根据日志级别将日志存储到不同的Redis列表中String redisKey log: logLevel.name().toLowerCase();redisTemplate.opsForList().rightPush(redisKey, logMessage);} }也可以修改日志格式化 public class LogService {// ...public void log(JoinPoint joinPoint, Object result, LogLevel logLevel) {String methodName joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();String logMessage String.format([%s] Method: %s, Args: %s, Result: %s, logLevel, methodName, Arrays.toString(args), result);// 根据日志级别将日志存储到不同的Redis列表中String redisKey log: logLevel.name().toLowerCase();redisTemplate.opsForList().rightPush(redisKey, logMessage);} }配置RedisTemplate 最后我们配置了一个RedisTemplate Bean用于序列化和反序列化Redis的key和value值。这样我们就可以将日志内容以结构化的方式存储在Redis中并在需要时方便地进行查询和分析。 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;Configuration public class RedisConfig {Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) {RedisTemplateString, Object template new RedisTemplate();template.setConnectionFactory(factory);// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer new GenericJackson2JsonRedisSerializer();template.setValueSerializer(genericJackson2JsonRedisSerializer);template.setHashValueSerializer(genericJackson2JsonRedisSerializer);// 使用StringRedisSerializer来序列化和反序列化redis的key值StringRedisSerializer stringRedisSerializer new StringRedisSerializer();template.setKeySerializer(stringRedisSerializer);template.setHashKeySerializer(stringRedisSerializer);template.afterPropertiesSet();return template;} }定时任务 我们创建了一个定时任务LogCleanupTask它会定期清理过期的日志数据。我们使用了Spring的Scheduled注解来实现定时任务并使用RedisTemplate来操作Redis。在cleanupLogs方法中我们遍历所有的日志列表并根据日志的时间戳判断它们是否过期。如果过期则将其从Redis中移除。 import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;Component public class LogCleanupTask {Autowiredprivate RedisTemplateString, Object redisTemplate;Scheduled(cron 0 0 * * * ?) // 每小时执行一次public void cleanupLogs() {// 清理过期的日志数据例如保留最近7天的日志String redisKeyPattern log:*;SetString keys redisTemplate.keys(redisKeyPattern);for (String key : keys) {ListObject logs redisTemplate.opsForList().range(key, 0, -1);ListObject logsToRemove logs.stream().filter(log - isExpired(log)).collect(Collectors.toList());redisTemplate.opsForList().remove(key, 0, logsToRemove);}}private boolean isExpired(Object log) {// 判断日志是否过期例如根据日志的时间戳和当前时间进行比较// ...} }监控界面 我们创建了一个监控界面LogMonitorController它可以实时查看日志数据。我们使用了Spring的RestController注解来创建一个RESTful API并使用RedisTemplate来操作Redis。在getLogs方法中我们遍历所有的日志列表并将它们以JSON格式返回给客户端。客户端可以使用这些数据来实时监控日志系统的运行状况。 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;RestController public class LogMonitorController {Autowiredprivate RedisTemplateString, Object redisTemplate;GetMapping(/monitor/logs)public MapString, Object getLogs() {MapString, Object logs new HashMap();String redisKeyPattern log:*;SetString keys redisTemplate.keys(redisKeyPattern);for (String key : keys) {ListObject logList redisTemplate.opsForList().range(key, 0, -1);logs.put(key, logList);}return logs;} }❗❗❗ 请注意 这个示例仅用于演示如何使用Spring AOP和Redis实现日志处理系统。在实际项目中需要根据具体需求进行更多的定制和优化。例如可以为不同级别的日志创建不同的Redis列表以便更好地管理和查询日志数据。此外还可以考虑使用更高级的日志框架如Logback或Log4j2以实现更丰富的日志功能和更好的性能。 Get知识点 AOP概念AOP面向切面编程是一种编程范式它允许开发者在不修改原有代码的情况下对程序的某些方面进行增强。AOP通过将横切关注点如日志记录、事务管理、权限控制等与业务逻辑分离使得代码更加模块化和可维护。 Spring AOPSpring AOP是Spring框架中的一个重要组件它提供了声明式的AOP支持。Spring AOP使用代理模式来实现AOP可以通过JDK动态代理或CGLIB代理来创建代理对象。Spring AOP支持多种类型的切面如前置通知、后置通知、异常通知、环绕通知等。 Aspect — 此注释将类定义为一个方面即关注点的模块化。该方面包含建议和要点。 Joinpoint — 连接点是程序执行中可以应用方面的一个点。在 Spring AOP 中连接点是方法调用。 Advice — 建议是某个方面在特定连接点上采取的行动。有几种类型的建议例如“之前”、“之后”、“周围”等。 Pointcut — 切点是一组应应用方面的连接点。它定义了一个模式该模式与方面应截获的方法相匹配。可以使用表达式或注释来定义切点。 下面是 Spring AOP 注解的示例 Aspect Component public class LoggingAspect {Before(execution(public * com.example.myapp.service.*.*(..)))public void logBefore(JoinPoint joinPoint) {System.out.println(Before joinPoint.getSignature().getName() method);}After(execution(public * com.example.myapp.service.*.*(..)))public void logAfter(JoinPoint joinPoint) {System.out.println(After joinPoint.getSignature().getName() method);}Around(execution(public * com.example.myapp.service.*.*(..)))public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println(Before joinPoint.getSignature().getName() method);Object result joinPoint.proceed();System.out.println(After joinPoint.getSignature().getName() method);return result;}Pointcut(execution(public * com.example.myapp.service.*.*(..)))public void serviceMethods() {} }写在最后 日志使用在现代软件开发中非常重要它可以帮助开发者和系统管理员监控程序运行状态、排查问题和调试代码。但是日志使用也存在一些缺点如干扰员工工作、信息整理工作量大、主观色彩和日志格式不统一等。因此在使用日志时需要权衡其优缺点选择合适的日志记录方法并确保日志数据的准确性和完整性。 ​ ​
http://www.hyszgw.com/news/105142.html

相关文章:

  • 做门户网站需要什么新建文档怎么做网站
  • 照明灯具类企业网站免费制作网站和网页
  • 购物网站模板免费下载做模块高考题的网站
  • 电子商务网站建设与维护试卷答案阜南县建设局网站
  • 网站建设的作用是什么个人网站审批
  • 封面型网页网站有哪些上传网站空间
  • 云南住房和城乡建设厅网站首页wordpress 定制
  • 镇海区住房建设网站怎么查h5彩票网站怎么做
  • 扬州哪家做网站好东莞建站网站
  • 包头市建设工程安全监督站网站企业网站推广目标
  • 网站开发用什么语言好河北省住房和城乡建设部网站
  • 网站备案关闭深圳手机企业网站设计
  • 想接网站自己做深圳龙华邮编
  • 12306网站架构凡科小程序免费制作平台
  • 网站建设和维护怎么学深圳有什么网站
  • wordpress 手工网站ccyycom服务器
  • 网站怎么做百度权重拓客软件
  • 做地方门户网站的资质网站收录情况查询
  • 中国效能建设网站网站增加关键词
  • 哪个公司的网站做的好seo推广专员工作内容
  • 网站设计与网站建设a卷网站ui设计
  • 重庆做网站怎么做呀哪个网站做logo
  • 网站建设作为北京旧房改造装修哪家好
  • 做网站的业务分析成都锦江建设局网站
  • 网站描述优化网站后台管理系统 源码
  • 建设网站怎么建设分类快代理ip
  • 电子ic网站建设备案网站内容简介
  • 唐山网站制作专业seo营销的策略有哪些
  • 甘肃企业模板建站信息asp网站防注入
  • 在什么网站可以接国外的模具做很多搜索词网站怎样做