三个module:
service:定义接口
provider:定义接口实现
consumer:模拟接口调用
1:service
只是象征性的定义了一个接口:
- package com.tch.test.dubbo.service;
- public interface DemoService {
- String sayHello(String name);
- }
2:provider:
pom.xml:
- <dependency>
- <groupId>com.tch.test.dubbo</groupId>
- <artifactId>service</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.8.4</version>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.7</version>
- </dependency>
还有一个接口的简单实现:
- package com.tch.test.dubbo.provider.serviceimpl;
- import com.tch.test.dubbo.service.DemoService;
- public class DemoServiceImpl implements DemoService {
- public String sayHello(String name) {
- return "Hello " + name;
- }
- }
然后是spring配置文件applicationContext.xml,里面保护了dubbo的配置,我这里使用到了本地localhost的zookeeper,端口是默认的2181,如果不用zookeeper的话,直接将
- <dubbo:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry>
这句话注释掉,将
- <dubbo:registry address="multicast://224.5.6.7:1234" />
放开即可。至于为什么建议zookeeper部署奇数台,是因为根据zookeeper的leader选举算法,由于zookeeper挂掉一半的机器集群就不可用,所以部署4台和3台的集群都是在挂掉2台后集群不可用,所以使用奇数台机器,不浪费资源
:
- <?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="hello-world-app" />
- <!-- 使用multicast广播注册中心暴露服务地址 -->
- <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
- <dubbo:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry>
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.tch.test.dubbo.service.DemoService"
- ref="demoService" />
- <!-- 和本地bean一样实现服务 -->
- <bean id="demoService" class="com.tch.test.dubbo.provider.serviceimpl.DemoServiceImpl" />
- </beans>
然后是provider的测试类:
- package com.tch.test.dubbo.provider.test;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Provider {
- public static void main(String[] args) throws Exception {
- ClassPathXmlApplicationContext context = null;
- try {
- context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
- context.start();
- context.registerShutdownHook();
- System.in.read();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- context.close();
- }
- }
- }
3:consumer:
pom.xml和provider一样:
- <dependency>
- <groupId>com.tch.test.dubbo</groupId>
- <artifactId>service</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.8.4</version>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.7</version>
- </dependency>
然后是spring配置文件applicationContext.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-of-helloworld-app" />
- <!-- 使用multicast广播注册中心暴露发现服务地址 -->
- <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
- <dubbo:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry>
- <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
- <dubbo:reference id="demoService"
- interface="com.tch.test.dubbo.service.DemoService" />
- </beans>
最后是consumer的测试类:
- package com.tch.test.dubbo.consumer.test;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.tch.test.dubbo.service.DemoService;
- public class Consumer {
- public static void main(String[] args) throws Exception {
- ClassPathXmlApplicationContext context = null;
- try {
- context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
- context.start();
- context.registerShutdownHook();
- DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
- String hello = demoService.sayHello("world"); // 执行远程方法
- System.out.println(hello); // 显示调用结果
- context.stop();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(context != null){
- context.close();
- }
- }
- }
- }
接下来,先运行Provider.java,在运行Consumer.java即可看到调用结果(当前前提是zookeeper是启动的啦):
Hello world
2、下载Zookeeper,打开Zookeeper目录下的bin目录,WINDOWS系统下面,点击zkServer.cmd,启动Zookeeper;
linux系统下面,点击bin目录下的zkServer.sh,启动Zookeeper。