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();
}

}

转载于:https://www.cnblogs.com/xiangjune/p/6846237.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值