Selaa lähdekoodia

dubbo拦截器案例

huangbo 7 vuotta sitten
vanhempi
commit
a31e945d23

+ 7 - 0
ipu-service-demo/src/main/java/com/ai/ipu/service/demo/IBizService.java

@ -16,4 +16,11 @@ public interface IBizService {
16 16
     * @desc: 用户信息查询
17 17
     */
18 18
    public IData queryUserInfo(IData param);
19
    
20
    /**
21
     * @author huangbo@asiainfo.com
22
     * @title: triggerException
23
     * @desc: 触发业务异常
24
     */
25
    public IData triggerException(IData param);
19 26
}

+ 5 - 0
ipu-service-demo/src/main/java/com/ai/ipu/service/demo/impl/BizService.java

@ -22,4 +22,9 @@ public class BizService implements IBizService{
22 22
        result.put("msg", "成功返回结果!");
23 23
        return result;
24 24
    }
25
    
26
    public IData triggerException(IData param) {
27
        // TODO Auto-generated method stub
28
        throw new RuntimeException("业务异常");
29
    }
25 30
}

+ 37 - 0
ipu-service-demo/src/main/java/com/ai/ipu/service/demo/interceptor/ServiceInterceptor.java

@ -0,0 +1,37 @@
1
package com.ai.ipu.service.demo.interceptor;
2

3
import com.ai.ipu.service.dubbo.interceptor.IServiceInterceptor;
4
import com.ailk.common.data.IData;
5

6
/**
7
 * @author huangbo@asiainfo.com
8
 * @team IPU
9
 * @date 2018年3月7日下午3:58:40
10
 * @desc 拦截器服务案例,onInvoke没有测试通过
11
 */
12
public class ServiceInterceptor implements IServiceInterceptor {
13

14
    public void onInvoke(Object ... params) {
15
        // TODO Auto-generated method stub
16
        for(Object param : params){
17
            System.out.println("服务入参:" + param);
18
        }
19
    }
20

21
    public void onReturn(Object result, Object ... params) {
22
        // TODO Auto-generated method stub
23
        System.out.println("服务返回:" + result);
24
        for(Object param : params){
25
            System.out.println("服务入参:" + param);
26
        }
27
    }
28

29
    public void onThrow(Throwable ex, Object ... params) {
30
        // TODO Auto-generated method stub
31
        System.out.println("异常情况:" + ex);
32
        for(Object param : params){
33
            System.out.println("服务入参:" + param);
34
        }
35
    }
36

37
}

+ 3 - 0
ipu-service-demo/src/main/resources/dubbo-provider-mult.xml

@ -18,11 +18,14 @@
18 18
    <!-- 服务注册配置 start=========================== -->
19 19
    <dubbo:service interface="com.ai.ipu.service.demo.IBizService" ref="bizService"/>
20 20
    <bean id="bizService" class="com.ai.ipu.service.demo.impl.BizService"/>
21
    <!-- 服务上下文 -->
21 22
    <dubbo:service interface="com.ai.ipu.service.demo.IContextService" ref="contextService"/>
22 23
    <bean id="contextService" class="com.ai.ipu.service.demo.impl.ContextService"/>
24
    <!-- 异步服务 -->
23 25
    <dubbo:service interface="com.ai.ipu.service.demo.IAsyncService" ref="asyncService"/>
24 26
    <bean id="asyncService" class="com.ai.ipu.service.demo.impl.AsyncService"/>
25 27
    
28
    <!-- 服务回调 -->
26 29
    <dubbo:service interface="com.ai.ipu.service.demo.ICallbackService" ref="callbackService" connections="1" callbacks="1000">
27 30
	    <dubbo:method name="addListener">
28 31
	        <dubbo:argument index="1" callback="true" />

+ 3 - 0
ipu-service-demo/src/main/resources/dubbo-provider-simple.xml

@ -25,11 +25,14 @@
25 25
    <!-- 服务注册配置 start=========================== -->
26 26
    <dubbo:service interface="com.ai.ipu.service.demo.IBizService" ref="bizService"/>
27 27
    <bean id="bizService" class="com.ai.ipu.service.demo.impl.BizService"/>
28
    <!-- 服务上下文 -->
28 29
    <dubbo:service interface="com.ai.ipu.service.demo.IContextService" ref="contextService"/>
29 30
    <bean id="contextService" class="com.ai.ipu.service.demo.impl.ContextService"/>
