美文网首页
spring+dubbo入门手记

spring+dubbo入门手记

作者: 码在路上 | 来源:发表于2019-08-19 14:39 被阅读0次

一.dubbo简介

  1. Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring 框架无缝集成。

  2. 主要核心部件:

  • Remoting: 网络通信框架,实现了 sync-over-async 和 request-response 消息机制
  • RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能
  • Registry: 服务目录框架用于服务的注册和服务事件发布和订阅
  1. Dubbo 工作原理


    开源官方原理图
  • Provider
    暴露服务方称之为“服务提供者”
  • Consumer
    调用远程服务方称之为“服务消费者”
  • Registry
    服务注册与发现的中心目录服务称之为“服务注册中心”
  • Monitor
    统计服务的调用次调和调用时间的日志服务称之为“服务监控中心”

二. 快速启动

Dubbo 采用全 Spring 配置方式,透明化接入应用,对应用没有任何 API 侵入,只需用 Spring 加载 Dubbo 的配置即可,Dubbo 基于 Spring 的 Schema 扩展 进行加载。
如果不想使用 Spring 配置,可以通过 API 的方式 进行调用。

  1. 提供服务接口与实现(Service与ServiceImpl)
package org.apache.dubbo.demo;

public interface DemoService {
    String sayHello(String name);
}
//实现略 DemoServiceImpl 
  1. 用 Spring 配置声明暴露服务与引用服务
  • 暴露服务 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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
                        http://dubbo.apache.org/schema/dubbo    
                        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl" />
</beans>
  • 加载 Spring 配置 要启动spring容器
    Provider.java:
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
    }
}
  • 引用远程服务
    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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app"  />
 
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService" />
</beans>
  • Consumer.java (也可以使用 IoC 注入)
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;
 
public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
        String hello = demoService.sayHello("world"); // 执行远程方法
        System.out.println( hello ); // 显示调用结果
    }
}

相关文章

  • spring+dubbo入门手记

    一.dubbo简介 Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现...

  • Java随手记

    (一)JAVA的初级入门随手记 —12月22日 入门程序,输出一个Hello_World; CMD+B运...

  • python手记:入门

    python的下载地址:https:\\www.python.org,环境配置请自己百度。 (1)数据类型分为整数...

  • docker入门手记

    这不是一篇教程文章。主要是记录我个人在学习docker的过程中遇到的一些问题以及解决方法。1、安装docker注意...

  • 理财学习第四周-随手记的使用

    其实去年就已下载随手记,想理清自己的消费情况,但没有坚持。第一周听简七小白入门课之后,又重新启动了随手记,用了两...

  • Java web 项目中使用JNI技术(如何在程序运行期间改变

    记录结构:JNI技术入门详解,参照刚哥的手记:http://www.jianshu.com/p/fe42aa315...

  • JavaScript:ES6的一些习惯

    ECMAScript 6 入门这篇文章不错,下面的只是随手记。 块级作用域 能用let的地方,就不要用var,可以...

  • Spring Security入门

    前言 公众号 《java编程手记》记录JAVA学习日常,分享学习路上点点滴滴,从入门到放弃,欢迎关注 Spring...

  • 入门webpack手记(一)

    前言 入门webpack通过zhangwang同学的《入门Webpack,看这篇就够了》进行学习的。地址:www....

  • 爬虫入门学习手记

    一、简单爬虫架构 URL管理器:管理待抓取URL集合和已抓取URL集合 添加新URL到待爬取集合中 判断待添加UR...

网友评论

      本文标题:spring+dubbo入门手记

      本文链接:https://www.haomeiwen.com/subject/slmuwqtx.html