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

学习了 dubbo rest服务之后,再使用dubbo http协议,就非常简单了,只要将服务的暴露协议改为http即可,不需要根据jax-rs规范,引入@Path等一堆annotation。
[b]1. dubbo 配置文件[/b]
<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保持不变。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值