简单的Dubbo RPC调用描述如下:
复杂的Dubbo RPC调用描述如下:
在这里我们先用zookeeper来做registry,做一个简单的例子:
一、首先是zookeeper:
1、到zookeeper进行下载
2、解压
3、拷贝conf/zoo_sample.cfg到conf/zoo.cfg,然后按需要修改配置
4、双击bin/zkServer.cmd
二、然后是接口定义,服务端和接口端都会用到,最好打一个jar包,防止错误修改:
IJustATest.java
package com.neohope.dubbo.test; public interface IJustATest { public String SayHelloTo(String name); public int Add(int a, int b); }
三、再就是服务端实现:
1、新建java程序,添加mvn功能,引用dubbo-x.x.x.jar
2、服务实现类MyDubboService.java
package com.neohope.dubbo.test; public class MyDubboService implements IJustATest{ public String SayHelloTo(String name) { return "Hello " + name; } public int Add(int a, int b) { return a+b; } }
3、服务注册
ServceTest.java
package com.neohope.dubbo.test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ServceTest { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}); context.start(); System.in.read(); } }
4、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 "> <!-- 具体的实现bean --> <bean id="myDubboService" class="com.neohope.dubbo.test.MyDubboService" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="neo_service_provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.neohope.dubbo.test.IJustATest" ref="myDubboService" /> </beans>
5、编译运行
四、最后是客户端实现:
1、新建java程序,添加mvn功能,引用dubbo-x.x.x.jar
2、服务调用
ServceTest.java
package com.neohope.dubbo.test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ClientTest { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); context.start(); IJustATest proxy = (IJustATest) context.getBean("myDubboService"); System.out.println(proxy.SayHelloTo("neohope")) ; System.out.println(proxy.Add(1,2)) ; System.in.read(); } }
3、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="neo_service_consumer" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> <dubbo:reference id="myDubboService" interface="com.neohope.dubbo.test.IJustATest" /> </beans>
4、编译运行