Dubbo的使用

博客链接地址:https://www.cnblogs.com/wang-meng/p/5791598.html



Dubbo是什么?

Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

其核心部分包含:

远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。

集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。

自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。


Dubbo能做什么?

透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。

软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。

服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。


Spring集成

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

上面简单介绍了Dubbo的一些概念, 这里再给一张图来形象的形容下:


我们用这张图来形容 , 那么映射到 项目中:      当我们在多个Tomcat部署不同的系统时, 例如A系统(TomcatA)想调用B系统(TomcatB)中的服务, 这时Dubbo就有了用武之地. 首先我们需要B系统在注册中心将自己的Url注册进去, 然后注册中心将Url返还给系统A, 那么系统A就可以调用了. 当然了我这里说的只是Dubbo的一种用法, 在这个项目中使用的也是Dubbo的远程调用功能. (感觉真的和webservice有点像)



下面就步入正题, 看看Dubbo在项目中的使用实例:

1, 在linux下安装Zookeeper

这个地方就不详细概述Zookeeper的安装了, 前面关于Linux的博文已经有讲过在Linux下软件的安装了, 这里安装好后直接启动 Zookeeper.


zookeeper

2, 使用需求

在这里 当我们有一种需求, 我们需要在后台(console)去对商品(product)做一些操做, 而这里我们只能够使用到公共的service方法, 那么怎么调用product中service的实现呢?

项目结构:


项目结构

公共service方法:

TestTbService.java:

package cn.itcast.core.service;

import cn.itcast.core.bean.TestTb;

public interface TestTbService {

      public void insertTestTb(TestTb testTb);

}


公共service方法

product中的service实现类:

TestTbService.java:

package cn.itcast.core.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import cn.itcast.core.bean.TestTb;

import cn.itcast.core.dao.TestTbDao;

@Service("testTbService")

@Transactional

public class TestTbServiceImpl implements TestTbService {

    @Autowired

    private TestTbDao testTbDao;


    //保存

    public void insertTestTb(TestTb testTb){

        testTbDao.insertTestTb(testTb);

    }

}


product中的service实现类

在console中使用product中的service实现类:

CenterController.java:

package cn.itcast.core.controller;

import java.util.Date;

import org.junit.runners.model.TestTimedOutException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.core.bean.TestTb;

import cn.itcast.core.service.TestTbService;

@Controller

public class CenterController {


    @Autowired

    private TestTbService testTbService;


    //测试

    @RequestMapping(value = "/test/index.do")

    public void index(Model model){


        TestTb testTb = new TestTb();

        testTb.setName("范冰冰");

        testTb.setBirthday(new Date());


        testTbService.insertTestTb(testTb);


    }

}


在console中使用product中的service实现类

如果这样直接调用能够行的通吗?  当然是不行的, 在console里面定义的只是service方法, 那么这里是怎么直接调用到了product中的service实现类呢?

当然了, 这里就需要一些配置文件了:

首先是需要在product中注册服务:

dubbo-provider.xml:

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:task="http://www.springframework.org/schema/task"

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-4.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

        http://www.springframework.org/schema/task

          http://www.springframework.org/schema/task/spring-task-4.0.xsd

        http://code.alibabatech.com/schema/dubbo       

        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">



        <!-- 整合Dubbo -->

        <!-- 第一步:Dubbo起名称    计算用此名称来区分  -->

        <dubbo:application name="babasport-service-product"/>

        <!-- 第二步:中介  注册中心: zookeeper  redis ... -->

        <!-- <dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->

        <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>

        <!-- 第三步:设置dubbo的端口号    192.168.40.88:20880/接口  -->

        <dubbo:protocol name="dubbo" port="20880"/>

        <!-- 第四步:设置服务提供方 提供的接口 -->

        <dubbo:service interface="cn.itcast.core.service.TestTbService" ref="testTbService"/>


</beans>


在product中注册服务

接下来就是在console中使用了:

服务消费方:

dubbo-cusmer.xml:

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:task="http://www.springframework.org/schema/task"

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-4.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

        http://www.springframework.org/schema/task

          http://www.springframework.org/schema/task/spring-task-4.0.xsd

        http://code.alibabatech.com/schema/dubbo       

        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


        <!-- 整合Dubbo -->

        <!-- 第一步:Dubbo起名称    计算用此名称来区分  -->

        <dubbo:application name="babasport-console"/>

        <!-- 第二步:中介  注册中心    zookeeper  redis ... -->

        <!--<dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->

        <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>

        <!-- 第三步:调用服务提供方 提供的接口 -->

        <dubbo:reference interface="cn.itcast.core.service.TestTbService" id="testTbService"/>


</beans>


在console中使用

剩下的就是启动服务了:

注意先启动服务提供方, 然后再启动服务消费方.


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容