创建一个父工程springdubbo,3个子工程分别为服务端provider,客户端consumer
,接口api
选择都选择maven quickstart即可
在main目录下面创建resources文件夹,并且让idea识别,选择中右击:
给父工程添加依赖,这样provider和consumer最为子项目都可以依赖,由于pom文件中的依赖子项目都需要,因此,吧依赖都添加到父工程的pom文件中。
<dependencies><dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.9</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.9</version><type>pom</type></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version></dependency><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.6.Final</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.21.0-GA</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- spring相关jar --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.3.3.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies>
服务端provider配置如下:
创建配置文件
applicationContext-provider.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 提供方应用信息,用于计算依赖关系 --><dubbo:application name="provider"/><!--使用zookeeper进行注册中心化--><dubbo:registry address="zookeeper://localhost:2181"/><!-- 用dubbo协议在20880端口暴露服务 --><dubbo:protocol name="dubbo" port="20880"/><!-- 和本地bean一样实现服务 --><bean id="serviceAPI" class="com.gblfy.dubbo.api.impl.ServiceAPIImpl"/><!-- 声明需要暴露的服务接口 --><dubbo:serviceinterface="com.gblfy.dubbo.api.ServiceAPI"ref="serviceAPI"/>
</beans>
创建一个接口实现类
public class ServiceAPIImpl implements ServiceAPI {@Overridepublic String sendMessage(String message) {return "quickstart-provider-message="+message;}
}
创建一个客户端
public class ProvierClient {/*** 1.读取配置文件* 2.创建ioc容器* 3.堵塞(状态停留,输入停止,不输入就状态一直保留)** @param args*/public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-provider.xml");context.start();try {System.in.read(); // 按任意键退出} catch (IOException e) {e.printStackTrace();}}
}
依赖
<dependency><groupId>com.gblfy.dubbo</groupId><artifactId>api</artifactId><version>1.0-SNAPSHOT</version></dependency>
客户端配置:
applicationContext-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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --><dubbo:application name="consumer-of-helloworld-app" /><!-- 用zookeeper进行注册中心化 --><dubbo:registry address="zookeeper://localhost:2181"/><!-- 生成远程服务代理,可以和本地bean一样使用demoService --><dubbo:referenceid="consumerService"interface="com.gblfy.dubbo.api.ServiceAPI"/></beans>
客户端
public class ConsumerClient {public static void main(String[] args) {ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext-consumer.xml");context.start();while (true){Scanner scanner = new Scanner(System.in);String message = scanner.next();//获取接口//从spring容器中,把实现类读出来 bean idServiceAPI serviceAPI = (ServiceAPI) context.getBean("consumerService");//message 是上面的//客户端发送消息--->>>服务端接收消息--->>>处理后,将结果返回--->>>客户端System.out.println(serviceAPI.sendMessage(message));}}
}
依赖
<dependency><groupId>com.gblfy.dubbo</groupId><artifactId>api</artifactId><version>1.0-SNAPSHOT</version></dependency>
启动zookeeper、服务端,客户端即可测试