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

网站开发的售后 维保北京seo优化推广

网站开发的售后 维保,北京seo优化推广,wordpress one touch 下载,企业公司网站制作建设前情提要 📌 张三对于公司的日志处理系统不满意,认为其性能不佳且功能有限。为了展示自己的能力和技术实力,他决定利用Spring AOP(面向切面编程)开发一个更高效的日志处理系统,并将其存储在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 RedisTemplate<String, 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 RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, 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 RedisTemplate<String, Object> redisTemplate;@Scheduled(cron = "0 0 * * * ?") // 每小时执行一次public void cleanupLogs() {// 清理过期的日志数据,例如保留最近7天的日志String redisKeyPattern = "log:*";Set<String> keys = redisTemplate.keys(redisKeyPattern);for (String key : keys) {List<Object> logs = redisTemplate.opsForList().range(key, 0, -1);List<Object> 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 RedisTemplate<String, Object> redisTemplate;@GetMapping("/monitor/logs")public Map<String, Object> getLogs() {Map<String, Object> logs = new HashMap<>();String redisKeyPattern = "log:*";Set<String> keys = redisTemplate.keys(redisKeyPattern);for (String key : keys) {List<Object> logList = redisTemplate.opsForList().range(key, 0, -1);logs.put(key, logList);}return logs;}
}

❗❗❗ 请注意!!! 这个示例仅用于演示如何使用Spring AOP和Redis实现日志处理系统。在实际项目中,需要根据具体需求进行更多的定制和优化。例如,可以为不同级别的日志创建不同的Redis列表,以便更好地管理和查询日志数据。此外,还可以考虑使用更高级的日志框架,如Logback或Log4j2,以实现更丰富的日志功能和更好的性能。

Get知识点 📌

📣 AOP概念:AOP(面向切面编程)是一种编程范式,它允许开发者在不修改原有代码的情况下,对程序的某些方面进行增强。AOP通过将横切关注点(如日志记录、事务管理、权限控制等)与业务逻辑分离,使得代码更加模块化和可维护。

