zoukankan      html  css  js  c++  java
  • springcloud 服务注册、反注册 AOP 拦截,实现自定义功能


    @Aspect
    @Component
    @Order(1000)
    public class EurekaServerAspect
    {
    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    IRegisterSevice registerSevice;

    @Pointcut("execution(public * org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration.peerAwareInstanceRegistry(..))")
    public void instanceAspect(){}

    @Pointcut("execution(public * org.springframework.cloud.netflix.eureka.server.InstanceRegistry.register(..))")
    public void registerAspect(){}

    @Pointcut("execution(public * org.springframework.cloud.netflix.eureka.server.InstanceRegistry.cancel(..))")
    public void cancelAspect(){}

    @Pointcut("execution(public * org.springframework.cloud.netflix.eureka.server.InstanceRegistry.renew(..))")
    public void renewAspect(){}


    @Around("instanceAspect()")
    public Object instance(ProceedingJoinPoint joinPoint) throws Throwable
    {
    logger.info("aspect joinPoint = "+joinPoint);
    //暂时不做修改,待后续扩展
    return joinPoint.proceed();
    }






    @Around("registerAspect()")
    public void register(ProceedingJoinPoint joinPoint) throws Throwable
    {
    logger.info("registerAspect()");
    logger.info("aspect joinPoint = "+joinPoint);
    Object[] args =joinPoint.getArgs();
    if(args!=null&&args.length>0)
    {
    InstanceInfo arg0 = (InstanceInfo) args[0];
    if (registerSevice.containInstanceInfo(arg0.getInstanceId(), arg0.getAppName()))
    {
    return ;
    }
    // 注册新服务实例,保存入数据库
    registerSevice.insertMicroServerInstance((InstanceInfo)arg0);
    }
    //加入自定义的注册的拦截逻辑
    joinPoint.proceed();
    }

    @Around("cancelAspect()")
    public Object cancel(ProceedingJoinPoint joinPoint) throws Throwable
    {
    logger.info("cancelAspect()");
    logger.info("aspect joinPoint = "+joinPoint);
    Object[] args =joinPoint.getArgs();
    if(args!=null&&args.length>0)
    {
    String appName = (String) args[0];
    String serverId = (String) args[1];
    registerSevice.cancelInstanceInfo(serverId, appName);
    }

    //加入自定义的反注册逻辑
    return joinPoint.proceed();
    }

    @Around("renewAspect()")
    public Object renew(ProceedingJoinPoint joinPoint) throws Throwable
    {
    logger.info("renewAspect()");
    logger.info("aspect joinPoint = "+joinPoint);
    //加入自定义的反注册逻辑
    Object[] args =joinPoint.getArgs();
    if(args!=null&&args.length>0)
    {
    String appName = (String) args[0];
    String serverId = (String) args[1];
    registerSevice.updateInstanceInfoHeartbeatTime(serverId, appName, System.currentTimeMillis(), null);;
    }
    return joinPoint.proceed();
    }

    }

  • 相关阅读:
    第六章:随机化(续1)
    第六章:随机化
    PAT甲组 1010 Radix (二分)
    关于我的2019年度总结
    Codeforces 567D:One-Dimensional Battle Ships(二分)
    Codeforces 567C:Geometric Progression(DP)
    Codeforces 567B:Berland National Library(模拟)
    HDU 4790:Just Random(容斥)
    Codeforces 450C:Jzzhu and Chocolate(贪心)
    Codeforces 450E:Jzzhu and Apples(构造,数学)
  • 原文地址:https://www.cnblogs.com/xiangjune/p/6846237.html
Copyright © 2011-2022 走看看