需要注意的是,如果你只导入 dubbo 的包的时候是会报错的,找不到 netty 和 curator 的依赖, Curator 是 Netflix 开源的一个 Zookeeper 客户端实现。
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.6</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.5</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.32.Final</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.8.0</version> </dependency> 复制代码
dubbo都有多种连接方式,推荐使用xml
provider.xml示例
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 在这个分布式系统中的唯一名称 --> <dubbo:application name="demo-provider"/> <!-- 监控中心配置 --> <dubbo:monitor protocol="registry"/> <!-- 单机配置 --> <!-- <dubbo:registry address="zookeeper://192.168.31.178:2181"/>--> <!-- 集群配置 --> <dubbo:registry address="zookeeper://192.168.31.178:2181?backup=192.168.31.178:2182,192.168.31.178:2183"/> <!-- 当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http--> <dubbo:protocol name="dubbo" port="20890"/> <!-- 配置spring接口 --> <bean id="helloWorld" class="com.example.demo.service.HelloWorldService"/> <!-- 暴露要发布的服务 --> <dubbo:service interface="com.example.demo.service.HelloWorldService" ref="helloWorld"/> </beans> 复制代码