[dubbo入门] dubbo入门
来源:互联网 发布:教务系统数据库表 编辑:程序博客网 时间:2024/05/17 04:13
使用dubbo实现一个远程的RPC调用的过程:
1.定义一个服务接口
服务接口ISayHello
public interface ISayHello { ObjectResponse sayHello(ObjectRequest objectRequest);}
接口参数需要序列化
public class ObjectRequest implements Serializable { private static final long serialVersionUID = -8141279374881183198L; private String name; public ObjectRequest(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "ObjectRequest{" + "name='" + name + '\'' + '}'; }}
返回对象也需要序列化
public class ObjectResponse implements Serializable { private static final long serialVersionUID = -6961033674286036464L; private String name; public ObjectResponse(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "ObjectResponse{" + "name='" + name + '\'' + '}'; }}
2.实现一个Provider
Provider是一个服务提供者,
第一步:需要实现服务接口
public class SayHelloImpl implements ISayHello { public ObjectResponse sayHello(ObjectRequest objectRequest) { String name = objectRequest.getName(); return new ObjectResponse(name); }}
第二步:配置dubbo,在资源文件中创建META-INF/spring/xxx.xml文件
<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-provider" owner="luis"/> //这个地方可以使用很多种方式,如注册中心等 <dubbo:registry address="N/A" /> //这地方可以使用dubbo、webservice等协议 <dubbo:protocol name="http" port="20881" /> //接口以及接口的实现类 <dubbo:service interface="dubbodemo.ISayHello" ref="sayHello"/> //接口实现类bean <bean id="sayHello" class="dubbodemo.SayHelloImpl" /></beans>
第三步:发布服务
public class App { public static void main( String[] args ) throws IOException { ClassPathXmlApplicationContext cac= new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbodemoprovider.xml"}); classPathXmlApplicationContext.start(); System.in.read(); }}
3.创建一个消费者Consumer
Consume想当然肯定是获取的Provider暴露出来的接口,根据上面Provider配置的方式,Consumer的配置方式如下所示:
<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-cusumer" owner="luis"/> <dubbo:registry address="N/A" /> //url必须配置,url为 协议(此处配置为http)://ip地址+端口号/接口的全路径 <dubbo:reference id="sayHello" interface="dubbodemo.ISayHello" url="http://192.168.36.1:20881/dubbodemo.ISayHello" /></beans>
通过下面的方式来调用Provider提供的服务接口
public class App { public static void main( String[] args ) { ObjectRequest objectRequest = new ObjectRequest("luis"); ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbodemocusumer.xml"}); cac.start();
一个简单的dubbo实现远程RPC的调用就已经完成
对比Provider和Consumer的配置文件
都需要配置的
dubbo:application name="dubbo-xxx" owner="luis"/>//注册中心的配置<dubbo:registry address="N/A" />
Provider需要配置的,发布服务之后都是已URL方式提供url包括 协议+ip+端口+接口的全称
以及Provider需要实现服务接口
<dubbo:protocol name="http" port="20881" /><dubbo:service interface="dubbodemo.ISayHello" ref="sayHello"/>
Consumer需要配置的,consumer主要关注需要引用服务接口,以及从哪里(url)引用的
<dubbo:reference id="sayHello" interface="dubbodemo.ISayHello" url="http://192.168.36.1:20881/dubbodemo.ISayHello" />
阅读全文
0 0
- [dubbo入门] dubbo入门
- Dubbo入门
- dubbo入门
- dubbo入门
- Dubbo入门
- Dubbo入门
- dubbo入门
- Dubbo入门
- dubbo入门
- Dubbo入门
- dubbo入门
- Dubbo入门
- dubbo入门
- Dubbo入门
- Dubbo入门
- dubbo 入门
- Dubbo入门
- Dubbo入门
- 【C语言】【unix c】内存管理
- 【shiro】shiro和sping整合
- Linux中源码编译lua动态库
- 数据结构-算法和数据操作
- 原始数据类型
- [dubbo入门] dubbo入门
- Largest_Divisible_Subset
- 【C语言】【unix c】使用mmap将物理地址映射到进程的虚拟地址空间
- windwos下怎样配置redis服务器
- 假定CSomething是一个类,执行下面这些语句之后,内存里创建了____个CSomething对象。
- VMware vSphere Web Services SDK编程指南(八)- 8.1 认证和授权(相关对象)
- PAT_A 1041. Be Unique (20)
- 防止表单重复提交的三种方式
- C++面向对象模型初探