百度百科:Dubbo(开源分布式服务框架)_百度百科
首先我们使用dubbo一般是用来做分布式,也就是说服务端和客户端不在一个服务器。
Spring集成
新增dubbo.properties
##
#
# dubbo config
#
##
# 是否启用dubbo
dubbo.enable=true
dubbo.applicationName=XXXX
# dubbo的注册中心可以是zookeeper redis
dubbo.registryProtocol=zookeeper
# 例如xx.xx.xx.xx:2181,xx.xx.xx.xx::2181,xx.xx.xx.xx::2181
dubbo.registryAddress=xx.xx.xx.xx::2181,xx.xx.xx.xx::2181,xx.xx.xx.xx::2181
# zookeeper根节点,如果多套环境用一套zookeeper,这个地方一定要区分
dubbo.registryGroup=dubbo-pro
# dubbo的注册中心的用户和密码,如:zookeeper的用户 密码 redis的用户密码
dubbo.registryUsername=
dubbo.registryPassword=
# dubbo的监听端口
dubbo.protocolPort=20880
# 最大线程数
dubbo.protocolThreads=300
#dubbo2.7.x新增配置
#dubbo 配置中心
dubbo.configCenterAddress=ip1:2181,ip2:2181,ip3:2181
dubbo.configCenterProtocol=zookeeper
#dubbo 元数据中心
dubbo.metaReportAddress=zookeeper://zookeeper-0.zookeeper.gtms-preprod.svc.cluster.local:2181,zookeeper-1.zookeeper.gtms-preprod.svc.cluster.local:2181,zookeeper-2.zookeeper.gtms-preprod.svc.cluster.local:2181
#dubbo 服务统计配置 是否启用
dubbo.enableMetrics=true
#dubbo 生产者服务统计配置 端口和协议,端口默认和dubbo的监听端口一致
dubbo.preMetricsPort=20880
dubbo.preMetricsProtocol=dubbo
开发使用
通过<dubbo.../> 配置统一地址
配置服务端<
dubbo:serviceinterface
=
"com.alibaba.hello.api.HelloService"
ref
=
"helloService"
/>
相应客户端
<
dubbo:serviceinterface
=
"com.alibaba.hello.api.HelloService"
ref
=
"helloService"
/>
再结合spring框架,服务端配置<beanid> 客户端通过contenxt上下文获取到
服务端
定义一个Service Interface:(HelloService.java)
1 2 3 4 5 6 7 8 | package com.alibaba.hello.api; public interface HelloService { String sayHello(String name); } |
接口的实现类:(HelloServiceImpl.java)
1 2 3 4 5 6 7 | package com.alibaba.hello.impl; import com.alibaba.hello.api.HelloService; public class HelloServiceImpl implements HelloService{ public String sayHello(String name){ return "Hello" + name; } } |
Spring配置:(provider.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 | <? xmlversion = "1.0" encoding = "UTF-8" ?> < beans...... > <!--Applicationname--> < dubbo:applicationname = "hello-world-app" /> <!--registryaddress,usedforservicetoregisteritself--> < dubbo:registryaddress = "multicast://224.5.6.7:1234" /> <!--exposethisservicethroughdubboprotocol,throughport20880--> < dubbo:protocolname = "dubbo" port = "20880" /> <!--whichserviceinterfacedoweexpose?--> < dubbo:serviceinterface = "com.alibaba.hello.api.HelloService" ref = "helloService" /> <!--designateimplementation--> < beanid = "helloService" class = "com.alibaba.hello.impl.HelloServiceImpl" /> </ beans > |
测试代码:(Provider.java)
1 2 3 4 5 6 7 | importorg.springframework.context.support.ClassPathXmlApplicationContext; public class Provider{ public static void main(String[] args){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(newString[]{ "provider.xml" }); //启动成功,监听端口为20880System.in.read();//按任意键退出 } } |
客户端
Spring配置文件:(consumer.xml)
1 2 3 4 5 6 7 8 9 | <? xmlversion = "1.0" encoding = "UTF-8" ?> < beans xmlns=......> <!--consumerapplicationname--> < dubbo:applicationname = "consumer-of-helloworld-app" /> <!--registryaddress,usedforconsumertodiscoverservices--> < dubbo:registryaddress = "multicast://224.5.6.7:1234" /> <!--whichservicetoconsume?--> < dubbo:referenceid = "helloService" interface = "com.alibaba.hello.api.HelloService" /> </ beans > |
客户端测试代码:(Consumer.java)
1 2 3 4 5 6 7 8 9 10 11 | import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.hello.api.HelloService; public class Consumer{ public static void main(String[] args){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(newString[]{ "consumer.xml" }); HelloService helloService = (HelloService)context.getBean( "helloService" ); //getserviceinvocationproxyStringhello=helloService.sayHello("world"); //doinvoke!System.out.println(hello); //cool,howareyou~ } } |
未完待续.