第一步:先不管那麽多,加入相关jar包.
<!-- dubbo --> <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.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- dobbo webservice --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-simple</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.6.1</version> </dependency>
2.配置dubbo配置文件:提供者和消费者的这份配置文件是一样的.都是使用spring集成的方式:
<?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="${dubbo.application.name}"/> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="${dubbo.registry.address}"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}"/> <!-- 引用需要对外发布的服务接口配置 --> <import resource="dubbo-server-service.xml"/> <!--<import resource="dubbo-client-service.xml"/>--> </beans>
dubbo.application.name=provider dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=208803.提供者提供服务的配置文件:
<?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:service interface="com.ihuaqiang.spring.service.facade.UserService" ref="userService" timeout="1200000" version="1.0.0"/> <bean name="userService" class="com.ihuaqiang.spring.service.UserServiceImpl"/> </beans>4.消费者接收服务的配置文件:
<?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:reference id="userService" interface="com.ihuaqiang.spring.service.facade.UserService" version="1.0.0"/> </beans>配置完成之后使用Junit来测试下:
provider类:这里采用输入流的阻塞方式使服务一直在启动状态:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring-context.xml", "classpath:globals.datasource.xml"}) public class Provider { @Test public void test() { System.out.println("回车键结束..."); try { Log4jConfigurer.initLogging("classpath:log4j.properties"); } catch (FileNotFoundException ex) { System.err.println("Cannot Initialize log4j"); } int read = 0; try { read = System.in.read(); } catch (IOException e) { e.printStackTrace(); } System.out.println(read); } }
消费者接收的类:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring-context.xml", "classpath:globals.datasource.xml"}) public class Consumer { Logger logger = LogManager.getLogger(this.getClass()); @Autowired(required = false) private UserService userService; @Test public void test() throws Exception { logger.warn(userService.getUser().getName()); } }
控制台有打印,说明服务接收成功啦.