[Dubbo开发]配置简单的生产者和消费者
Posted 圣所SANCTUARY
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Dubbo开发]配置简单的生产者和消费者相关的知识,希望对你有一定的参考价值。
配置好jdk1.7、Zookeeper和Maven环境之后,开始尝试第一次搭建简单的dubbo生产者和消费者。
dubbo服务的原理,引用经典的官方图(cr.Dubbo官网):
关于Dubbo的原理和机制,在此不做赘述,具体可以查询官方文档:http://dubbo.apache.org/#!/?lang=zh-cn。
接下来开始搭建生产者和消费者。
1.生产者(Provider)
创建一个maven项目,
代码如下:
(1)接口ProviderService.java
package com.mohrss.service; public interface ProviderService { public void sayHello(); }
(2)实现类ProviderServiceImpl.java
package com.mohrss.service.impl; import com.mohrss.service.ProviderService; import org.springframework.stereotype.Service; @Service("providerService") public class ProviderServiceImpl implements ProviderService { public void sayHello() { // TODO Auto-generated method stub System.out.println("Hello I am Provider"); } }
(3)程序入口TestProviderService.java
package com.mohrss.service; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestProviderService { public static void main(String[] args) { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"}); context.start(); System.out.println("生产者服务已经注册成功!"); try { System.in.read();//让此程序一直跑,表示一直提供服务 } catch (Exception e) { e.printStackTrace(); } } }
(4)dubbo配置文件dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!--用注解方式实现bean--> <context:component-scan base-package="com.mohrss.service"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="29014" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.mohrss.service.ProviderService" ref="providerService" /> </beans>
(5)依赖文件配置pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mohrss</groupId> <artifactId>provider</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>4.3.2.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> </project>
2.消费者(Consumer)
消费者搭建和生产者相同的maven项目
(1)TestConsumerService.java
package com.mohrss.consumer; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mohrss.service.ProviderService; public class TestConsumerService { public static void main(String[] args) { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer.xml"}); context.start(); ProviderService testService = (ProviderService) context.getBean("testProviderService"); testService.sayHello(); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
(2)dubbo-consumer.xml
<?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" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="testProviderService" interface="com.mohrss.service.ProviderService" /> </beans>
3.启动zookeeper,右键启动TestProviderService.java,将看到生产者成功注册:
然后右键TestConsumerService.java, Run as Application,将看到:
注意:消费者调用服务方接口,应当将provider编译成jar包放到消费者中进行引用,此处为了方便,直接引用了生产者的接口文件ProviderService.java
注:原创博客,转载请注明。
以上是关于[Dubbo开发]配置简单的生产者和消费者的主要内容,如果未能解决你的问题,请参考以下文章