01Dubbo 点对点提供服务通信

Dubbo是什么

Dubbo是一个分布式的服务框架,提供高性能的以及透明的RPC远程服务调用解决方法,以及SOA服务治理方案。

Dubbo的核心部分

  • 远程通信
  • 集群容错
  • 服务的自动发现
  • 负载均衡

下面先对其远程通信进行实现。

架构图

在这里插入图片描述

流程图

在这里插入图片描述

核心类

  • IOrderServices 订单服务接口定义类
  • OrderServicesImpl 订单服务实现类
  • OrderRequest OrderResponse 订单请求、返回参数

代码实现

订单服务代码实现

暴露接口定义

/**
 * Created by zhangdd on 2019/12/28
 */
public interface IOrderServices {

    OrderResponse doOrder(OrderRequest request);
}

接口实现

/**
 * Created by zhangdd on 2019/12/28
 */
public class OrderServicesImpl implements IOrderServices {

    @Override
    public OrderResponse doOrder(OrderRequest request) {
        System.out.println("曾经来过" + request);
        OrderResponse response = new OrderResponse();
        response.setCode("1000");
        response.setMsg("处理成功");
        return response;
    }
}
/**
 * Created by zhangdd on 2019/12/28
 */
public class OrderRequest implements Serializable {
    private static final long serialVersionUID = -4200255400834793697L;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "OrderRequest{" +
                "name='" + name + '\'' +
                '}';
    }
}
/**
 * Created by zhangdd on 2019/12/28
 */
public class OrderResponse implements Serializable {
    private static final long serialVersionUID = 430459906156398864L;

    private Object data;
    private String code;
    private String msg;

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "OrderResponse{" +
                "data=" + data +
                ", code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                '}';
    }
}

上面这些就完成了,代码的业务部分,接着需要将接口暴露出去,这里使用xml配置文件的方式将定义的接口暴露出去。首先在项目的 resource 目录下 创建 META-INF.spring包 ,然后再创建 order-provider.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">

    <!--name:当前项目在整个分布式架构里面的唯一名称,owner:项目维护者-->
    <dubbo:application name="order-provider" owner="lucky"/>
    <!--registry: 配置注册中心的信息,address:是注册中心的地址,
    这里我们配置的是 N/A 表示由 dubbo 自动分配地址。或者说是一种直连的方式,不通过注册中心-->
    <dubbo:registry address="N/A"/>
    <!--protocol:服务发布的时候 dubbo 依赖什么协议,可以配置 dubbo、webserovice、Thrift、Hessain、http等协议-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="com.lucky.api.IOrderServices"
                   ref="orderServices"/>
    <bean id="orderServices"
            class="com.lucky.facade.impl.OrderServicesImpl"/>
</beans>

服务启动

public class App {

    public static void main(String[] args) {
        Main.main(args);
    }
}

到这里服务已经启动,并对外暴露了。可以看下启动日志:

十二月 29, 2019 8:18:12 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: using logger: com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter
十二月 29, 2019 8:18:12 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] Use container type([spring]) to run dubbo serivce., dubbo version: 2.5.3, current host: 127.0.0.1
十二月 29, 2019 8:18:12 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3fa77460: display name [org.springframework.context.support.ClassPathXmlApplicationContext@3fa77460]; startup date [Sun Dec 29 08:18:12 CST 2019]; root of context hierarchy
十二月 29, 2019 8:18:12 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [/Users/lucky/java/dubbo-demo/dubbo-order/order-provider/target/classes/META-INF/spring/order-provider.xml]
十二月 29, 2019 8:18:13 上午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@3fa77460]: org.springframework.beans.factory.support.DefaultListableBeanFactory@17d99928
十二月 29, 2019 8:18:13 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@17d99928: defining beans [order-provider,com.alibaba.dubbo.config.RegistryConfig,dubbo,com.lucky.api.IOrderServices,orderServices]; root of factory hierarchy
十二月 29, 2019 8:18:13 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] The service ready on spring started. service: com.lucky.api.IOrderServices, dubbo version: 2.5.3, current host: 127.0.0.1
十二月 29, 2019 8:18:13 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] Export dubbo service com.lucky.api.IOrderServices to local registry, dubbo version: 2.5.3, current host: 127.0.0.1
十二月 29, 2019 8:18:13 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] Export dubbo service com.lucky.api.IOrderServices to url dubbo://192.168.31.108:20880/com.lucky.api.IOrderServices?anyhost=true&application=order-provider&dubbo=2.5.3&interface=com.lucky.api.IOrderServices&methods=doOrder&owner=lucky&pid=1637&side=provider&timestamp=1577578693621, dubbo version: 2.5.3, current host: 127.0.0.1
十二月 29, 2019 8:18:14 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.31.108:20880, dubbo version: 2.5.3, current host: 127.0.0.1
十二月 29, 2019 8:18:14 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] Dubbo SpringContainer started!, dubbo version: 2.5.3, current host: 127.0.0.1
[2019-12-29 08:18:14] Dubbo service server started!