31
    <!-- 异步服务 -->
30 32
    <dubbo:service interface="com.ai.ipu.service.demo.IAsyncService" ref="asyncService"/>
31 33
    <bean id="asyncService" class="com.ai.ipu.service.demo.impl.AsyncService"/>
32 34
    
35
    <!-- 服务回调 -->
33 36
	<dubbo:service interface="com.ai.ipu.service.demo.ICallbackService" ref="callbackService" connections="1" callbacks="1000">
34 37
	    <dubbo:method name="addListener">
35 38
	        <dubbo:argument index="1" callback="true" />

+ 3 - 0
ipu-service-demo/src/main/resources/dubbo-provider-zoo.xml

@ -23,11 +23,14 @@
23 23
    <!-- 服务注册配置 start=========================== -->
24 24
    <dubbo:service interface="com.ai.ipu.service.demo.IBizService" ref="bizService"/>
25 25
    <bean id="bizService" class="com.ai.ipu.service.demo.impl.BizService"/>
26
    <!-- 服务上下文 -->
26 27
    <dubbo:service interface="com.ai.ipu.service.demo.IContextService" ref="contextService"/>
27 28
    <bean id="contextService" class="com.ai.ipu.service.demo.impl.ContextService"/>
29
    <!-- 异步服务 -->
28 30
    <dubbo:service interface="com.ai.ipu.service.demo.IAsyncService" ref="asyncService"/>
29 31
    <bean id="asyncService" class="com.ai.ipu.service.demo.impl.AsyncService"/>
30 32
    
33
    <!-- 服务回调 -->
31 34
    <dubbo:service interface="com.ai.ipu.service.demo.ICallbackService" ref="callbackService" connections="1" callbacks="1000">
32 35
	    <dubbo:method name="addListener">
33 36
	        <dubbo:argument index="1" callback="true" />

+ 39 - 0
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/InterceptorServiceTest.java

@ -0,0 +1,39 @@
1
package com.ai.ipu.service.demo.impl;
2

3
import junit.framework.TestCase;
4

5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6

7
import com.ai.ipu.service.DubboServiceStart;
8
import com.ai.ipu.service.demo.IBizService;
9
import com.ailk.common.data.IData;
10
import com.ailk.common.data.impl.DataMap;
11

12
public class InterceptorServiceTest extends TestCase{
13
    private ClassPathXmlApplicationContext context;
14
    
15
    @Override
16
    protected void setUp() throws Exception {
17
        // TODO Auto-generated method stub
18
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
19
        context.start();
20
    }
21

22
    public void testInterceptorService() {
23
        // TODO Auto-generated method stub
24
        IBizService contextService = (IBizService)context.getBean("interceptorService"); // 获取远程服务代理
25
        IData param = new DataMap();
26
        param.put("key", "入参");
27
        IData result = contextService.queryUserInfo(param); // 执行远程方法
28
        System.out.println(result); // 显示调用结果
29
    }
30
    
31
    public void testTriggerException() {
32
        // TODO Auto-generated method stub
33
        IBizService contextService = (IBizService)context.getBean("interceptorService"); // 获取远程服务代理
34
        IData param = new DataMap();
35
        param.put("key", "入参");
36
        IData result = contextService.triggerException(param); // 执行远程方法
37
        System.out.println(result); // 显示调用结果
38
    }
39
}

+ 9 - 0
ipu-service-demo/src/test/resources/dubbo-consumer-mult.xml

@ -20,6 +20,15 @@
20 20
		<dubbo:method name="findFoo" async="true" timeout="3500"/>
21 21
		<dubbo:method name="findBar" async="true" timeout="5500"/>
22 22
	</dubbo:reference>
23
	<!-- 服务回调 -->
23 24
	<dubbo:reference id="callbackService" interface="com.ai.ipu.service.demo.ICallbackService" />
25
	
26
	<!-- 服务拦截 -->
27
	<bean id="serviceInterceptor" class="com.ai.ipu.service.demo.interceptor.ServiceInterceptor" />
28
	<dubbo:reference id="interceptorService" interface="com.ai.ipu.service.demo.IBizService">
29
		<!-- oninvoke="serviceInterceptor.onInvoke" -->
30
		<dubbo:method name="queryUserInfo" async="true" onreturn="serviceInterceptor.onReturn"/>