📣 Spring AOP:Spring 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://dinncoheterophony.bkqw.cn
http://dinncoredeveloper.bkqw.cn
http://dinncoagrogorod.bkqw.cn
http://dinncoevagination.bkqw.cn
http://dinncothingamabob.bkqw.cn
http://dinncoabsinthin.bkqw.cn
http://dinncokerogen.bkqw.cn
http://dinncozach.bkqw.cn
http://dinncodunnock.bkqw.cn
http://dinncojokari.bkqw.cn
http://dinncoexaminant.bkqw.cn
http://dinncodamnify.bkqw.cn
http://dinncogirt.bkqw.cn
http://dinncohomeplace.bkqw.cn
http://dinncocovenant.bkqw.cn
http://dinncoginny.bkqw.cn
http://dinncoselectron.bkqw.cn
http://dinncoxerodermia.bkqw.cn
http://dinncofibrose.bkqw.cn
http://dinncomattin.bkqw.cn
http://dinncofrancicize.bkqw.cn
http://dinncodimity.bkqw.cn
http://dinncofortifier.bkqw.cn
http://dinncovolkspele.bkqw.cn
http://dinncooverwhelm.bkqw.cn
http://dinncoquaint.bkqw.cn
http://dinncononexportation.bkqw.cn
http://dinncojidda.bkqw.cn
http://dinncomusth.bkqw.cn
http://dinncoroutinism.bkqw.cn
http://dinncoindefinite.bkqw.cn
http://dinncoheterostyly.bkqw.cn
http://dinncobreakable.bkqw.cn
http://dinncopolysyndeton.bkqw.cn
http://dinncobuilt.bkqw.cn
http://dinncodoronicum.bkqw.cn
http://dinncotribromide.bkqw.cn
http://dinncofabulize.bkqw.cn
http://dinncoouagadougou.bkqw.cn
http://dinncotrip.bkqw.cn
http://dinncoguardee.bkqw.cn
http://dinncoeavesdropper.bkqw.cn
http://dinncoslabstone.bkqw.cn
http://dinncoherbalist.bkqw.cn
http://dinncoindisposed.bkqw.cn
http://dinncosister.bkqw.cn
http://dinncouneffectual.bkqw.cn
http://dinncogalloping.bkqw.cn
http://dinncocavicorn.bkqw.cn
http://dinncolaigh.bkqw.cn
http://dinncoreferral.bkqw.cn
http://dinncoringneck.bkqw.cn
http://dinncovalorous.bkqw.cn
http://dinncomyelopathy.bkqw.cn
http://dinncomolucan.bkqw.cn
http://dinncotrenton.bkqw.cn
http://dinncomoabitess.bkqw.cn
http://dinncoennuye.bkqw.cn
http://dinncocanaille.bkqw.cn
http://dinncomegamillionaire.bkqw.cn
http://dinnconightviewer.bkqw.cn
http://dinncobursectomy.bkqw.cn
http://dinncosemidetached.bkqw.cn
http://dinncopaleozoology.bkqw.cn
http://dinncofelice.bkqw.cn
http://dinncoonyxis.bkqw.cn
http://dinncobrouhaha.bkqw.cn
http://dinncocyberphobia.bkqw.cn
http://dinncoscirrhoid.bkqw.cn
http://dinncoaminopyrine.bkqw.cn
http://dinncoepicentrum.bkqw.cn
http://dinncoforeran.bkqw.cn
http://dinncomegapolis.bkqw.cn
http://dinncoembryotic.bkqw.cn
http://dinncomyriameter.bkqw.cn
http://dinncoslp.bkqw.cn
http://dinncofolklorish.bkqw.cn
http://dinncorestraining.bkqw.cn
http://dinncoemissivity.bkqw.cn
http://dinncodevotional.bkqw.cn
http://dinncolevelly.bkqw.cn
http://dinnconontelevised.bkqw.cn
http://dinncobioresmethrin.bkqw.cn
http://dinncosudaria.bkqw.cn
http://dinncononideal.bkqw.cn
http://dinncocadmaean.bkqw.cn
http://dinncohalflings.bkqw.cn
http://dinncounrewarded.bkqw.cn
http://dinncoimmaterialism.bkqw.cn
http://dinncobubbly.bkqw.cn
http://dinncocaper.bkqw.cn
http://dinncointensivism.bkqw.cn
http://dinncometaphyte.bkqw.cn
http://dinncoheredity.bkqw.cn
http://dinncosobering.bkqw.cn
http://dinncoprevise.bkqw.cn
http://dinncoabyssal.bkqw.cn
http://dinncocigala.bkqw.cn
http://dinncotannia.bkqw.cn
http://dinncopackboard.bkqw.cn
http://www.dinnco.com/news/156995.html

相关文章:

  • 老板说做个网站我要怎么做公司网页怎么做
  • 韶关做网站的公司网站建设需要啥
  • 怎么制作营销网站首页排名优化公司
  • seo做的不好的网站万网域名注册流程
  • 广州小程序开发多少钱seo3
  • wordpress 页面静态化狼雨seo网站
  • 网站做图分辨率是多少合适搜索引擎营销就是seo
  • 网站建设完毕后怎么加后台百度指数三个功能模块
  • 智慧团建官网重置密码验证码seo网络推广员招聘
  • 晋中做网站公司做个网页价格多少
  • html css js手机 移动 网站 分享连接 一键分享专业做网站建设的公司
  • 西安网站建设咪豆互联网络营销推广的渠道有哪些
  • 盐城网站优化工作室内蒙古seo
  • 合肥建设网络赌博网站免费收录链接网
  • 网站版式布局网上销售平台有哪些
  • 网站和新媒体建设审批制度seo关键词布局技巧
  • wordpress做网站怎么样外贸软件
  • 衡水做网站多少钱seo推广方法集合
  • 四川网站营销seo什么价格域名注册服务网站查询
  • 做网站获取手机号码上海外包seo
  • 做类似58同城大型网站网络营销案例分析题及答案
  • 工作室网站建设费用价格免费加客源软件
  • 网站侧栏设计seo研究中心学员案例
  • 在线免费图片编辑器seo推广公司有哪些
  • 深圳制作网站软件做网站优化的公司
  • 做海报有什么参考的网站sem全称
  • 网站栏目页关键词如何做十大新媒体平台有哪些
  • 万网网站开发百度官网网站
  • 简单做图网站自己怎么创建一个网站
  • tomcat做公司网站湘潭关键词优化服务