[首页]
[文章]
[教程]
首页
Web开发
Windows开发
编程语言
数据库技术
移动平台
系统服务
微信
设计
布布扣
其他
数据分析
首页
>
其他
> 详细
dubbo client server demo
时间:
2015-10-29 13:38:26
阅读:
309
评论:
0
收藏:
0
[点我收藏+]
个客户端。
服务端
服务端maven父工程
首先搭建一个maven父工程,引入dubbo和spring的依赖,dubbo可以和spring无缝集成。
[html]
view plain
copy
<
properties
>
<
project.build.sourceEncoding
>
UTF-8
</
project.build.sourceEncoding
>
<
v.dubbo.ext
>
1.3.0-SNAPSHOT
</
v.dubbo.ext
>
<
v.spring
>
3.1.2.RELEASE
</
v.spring
>
<
v.plugin.jar
>
2.3.1
</
v.plugin.jar
>
</
properties
>
<
dependencies
>
<
dependency
>
<
groupId
>
com.jd.dubbo.ext
</
groupId
>
<
artifactId
>
dubbo-ext-spi
</
artifactId
>
<
version
>
${v.dubbo.ext}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-core
</
artifactId
>
<
version
>
${v.spring}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-beans
</
artifactId
>
<
version
>
${v.spring}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-context
</
artifactId
>
<
version
>
${v.spring}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-context-support
</
artifactId
>
<
version
>
${v.spring}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-tx
</
artifactId
>
<
version
>
${v.spring}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-orm
</
artifactId
>
<
version
>
${v.spring}
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-web
</
artifactId
>
<
version
>
${v.spring}
</
version
>
</
dependency
>
</
dependencies
>
这些都是开发dubbo服务端必须要用的依赖包。
服务端接口子模块
这个模块负责向第三方应用暴露接口的定义,实现在其它的包里。third party 应用需要引入这个包,但无法看到接口的实现。
这个模块十分简单,只需提供接口定义。
[java]
view plain
copy
package
org.dubbo.server.api;
public
interface
DemoService {
public
String sayHello(String name);
}
服务端接口实现子模块
重新创建一个maven module,这个模块负责实现上面模块定义的接口。
实现如下:
[java]
view plain
copy
package
org.dubbo.server.service;
import
org.dubbo.server.api.DemoService;
import
org.springframework.stereotype.Service;
@Service
(
"demoService"
)
public
class
DemoServiceImpl
implements
DemoService{
public
String sayHello(String name) {
return
"Hello "
+ name;
}
}
同时服务端应该作为一个独立的应用部署起来,处理客户端的请求。
[java]
view plain
copy
package
org.dubbo.server.service;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
DubboServer {
public
static
void
main(String[] args)
throws
Exception {
ClassPathXmlApplicationContext context =
new
ClassPathXmlApplicationContext(
new
String[] {
"applicationContext.xml"
});
context.start();
System.in.read();
// 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
好,服务端跑起来了。
服务端dubbo配置
[html]
view plain
copy
<?
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://code.alibabatech.com/schema/dubbo"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire
=
"byName"
>
<
context:component-scan
base-package
=
"org.dubbo.server.service.**"
/>
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<
dubbo:application
name
=
"hehe_consumer"
organization
=
"risk.im.jd.com"
owner
=
"lvsheng"
/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<
dubbo:registry
id
=
"registry"
address
=
"10.28.163.15:6060"
/>
<
dubbo:protocol
id
=
"protocol"
name
=
"dubbo"
port
=
"25000"
heartbeat
=
"0"
threadpool
=
"cached"
threads
=
"512"
/>
<
dubbo:provider
id
=
"im.riskctrl.dubbo.provider"
timeout
=
"5000"
retries
=
"5"
loadbalance
=
"roundrobin"
cluster
=
"failover"
registry
=
"registry"
>
</
dubbo:provider
>
<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<
dubbo:service
interface
=
"org.dubbo.server.api.DemoService"
ref
=
"demoService"
provider
=
"im.riskctrl.dubbo.provider"
>
</
dubbo:service
>
</
beans
>
注册中心
dubbo注册中心我使用的是dubbo官网的注册中心demo。
客户端
客户端简单的搭建一个普通的maven工程就行了。pom依赖跟服务端的一致。
客户端的调用代码如下:
[java]
view plain
copy
package
com.jd.lvsheng.dubbo.client.test;
import
org.dubbo.server.api.DemoService;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
org.springframework.stereotype.Service;
@Service
(
"dubboConsumer"
)
public
class
DubboConsumer {
public
static
void
main(String[] args)
throws
Exception {
ClassPathXmlApplicationContext context =
new
ClassPathXmlApplicationContext(
new
String[] {
"applicationContext.xml"
});
context.start();
DemoService demoService = context.getBean(
"demoService"
, DemoService.
class
);
System.out.println(demoService.sayHello(
"aaa"
));
System.in.read();
}
}
客户端的spring配置如下:
[html]
view plain
copy
<?
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://code.alibabatech.com/schema/dubbo"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire
=
"byName"
>
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<
dubbo:application
name
=
"dubbo_consumer"
organization
=
"risk.im.com"
owner
=
"lvsheng"
/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<
dubbo:registry
id
=
"registry"
address
=
"10.28.163.15:6060"
/>
<
dubbo:protocol
id
=
"protocol"
name
=
"dubbo"
port
=
"25001"
heartbeat
=
"0"
threadpool
=
"cached"
threads
=
"512"
/>
<
dubbo:provider
id
=
"im.riskctrl.dubbo.provider"
timeout
=
"5000"
retries
=
"5"
loadbalance
=
"roundrobin"
cluster
=
"failover"
registry
=
"registry"
>
</
dubbo:provider
>
<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<
dubbo:reference
interface
=
"org.dubbo.server.api.DemoService"
id
=
"demoService"
registry
=
"registry"
>
</
dubbo:reference
>
</
beans
>
整个工程是可以运行的。
dubbo client server demo
原文:http://my.oschina.net/u/2357525/blog/523456
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年09月23日 (328)
2021年09月24日 (313)
2021年09月17日 (191)
2021年09月15日 (369)
2021年09月16日 (411)
2021年09月13日 (439)
2021年09月11日 (398)
2021年09月12日 (393)
2021年09月10日 (160)
2021年09月08日 (222)
最新文章
更多>
2021/09/28 scripts
2022-05-27
vue自定义全局指令v-emoji限制input输入表情和特殊字符
2022-05-27
9.26学习总结
2022-05-27
vim操作
2022-05-27
深入理解计算机基础 第三章
2022-05-27
C++ string 作为形参与引用传递(转)
2022-05-27
python 加解密
2022-05-27
JavaScript-对象数组里根据id获取name,对象可能有children属性
2022-05-27
SQL语句——保持现有内容在后面增加内容
2022-05-27
virsh命令文档
2022-05-27
教程昨日排行
更多>
1.
list.reverse()
2.
Django Admin 管理工具
3.
AppML 案例模型
4.
HTML 标签列表(功能排序)
5.
HTML 颜色名
6.
HTML 语言代码
7.
jQuery 事件
8.
jEasyUI 创建分割按钮
9.
jEasyUI 创建复杂布局
10.
jEasyUI 创建简单窗口
友情链接
汇智网
PHP教程
插件网
关于我们
-
联系我们
-
留言反馈
- 联系我们:wmxa8@hotmail.com
© 2014
bubuko.com
版权所有
打开技术之扣,分享程序人生!