Author: Wang Bei
Always found recently pay out coupons to send out, but found that slow, so want to add a sentinel surveillance, statistics, and a lot of demand in the group has sent a coupon, often encounter send abnormal situation, so, want to send packaging for coupons a public method is invoked, below is the encapsulation of public methods:
public CouponResponse<BatchDispatchResult> sendCoupon(List<String> reedcodeList,String accountId,AntiInfoVO antiInfoVO){ // Dubbo interface couponResponseVo = couponComposeFacade.batchDispatchCouponByRedeemCodeList(reedcodeList, accountId, antiInfoVO, 1); // Sentry monitorif(couponResponseVo ! = null && couponResponseVo.isNotSuccess()){ StatsTool.onIntegerKey1Value1Stats("couponSendFail", 1);
}
return couponResponseVo;
}Copy the code
The method is very simple, is to encapsulate, add a monitoring, but found that the code calls the issuing dubbo interface a lot of places, to change a lot of places, so I thought of facing the section programming AOP, Here a brief introduction of AOP (quoted from http://www.cnblogs.com/hongwz/p/5764917.html).
AOP (Aspect Oriented Programming), namely Aspect Oriented Programming, can be said to be OOP (Object Oriented Programming) complement and perfect. OOP introduces concepts such as encapsulation, inheritance, and polymorphism to build a hierarchy of objects that simulate a collection of common behaviors. OOP, however, allows developers to define vertical relationships, but not horizontal ones, such as logging. Logging code tends to be horizontally scattered across all object hierarchies, regardless of the core functionality of the object to which it corresponds. This scattering of unrelated code, known as cross cutting, is also true for other types of code such as security, exception handling, and interface call monitoring. In OOP design, it leads to a lot of code duplication, which is not conducive to reuse of individual modules.
AOP, on the other hand, uses a technique called “crosscutting” to rip open the insides of wrapped objects and encapsulate common behavior that affects multiple classes into a reusable module named “Aspect, “or Aspect. The so-called “facets”, simply speaking, are those unrelated to the business, but are encapsulated by the logic or responsibility of business modules, which is convenient to reduce the repetitive code of the system, reduce the degree of coupling between modules, and is conducive to the future operability and maintainability.
Using a “crosscutting” technique, AOP divides a software system into two parts: core concerns and crosscutting concerns. The main flow of business processing is the core concern, and the less relevant part is the crosscutting concern. One characteristic of crosscutting concerns is that they often occur in multiple locations of the core concern, while the locations are basically similar, such as permissions, logs, and things. AOP’s role is to separate the various concerns in a system, separating core concerns from crosscutting concerns.
So, just do it, and soon the code comes out
@aspect public class CouponWarnAspect {/** ** */ @pointcut (value ="execution(com.netease.kaola.compose.coupon.vo.CouponResponse
>"
+
" com.netease.kaola.compose.coupon.provider.CouponComposeFacade.*(..) )")
public void couponWarnPoint(){} /** */ @pointcut (value = value)"execution(com.netease.kaola.compose.coupon.vo.CouponResponse<com.netease.kaola.compose.coupon.vo.BatchDispatchResult>" +
" com.netease.kaola.compose.coupon.provider.CouponComposeFacade.*(..) )")
public void couponBatchWarnPoint(){}
@Around(value = "couponWarnPoint()")
public CouponResponse<List<CouponVO>> couponWarn(ProceedingJoinPoint pjp) throws Throwable {
try {
CouponResponse<List<CouponVO>> couponResponse = (CouponResponse) pjp.proceed();
if(couponResponse ! = null && couponResponse.isNotSuccess()){ StatsTool.onIntegerKey1Value1Stats("couponSendFail", 1); }return couponResponse;
} catch (Throwable throwable) {
StatsTool.onIntegerKey1Value1Stats("couponSendError", 1); throw throwable; } } @Around(value ="couponBatchWarnPoint()")
public CouponResponse<BatchDispatchResult> couponBatchWarn(ProceedingJoinPoint pjp) throws Throwable {
try {
CouponResponse<BatchDispatchResult> couponResponse = (CouponResponse) pjp.proceed();
if(couponResponse ! = null && couponResponse.isNotSuccess()){ StatsTool.onIntegerKey1Value1Stats("couponBatchSendFail", 1); }return couponResponse;
} catch (Throwable throwable) {
StatsTool.onIntegerKey1Value1Stats("couponBatchSendError", 1); throw throwable; }}}Copy the code
What a neat way to add sentry monitoring without fiddling with the original logic. We start by defining the pointcut, which specifies a set of join points that Adivce will raise. We define two pointcuts based on regular matches. The first entry point matching is com.netease.kaola.com pose. Coupon. The provider. The return type is CouponComposeFacade Com.netease.kaola.com pose. Coupon. Vo. CouponResponse < Java. Util. List < com.netease.kaola.com pose. Coupon. Vo. CouponVO > >, From the current system dubbo call, matching is a single method of issuing bonds, The second pointcut matches Com.netease.kaola.com pose. Coupon. The provider. In the return type is com.netease.kaola.com CouponComposeFacade pose. Coupon. Vo. CouponResponse < co M.netease.kaola.com pose. Coupon. Vo. BatchDispatchResult > method, from the current system call dubbo, matching method is sending bulk vouchers.
Once the pointcuts are defined, they can be referenced in Advice, or they can be specified directly in Advice through regular matches. Advice, including before, AfterReturning, AfterThrowing, and around. We use the wrap here, the wrap is to call the target method, before and after the call can do some of the operations we need, such as the above, after calling the target method by judging the return code and exceptions to add sentinel monitoring.
Finally, create the proxy class with the Spring configuration and aspect annotations:
<aop:aspectj-autoproxy proxy-target-/>
<bean id="couponWarnAspect" ></bean>Copy the code
To test it out, the coupon delivery failure was perfectly displayed on sentinel:
Through this, probably summed up, programming can not step by step, think about it, you will find that there are many shortcuts to take, so as to efficiently design the system more robust and maintainable, technology has also been breakthrough and improvement, why not?
Netease Cloud Free experience pavilion, 0 cost experience 20+ cloud products!
For more information about NETEASE’s r&d, product and operation experience, please visit netease Cloud Community.
[5.19 offline activities]Docker Meetup hangzhou — embrace Kubernetes, container deep practice [recommendation] on the cloud, microservialization and DevOps, less detour way [recommendation] visual designer evolution