dubbo rest 服务学习笔记(二)【原创】

采用dubbo客户端访问dubbo发布的rest服务,接上一篇。
[b]1)启动类[/b]
package cn.gov.zjport.dubborest.test;

public class DemoConsumer {

public static void main(String[] args) {
com.alibaba.dubbo.container.Main.main(args);
}

}
[b]2)beans-consumer.xml[/b]

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
<!--service服务 -->
<import resource="classpath:/test/spring/consumer/spring-business-service.xml" />
<import resource="classpath:/test/spring/consumer/spring-dubbo-service.xml" />
</beans>

[b]3.spring 配置文件:spring-business-service.xml[/b]
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
<bean class="cn.gov.zjport.dubborest.test.DemoStart" init-method="start">
<property name="restDemoService" ref="restDemoService" />
</bean>
</beans>

[b]4.dubbo配置文件:spring-dubbo-service.xml[/b]
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2011 Alibaba Group.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-->
<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-test-consumer" owner="dubborest" organization="zjport"/>

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

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

<dubbo:reference id="restDemoService" interface="cn.gov.zjport.dubborest.service.rest.RestDemoService"/>
</beans>

[b]5. 客户端访问类:DemoStart [/b]
package cn.gov.zjport.dubborest.test;

import cn.gov.zjport.dubborest.pojo.User;
import cn.gov.zjport.dubborest.service.rest.RestDemoService;

public class DemoStart {

private RestDemoService restDemoService;

public void start(){
System.out.println(restDemoService.search("zhenggm"));

User user=new User();
user.setId(1L);
user.setName("zhengxl");
System.out.println(restDemoService.post(user));
System.out.println(restDemoService.get("zhangsan"));
}

public RestDemoService getRestDemoService() {
return restDemoService;
}

public void setRestDemoService(RestDemoService restDemoService) {
this.restDemoService = restDemoService;
}


}

[b]6. 运行 DemoConsumer[/b]
在run configuration 里设置 vm参数
-Ddubbo.spring.config=classpath:test/beans-consumer.xml
然后运行,就能打印出访问结果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值