初识Dubbo

作为一个程序员小白,发现公司的项目中使用了Dubbo,于是闲暇之余学习了一下Dubbo的基础知识。

Dubbo的定义

百度上对Dubbo的定义是这样的:

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

Dubbo是一款高性能、轻量级的开源Java
RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

三大核心部件

Remoting

网络通信框架,实现了 sync-over-async 和request-response 消息机制。

RPC

一个远程过程调用的抽象,支持负载均衡、容灾和地址路由,动态配置等集群功能。

Registry

服务目录框架用于服务的注册和服务事件发布和订阅。

Spring集成

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

搭建第一个Dubbo服务

服务端

定义一个服务接口(HelloService.java)

package com.alibaba.hello.api;

public interface HelloService

{

  String sayHelloWorld(String name);

}

接口实现类(HelloServiceImpl.java)

package com.alibaba.hello.impl;

import com.alibaba.hello.api.HelloService;

public class HelloServiceImpl  implements HelloService{

    public String  sayHelloWorld(String  name){

        return"Hello"+ name;

    }

}

Spring配置(provide.xml)

<?xmlversion="1.0"encoding="UTF-8"?>

<beans......>

    <!--Applicationname-->

    <dubbo:applicationname="hello-world-app"/>
<!--registryaddress,usedforservicetoregisteritself-->

<dubbo:registryaddress="multicast://127.0.0.1:1234"/>

<!--exposethisservicethroughdubboprotocol,throughport20880-->

<dubbo:protocolname="dubbo"port="20880"/>

<!--whichserviceinterfacedoweexpose?-->

<dubbo:serviceinterface="com.alibaba.hello.api.HelloService"ref="helloService"/>

<!--designateimplementation-->

<beanid="helloService"class="com.alibaba.hello.impl.HelloServiceImpl"/>

测试(Provider.java)

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider{

    public static void main(String[]  args){
    ClassPathXmlApplicationContext  context = newClassPathXmlApplicationContext(newString[]{"provider.xml"});

    //启动成功,监听端口为20880

    System.in.read();//按任意键退出

}

}

客户端

Spring配置文件(consumer.xml)

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns=......>

    <!--consumerapplicationname-->
<dubbo:applicationname="consumer-of-helloworld-app"/>

<!--registryaddress,usedforconsumertodiscoverservices-->

<dubbo:registryaddress=" multicast://127.0.0.1:1234 "/>

<!--whichservicetoconsume?-->

<dubbo:referenceid="helloService"interface="com.alibaba.hello.api.HelloService"/>

客户端测试(Consumer.java)

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.hello.api.HelloService;

public class Consumer{

    public static void main(String[]  args){

        ClassPathXmlApplicationContext  context = newClassPathXmlApplicationContext(newString[]{"consumer.xml"});

    HelloService  helloService = (HelloService)context.getBean("helloService");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wunianisme

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值