31
		<dubbo:method name="triggerException" onthrow="serviceInterceptor.onThrow"/>
32
	</dubbo:reference>
24 33
	<!-- 服务注册配置 end =========================== -->
25 34
</beans>

+ 22 - 10
ipu-service-demo/src/test/resources/dubbo-consumer-simple.xml

@ -1,24 +1,36 @@
1 1
<?xml version="1.0" encoding="UTF-8"?>
2 2
<beans xmlns="http://www.springframework.org/schema/beans"
3
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
3
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
5 4
	xsi:schemaLocation="http://www.springframework.org/schema/beans
6 5
	http://www.springframework.org/schema/beans/spring-beans.xsd
7 6
	http://code.alibabatech.com/schema/dubbo 
8 7
	http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
9 8
	<!-- 全局配置 start=========================== -->
10
	<dubbo:application name="ipu-service-demo"/>
9
	<dubbo:application name="ipu-service-demo" />
11 10
	<dubbo:registry address="127.0.0.1:20880" />
12 11
	<!-- 全局配置 end=========================== -->
13
	
12

14 13
	<!-- 服务注册配置 start=========================== -->
15
	<dubbo:reference id="bizService" interface="com.ai.ipu.service.demo.IBizService" />
16
	<dubbo:reference id="contextService" interface="com.ai.ipu.service.demo.IContextService" />
14
	<dubbo:reference id="bizService"
15
		interface="com.ai.ipu.service.demo.IBizService" />
16
	<dubbo:reference id="contextService"
17
		interface="com.ai.ipu.service.demo.IContextService" />
17 18
	<!-- 两级超时配置 -->
18
	<dubbo:reference id="asyncService" interface="com.ai.ipu.service.demo.IAsyncService" timeout="3000" >
19
		<dubbo:method name="findFoo" async="true" timeout="3500"/>
20
		<dubbo:method name="findBar" async="true" timeout="5500"/>
19
	<dubbo:reference id="asyncService"
20
		interface="com.ai.ipu.service.demo.IAsyncService" timeout="3000">
21
		<dubbo:method name="findFoo" async="true" timeout="3500" />
22
		<dubbo:method name="findBar" async="true" timeout="5500" />
23
	</dubbo:reference>
24
	<!-- 服务回调 -->
25
	<dubbo:reference id="callbackService"
26
		interface="com.ai.ipu.service.demo.ICallbackService" />
27
		
28
	<!-- 服务拦截 -->
29
	<bean id="serviceInterceptor" class="com.ai.ipu.service.demo.interceptor.ServiceInterceptor" />
30
	<dubbo:reference id="interceptorService" interface="com.ai.ipu.service.demo.IBizService">
31
		<!-- oninvoke="serviceInterceptor.onInvoke" -->
32
		<dubbo:method name="queryUserInfo" async="true" onreturn="serviceInterceptor.onReturn"/>
33
		<dubbo:method name="triggerException" onthrow="serviceInterceptor.onThrow"/>
21 34
	</dubbo:reference>
22
	<dubbo:reference id="callbackService" interface="com.ai.ipu.service.demo.ICallbackService" />
23 35
	<!-- 服务注册配置 end =========================== -->
24 36
</beans>

+ 9 - 0
ipu-service-demo/src/test/resources/dubbo-consumer-zoo.xml

@ -23,7 +23,16 @@
23 23
		<dubbo:method name="findFoo" async="true" timeout="3500"/>
24 24
		<dubbo:method name="findBar" async="true" timeout="5500"/>
25 25
	</dubbo:reference>
26
	<!-- 服务回调 -->
26 27
	<dubbo:reference id="callbackService" interface="com.ai.ipu.service.demo.ICallbackService" />
28
	
29
	<!-- 服务拦截 -->
30
	<bean id="serviceInterceptor" class="com.ai.ipu.service.demo.interceptor.ServiceInterceptor" />
31
	<dubbo:reference id="interceptorService" interface="com.ai.ipu.service.demo.IBizService">
32
		<!-- oninvoke="serviceInterceptor.onInvoke" -->
33
		<dubbo:method name="queryUserInfo" async="true" onreturn="serviceInterceptor.onReturn"/>
34
		<dubbo:method name="triggerException" onthrow="serviceInterceptor.onThrow"/>
35
	</dubbo:reference>
27 36
	<!-- 服务注册配置 end =========================== -->
28 37
</beans>
29 38