使用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" />