Dubbo使用是依赖于Zookeeper的,如果要使用,必须先安装Zookeeper.
Dubbo配置
生产者
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="credit-shop" />
<!-- 配置注册中心 -->
<dubbo:registry protocol="zookeeper" address="192.168.21.207:2181,192.168.21.207:2182" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 绑定IP地址,防止注册到zookeeper的ip不对
<dubbo:protocol name="dubbo" host="192.168.23.180" port="20883" /> -->
<dubbo:consumer retries="1" timeout="5000" check="false"/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.dubbo.DubboTest" ref="test" timeout="5000" />
<bean id="test" class="com.dubbo.Impl.DubboTestImpl" /> <!-- 接口实现 -->
</beans>
消费者
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-autowire="byName">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-credit-shop" />
<!-- 配置注册中心 zk地址-->
<dubbo:registry protocol="zookeeper" address="192.168.21.207:2181,192.168.21.207:2182" />
<dubbo:consumer retries="1" timeout="50000" check="false"/>
<!-- 服务方ip地址
<dubbo:protocol host="192.168.23.180" /> -->
<dubbo:reference id="dubboTest" interface="com.dubbo.DubboTest" />
</beans>
调用方法
消费者在自己工程声明接口com.dubbo.DubboTest,类型和生产者工程一样,然后spring配置如上,在代码中
@Autowired
private DubboTest dubboTest;
然后便可使用dubboTest接口了
默认配置方法
dubbo.properties文件放到classpath下,默认注册
dubbo.container=log4j,spring
dubbo.application.name=application
dubbo.application.owner=xxxx
dubbo.registry.address=zookeeper://172.22.1.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20897