RPC服务框架dubbo(四):Dubbo中Provider搭建
1.新建Maven Project, 里面只有接口(dubbo-service)
1.1 为什么这么做?
RPC框架,不希望Consumer知道具体实现.如果实现类和接口在同一个项目中,Consumer依赖这个项目时,就会知道实现类具体实现.
2.新建Maven Project, 写接口的实现类(dubbo-service-impl)
3.在duboo-service-impl中配置pom.xml
3.1 依赖接口
3.2 依赖dubbo,去掉老版本spring
3.3 依赖新版本spring
3.4 依赖zookeeper客户端工具zkClient
<dependencies> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- 访问zookeeper的客户端jar --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies> |
4.新建实现类,并实现接口方法.
5.新建配置文件applicationContext-dubbo.xml,并配置
5.1 <dubbo:application/> 给provider起名,在monitor或管理工具中区别是哪个provider
5.2 <dubbo:registry/> 配置注册中心
5.2.1 address:注册中心的ip和端口
5.2.2 protocol使用哪种注册中心
5.3 <dubbo:protocol/> 配置协议
5.3.1 name 使用什么协议
5.3.2 port: consumer invoke provider时的端口号
5.4 <dubbo:service/> 注册接口
5.4.1 ref 引用接口实现类<bean>的id值
<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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 给当前Provider自定义个名字 --> <dubbo:application name="dubbo-service"/> <!-- 配置注册中心 --> <dubbo:registry address="192.168.139.130:2181" protocol="zookeeper"></dubbo:registry> <!-- 配置端口 --> <dubbo:protocol name="dubbo" port="20888"></dubbo:protocol> <!-- 注册功能 --> <dubbo:service interface="com.bjsxt.service.DemoService" ref="demoServiceImpl"></dubbo:service> <bean id="demoServiceImpl" class="com.bjsxt.service.impl.DemoServiceImpl"></bean> </beans> |
6.启动容器
6.1 通过spring方式启动
6.1.1 applicationContext-dubbo.xml位置没有要求
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml"); ac.start(); System.out.println("启动成功"); System.in.read(); |
6.2 使用dubbo提供的方式启动(推荐使用这种方式)
6.2.1 要求applicationContext-dubbo.xml必须放入类路径下/META-INF/spring/*.xml
Main.main(args); |