这里demo仅仅是为了了解dubbo的用法啦啦啦~~~
不知道咋说这个东西,无侵入式的代码,简直就是棒棒的,代码也敲的爽爽的
好了,话不多说了,上代码盛宴
前期准备,zookeeper这个我前面有介绍啦
没看的再看一下:http://blog.csdn.net/qq_14926283/article/details/79213271
Dubbo管理平台的搭建,这个最好要要搭一下啦
方便看效果噻:http://blog.csdn.net/qq_14926283/article/details/79213924
好放大招:
<?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" 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 "> <!-- 具体的实现bean --> <bean id="demoService" class="com.test.impl.DemoServiceImpl" /> <bean id="yhService" class="com.test.impl.UserServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="anyname_provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20881" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.test.Provider" ref="demoService" /> <dubbo:service interface="com.test.UserService" ref="yhService" /> </beans> |
直接就拿出来看看,原理就是直接写service到zookeeper注册,这里接口就暴露啦,这里是提供者端的,然后我们来看看消费者端
<?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" 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 "> <dubbo:application name="consumer_app" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:consumer timeout="5000" /> <dubbo:reference id="demoService" interface="com.test.Provider" /> <dubbo:reference id="yhService" interface="com.test.UserService" /> </beans> |
这里是(reference)指向zookeeper的暴露的service接口,把我们本地的 interface="com.test.Provider"指过去,完美串接起来了吧
具体demo请看这里吧,下载链接:http://download.csdn.net/download/qq_14926283/10257524