zrlw edited a comment on issue #8098:
URL: https://github.com/apache/dubbo/issues/8098#issuecomment-866111624


   1. 接口工程notifyApi.jar
   dubbo服务接口UserNotifyService:
   ```
   package com.test.notifyApi;
   public interface UserNotifyService {
       String getName(NotifyDto dto);
   }
   ```
   接口方法入参NotifyDto:
   ```
   package com.test.notifyApi;
   import java.io.Serializable;
   import javax.validation.constraints.NotNull;
   import lombok.Data;
   @Data
   public class NotifyDto implements Serializable {
       private static final long serialVersionUID = 1L;
       private String id;
       @NotNull
       private String sex;
   }
   ```
   2. consumer工程notifyConsumer.jar
   配置类DubboReferenceConfig:
   ```
   package com.test.notifyConsumer.config;
   import org.springframework.context.annotation.Configuration;
   import org.springframework.context.annotation.ImportResource;
   @Configuration
   @ImportResource({"classpath:notify.xml"})
   public class DubboReferenceConfig {
   }
   ```
   定义Dubbo Reference bean的notify.xml:
   ```
   <?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://www.springframework.org/schema/beans";
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo";
        xsi:schemaLocation="http://www.springframework.org/schema/beans        
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        
       http://dubbo.apache.org/schema/dubbo        
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd";>
       
       <dubbo:reference id="userNotifyService"
           interface="com.test.notifyApi.UserNotifyService"
           check="false" timeout="3000">
           <dubbo:method name="getName" validation="true"
               retries="0" async="true"
               oninvoke="consumerNotifyServiceImpl.onInvoke"
               onreturn="consumerNotifyServiceImpl.onReturn"
               onthrow="consumerNotifyServiceImpl.onThrow" />
       </dubbo:reference>               
   
   </beans>
   ```
   事件通知接口ConsumerNotifyService:
   ```
   package com.test.notifyConsumer.service;
   import com.test.notifyApi.NotifyDto;
   public interface ConsumerNotifyService {
       void onInvoke(NotifyDto dto);
       void onReturn(String name, NotifyDto dto);    
       void onThrow(Throwable e, NotifyDto dto);
       
       void getName(NotifyDto dto);
   }
   ```
   事件通知接口实现类ConsumerNotifyServiceImpl :
   ```
   package com.test.notifyConsumer.service.impl;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.context.ApplicationListener;
   import org.springframework.context.event.ContextRefreshedEvent;
   import org.springframework.stereotype.Component;
   import com.test.notifyApi.NotifyDto;
   import com.test.notifyApi.UserNotifyService;
   import com.test.notifyConsumer.aspect.MethodAnnotation;
   import com.test.notifyConsumer.service.ConsumerNotifyService;
   import lombok.extern.slf4j.Slf4j;
   
   @Slf4j
   @Component
   public class ConsumerNotifyServiceImpl implements ConsumerNotifyService {
   
       @Autowired
       UserNotifyService userNotifyService;
       
       @Autowired
       NotifyWork work;
       
       @MethodAnnotation("invoke-annotation")
       public void onInvoke(NotifyDto dto) {
           log.info("invoke for {}", dto);
           work.workInvoke(dto);
       }
   
       @MethodAnnotation("return-annotation")
       public void onReturn(String name, NotifyDto dto) {
           log.info("return {} for {}", name, dto);
           work.workReturn(name, dto);
       }
   
       @MethodAnnotation("throw-annotation")
       public void onThrow(Throwable e, NotifyDto dto) {
           log.info("throw exception {} for {}", e, dto);
           work.workThrow(e, dto);
       }
   
       public void getName(NotifyDto dto) {
           userNotifyService.getName(dto);
       }
   }
   ```
   注解MethodAnnotation:
   ```
   package com.test.notifyConsumer.aspect;
   import java.lang.annotation.Documented;
   import java.lang.annotation.ElementType;
   import java.lang.annotation.Inherited;
   import java.lang.annotation.Retention;
   import java.lang.annotation.RetentionPolicy;
   import java.lang.annotation.Target;
   @Target({ElementType.METHOD, ElementType.TYPE})
   @Retention(RetentionPolicy.RUNTIME)
   @Inherited
   @Documented
   public @interface MethodAnnotation {
       String value() default "";
   }
   ```
   切面MethodAspect :
   ```
   package com.test.notifyConsumer.aspect;
   import org.aspectj.lang.ProceedingJoinPoint;
   import org.aspectj.lang.annotation.Around;
   import org.aspectj.lang.annotation.Aspect;
   import org.springframework.stereotype.Component;
   
   import lombok.extern.slf4j.Slf4j;
   @Slf4j
   @Component
   @Aspect
   public class MethodAspect {
       @Around("@annotation(annotation)")
       public Object aroundMethod(ProceedingJoinPoint pjp, MethodAnnotation 
annotation) throws Throwable {
           log.info("aspect {} for {}", annotation.value(), 
pjp.getSignature().getName());
           return pjp.proceed();
       }
   }
   ```
   测试类:
   ```
   package com.test.notifyConsumer;
   import javax.validation.ConstraintViolationException;
   import org.junit.AfterClass;
   import org.junit.Test;
   import org.junit.runner.RunWith;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.boot.test.context.SpringBootTest;
   import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
   import com.test.notifyApi.NotifyDto;
   import com.test.notifyConsumer.service.ConsumerNotifyService;
   import lombok.extern.slf4j.Slf4j;
   
   @Slf4j
   @RunWith(SpringJUnit4ClassRunner.class)
   @SpringBootTest(classes = App.class)
   public class AppTest
   {
       
       @Autowired
       ConsumerNotifyService consumerNotifyService;
       
       @Test
       public void test1() {
           NotifyDto dto = new NotifyDto();
           dto.setId("12345");
           dto.setSex("M");
           consumerNotifyService.getName(dto);        
       }
       
       @Test(expected = ConstraintViolationException.class)
       public void test2() {
           NotifyDto dto = new NotifyDto();
           dto.setId("67890");
           consumerNotifyService.getName(dto);        
       }
   
       @Test
       public void test3() {
           NotifyDto dto = new NotifyDto();
           dto.setSex("W");
           consumerNotifyService.getName(dto);        
       }
       
       @AfterClass
       public static void waitForNotify() throws InterruptedException {
           log.info("WAIT 1s");        
           Thread.sleep(1000);
       }
   }
   ```
   
   3. provider工程
   服务UserNotifyServiceImpl:
   ```
   package com.test.notifyService.service.impl;
   import org.apache.commons.lang3.StringUtils;
   import org.apache.dubbo.config.annotation.Service;
   import org.apache.dubbo.rpc.RpcException;
   import com.test.notifyApi.NotifyDto;
   import com.test.notifyApi.UserNotifyService;
   import lombok.extern.slf4j.Slf4j;
   
   @Slf4j
   @Service
   public class UserNotifyServiceImpl implements UserNotifyService {
       @Override
       public String getName(NotifyDto dto) {
           log.info("getname param={}", dto);
           if (StringUtils.isEmpty(dto.getId())) {
               throw new RpcException("null id");
           }
           return "name-" + dto.getId() + "(" + dto.getSex() + ")";
       }
   }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to