dubbo http webservice 服务学习笔记【原创】

学习了 dubbo rest服务之后,再使用dubbo http协议,就非常简单了,只要将服务的暴露协议改为http即可,不需要根据jax-rs规范,引入@Path等一堆annotation。
1. dubbo 配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubborest-webapp" owner="zhenggm" organization="zjport"/>

    <dubbo:registry address="zookeeper://192.168.3.140:2181"/>

    <!--uncomment this if you want to test dubbo's monitor-->
    <!--<dubbo:monitor protocol="registry"/>-->

    <!-- here we demonstrate both annotation-based and xml-based configs -->
    <dubbo:annotation package="cn.gov.zjport.dubborest.service" />

    <!-- use tomcat server -->
    <dubbo:protocol name="rest" port="7056" contextpath="dubborest-webapp" server="servlet"
                    extension="com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"/>

	<dubbo:service interface="cn.gov.zjport.dubborest.service.rest.RestDemoService" ref="restDemoService"  protocol="rest"  timeout="2000" connections="100" validation="true"/>
	
	<dubbo:service interface="cn.gov.zjport.dubborest.service.http.HttpDemoService" ref="httpDemoService"  protocol="http"  timeout="2000" connections="100" validation="true"/>
</beans>

这个文件是在前面练习的dubbo rest服务基础上增加的,只要增加
<dubbo:service interface="cn.gov.zjport.dubborest.service.http.HttpDemoService" ref="httpDemoService"  protocol="http"  timeout="2000" connections="100" validation="true"/>
即完成服务的发布。
注意:如果以下这一行
<dubbo:protocol name="rest" port="7056" contextpath="dubborest-webapp" server="servlet"
                    extension="com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"/>
如果将name改为http, 那么会提示nullpoint的错误,是配置文件中另外一个restDemoService采用rest方式调用的原因造成。

2.接口类
package cn.gov.zjport.dubborest.service.http;

import cn.gov.zjport.dubborest.pojo.User;

public interface HttpDemoService {

	public User execute(User user);
}

3.实现类
package cn.gov.zjport.dubborest.service.http.impl;

import org.springframework.stereotype.Service;

import cn.gov.zjport.dubborest.pojo.User;
import cn.gov.zjport.dubborest.service.http.HttpDemoService;

@Service("httpDemoService")
public class HttpDemoServiceImpl implements HttpDemoService{
	
	public User execute(User user){
		user.setName(user.getName()+",hello");
		return user;
	}
}

4.客户端dubbo配置
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubborest-webapp" owner="zhenggm" organization="zjport"/>

    <dubbo:registry address="zookeeper://192.168.3.140:2181"/>

    <!--uncomment this if you want to test dubbo's monitor-->
    <!--<dubbo:monitor protocol="registry"/>-->

    <!-- here we demonstrate both annotation-based and xml-based configs -->
    <dubbo:annotation package="cn.gov.zjport.dubborest.service" />

    <!-- use tomcat server -->
    <dubbo:protocol name="rest" port="7056" contextpath="dubborest-webapp" server="servlet"
                    extension="com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"/>

	<dubbo:service interface="cn.gov.zjport.dubborest.service.rest.RestDemoService" ref="restDemoService"  protocol="rest"  timeout="2000" connections="100" validation="true"/>
	
	<dubbo:service interface="cn.gov.zjport.dubborest.service.http.HttpDemoService" ref="httpDemoService"  protocol="http"  timeout="2000" connections="100" validation="true"/>
</beans>
也是基于dubbo rest客户端配置修改的,增加了最后一行服务调用
5. 调用代码
System.out.println(httpDemoService.execute(user).getName());


6. 如果采用webservice协议暴露,只要将
<dubbo:service interface="cn.gov.zjport.dubborest.service.http.HttpDemoService" ref="httpDemoService"  protocol="http"  timeout="2000" connections="100" validation="true"/>
中的protocol="http"改为protocol="webservice"即可,其他调用代码如http保持不变。

猜你喜欢

转载自zhenggm.iteye.com/blog/2323819
今日推荐