dubbo 部署

今天参考dubbo官方文档,部署了一个bubbo服务,下面介绍一下官方文档部署的步骤。

创建maven工程

在IDEA中创建Maven工程,并添加如下依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.7</version>
</dependency>

这时候maven会自动将dubbo依赖的工程拉下来。主要是log4j,spring的一些包。

等代码拉完后就可以自己写代码了。

接口定义

首先定义服务接口,这个接口既要放到客户端,又要放到服务端。客户端作为RPC的调用方,需要知道服务方提供什么服务,服务端要实现接口来提供服务。接口如下:

package com.alibaba.dubbo.demo;

public interface DemoService {
    String sayHello(String name);
}

服务端实现

服务端需要实现定义的接口来提供服务。

package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

上面实现了接口中的sayHello()方法,来提供服务。

编写好了服务端的业务代码后,如何让客户端知道服务端提供的服务呢?这就需要配置文件,在一个广播地址上注册一个服务(高级一点的就是在zookeeper上注册服务),注册服务的配置如下:

<?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="demo-provider"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
</beans>
  • application name 为服务提供者的名称。
  • registry address 为服务广播的地址和端口号,可以看做在IP为224.5.6.7主机的1234端口上注册了一个服务(其实这个主机不存在,因为IP是广播地址)。
  • protocol 指的是协议类型,port为实际RPC调用的端口号,注意区分注册服务的端口号。我的理解是,client在进行RPC调用时,先去注册服务的端口1234上寻找服务,当找到后会和provider进行通信,这时候使用的是协议中的端口号。
  • service interface 指的是提供服务的接口。其中的ref值得是其真正的服务提供者,即下面定义的bean
  • bean 服务的真正提供者。

配置完以上信息,服务端的代码就算写好了。下面启动服务(本质上是注册服务)。

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.in.read(); // press any key to exit
    }
}

执行上面的代码,服务提供者(服务端)就已经准备好接受客户端的请求了。

客户端配置

客户端首先要有上面定义的接口DemoService,这个在前面已经说过了。接着我们就需要在客户端服务提供者的服务了。先进行客户端的配置:

<?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="demo-consumer"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
  • application name 服务调用者名称。
  • registry address 注册服务的地址,这里也叫注册服务的地址。大家看了感觉肯定很奇怪。我的理解是每个服务消费者consumer或者服务提供者provider都不只是单一的功能。换句话说,一个application既可以同时时consumer和provider。
  • reference 指的是consumer要调用服务的接口。

下面开始启动客户端,并调用服务端的代码。

import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation
        String hello = demoService.sayHello("world"); // execute remote invocation
        System.out.println(hello); // show the result
    }
}

以上就完成了dubbo的简单部署。也可以完成RPC的调用了。

学习dubbo的初衷

最近了解dubbo是因为其封装了RPC中异常处理。我们之前使用jetty作为服务器。但是jetty中使用的是比较底层的http链接(有可能有封装的比较好的RPC调用,忘大神留言回复)对异常没有较好的封装,自己封装很繁琐,所以使用了dubbo,这样作为应用开发,很好的封装了底层的http,所以采用这个比较合适。

遇到的问题

  • 服务端和客户端用的jar包(Hessian)版本不同会导致序列化或反序列化失败。
  • 如果RPC调用时链接超时,可以在客户端中的reference上添加属性:timeout="5000",单位为毫秒。

参考资料:

http://dubbo.io/

I am chris, and what about you?
原文地址:https://www.cnblogs.com/arax/p/7841464.html