启动日志分析

  • 日志模块使用的适配器JclLoggerAdapter
  • dubbo的版本信息
  • AbstractApplicationContext 启动信息
  • 服务暴露配置文件信息

信息: [DUBBO] Export dubbo service com.lucky.api.IOrderServices to url dubbo://192.168.31.108:20880/com.lucky.api.IOrderServices?anyhost=true&application=order-provider&dubbo=2.5.3&interface=com.lucky.api.IOrderServices&methods=doOrder&owner=lucky&pid=1637&side=provider&timestamp=1577578693621, dubbo version: 2.5.3, current host: 127.0.0.1

  • 重要的一条:发布了dubbo服务 com.lucky.api.IOrderServices ,url地址为ubbo://192.168.31.108:20880/com.lucky.api.IOrderServices?anyhost=true&application=order-provider&dubbo=2.5.3&interface=com.lucky.api.IOrderServices&methods=doOrder&owner=lucky&pid=1637&side=provider&timestamp=1577578693621, dubbo version: 2.5.3, current host: 127.0.0.1

    • 首先,形式上也是用类似http协议的方式,只不过这里使用的是dubbo协议
    • ?之前的这部分 ubbo://192.168.31.108:20880/com.lucky.api.IOrderServices,构成格式:协议://ip:端口/接口 协议、端口、接口是我们配置文件里定义的,ip:由于当前是采用点对点的方式所以IP是自动生成的。
    • ?后面的这部分也是我们在xml文件里配置的信息

用户调用服务代码实现

由于当前场景只有用户调用订单服务,所以只需要配置用户服务的引用服务即可。在resources目录下创建order-consumer.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="order-consumer" owner="lucky"/>

    <dubbo:reference id="orderProvider"
            interface="com.lucky.api.IOrderServices"
                     url="dubbo://192.168.31.108:20880/com.lucky.api.IOrderServices"/>

</beans>
  • 配置当前服务的名称
  • 配置当前服务需要调用的服务信息,由于当前是点对点通信模式所以需要配置服务发布时产生的url信息

服务调用

public class App {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new
                ClassPathXmlApplicationContext("order-consumer.xml");
        IOrderServices orderProvider = (IOrderServices) context.getBean("orderProvider");
        OrderRequest request = new OrderRequest();
        request.setName("lucky");
        OrderResponse orderResponse = orderProvider.doOrder(request);
        System.out.println(orderResponse.toString());

        System.in.read();
    }
}

用于服务模块的pom文件需要依赖order-api模块。

日志信息

十二月 29, 2019 9:00:11 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: display name [org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71]; startup date [Sun Dec 29 09:00:11 CST 2019]; root of context hierarchy
十二月 29, 2019 9:00:11 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [order-consumer.xml]
十二月 29, 2019 9:00:12 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息: using logger: com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter
十二月 29, 2019 9:00:12 上午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71]: org.springframework.beans.factory.support.DefaultListableBeanFactory@18e8568
十二月 29, 2019 9:00:12 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18e8568: defining beans [order-consumer,orderProvider]; root of factory hierarchy
十二月 29, 2019 9:00:13 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] Successed connect to server /192.168.31.108:20880 from NettyClient 192.168.31.108 using dubbo version 2.5.3, channel is NettyChannel [channel=[id: 0x71a794e5, /192.168.31.108:53904 => /192.168.31.108:20880]], dubbo version: 2.5.3, current host: 192.168.31.108
十二月 29, 2019 9:00:13 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] Start NettyClient luckydeMacBook-Pro.local/192.168.31.108 connect to the server /192.168.31.108:20880, dubbo version: 2.5.3, current host: 192.168.31.108
十二月 29, 2019 9:00:13 上午 com.alibaba.dubbo.common.logger.jcl.JclLogger info
信息:  [DUBBO] Refer dubbo service com.lucky.api.IOrderServices from url dubbo://192.168.31.108:20880/com.lucky.api.IOrderServices?application=order-consumer&dubbo=2.5.3&interface=com.lucky.api.IOrderServices&methods=doOrder&owner=lucky&pid=1677&side=consumer&timestamp=1577581212678, dubbo version: 2.5.3, current host: 192.168.31.108
OrderResponse{data=null, code='1000', msg='处理成功'}

从日志信息里可以看出,除了基本启动信息外,最后一句打印出了order-provider返回的信息。

同时order-provider模块里也有对应的日志信息

曾经来过OrderRequest{name='lucky'}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值