Dubbo学习入门

2024-09-08 09:58
文章标签 学习 入门 dubbo

本文主要是介绍Dubbo学习入门,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文参考自:Dubbo用户手册(中文)http://dubbo.apache.org/books/dubbo-user-book/

现在的参考文档地址:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

入门请参考自《Dubbo用户手册(中文)》第一节,在手册第二节说明如何快速启动Dubbo,下面就顺着手册的使用方式,自己搭建一个快速的入门应用(helloWorld)

1.前提

1.1.Dubbo整体结构

对于使用者来说Dubbo中主要存在有四种角色:消费者、提供者、注册中心、监视器。

  1. 提供者:是用来提供Duddo的服务,一般是在Service层暴露服务
  2. 消费者:是使用Dubbo提供的服务,一般消费者可以直连或者通过注册中心获取服务
  3. 注册中心:该组件负责服务的发现与注册,并通过负载算法给消费者提供某台提供者的服务
  4. 监控中心:dubbo提供的简单监控中心,可以用来显示接口暴露,注册情况,也可以看接口的调用明细,调用时间等。

 

1.2.目录结构

 

1.2.Maven依赖

 

服务提供者maven依赖:

<!--zkclient -->
<dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>${zkclient.version}</version>
</dependency>
<!-- dubbo -->
<dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion></exclusions>
</dependency>

服务消费者maven依赖:这里主要注意的是需要引入服务提供者的maven依赖以供使用

<dependency><groupId>org.springframework.samples</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version>
</dependency>
<!--zkclient -->
<dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>${zkclient.version}</version>
</dependency>
<!-- dubbo -->
<dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion></exclusions>
</dependency>
<!-- 注册中心zookeeper -->  
<dependency>  <groupId>org.apache.zookeeper</groupId>  <artifactId>zookeeper</artifactId>  <version>3.3.6</version>  
</dependency>

主要是添加dubbo的maven依赖

2.服务提供者

2.1.定义服务接口与实现

package com.demo.service;public interface DemoService {String sayHello(String str);
}
package com.demo.service.impl;
import com.demo.service.DemoService;public class DemoServiceImpl implements DemoService{@Overridepublic String sayHello(String str) {return "hello, " + str;}
}

2.2用Spring配置声明暴露服务

<?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.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!--服务提供方应用信息,用于计算依赖关系,需要唯一 --><dubbo:application name="dubboProvider"/><!--使用zookeeper注册中心暴露服务地址 --><dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/><!--用Dubbo协议在20880端口,暴露服务 Dubbo协议:采用NIO复用单一长连接,并使用线程池并发处理请求,减少握手和加大并发效率,性能较好(推荐使用)rmi协议:传入传出参数数据包大小混合,消费者与提供者个数差不多,可传文件!--><dubbo:protocol name="dubbo" port="20880"/><!--要提供的服务接口 interface是服务名,ref是要引用的spring bean--><dubbo:service interface="com.demo.service.DemoService" ref="demoService" timeout="1000" retries="3" owner="wuxinhui"></dubbo:service><!-- 普通Spring Bean --><bean id="demoService" class="com.demo.service.impl.DemoServiceImpl"></bean>
</beans>

2.3.Main函数中加载Spring配置(App.class)

public class App {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationProvider.xml");context.start();System.out.println("按任意键退出......");System.in.read();context.close();}
}

3.服务消费者

3.1.通过Spring配置引用远程服务

<?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.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!--服务提供方应用信息,用于计算依赖关系,唯一 --><dubbo:application name="dubboConsumer"/><!-- 使用zookeeper注册中心暴露服务地址 --><dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/><dubbo:reference id="demoService" interface="com.demo.service.DemoService"></dubbo:reference>
</beans>

3.2.加载Spring配置并调用远程服务

public class App {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationConsumer.xml");context.start();DemoService demoService = (DemoService) context.getBean("demoService");System.out.println(demoService.sayHello("wuxinhui"));context.close();}
}

4.注册中心安装

参考:https://blog.csdn.net/tlk20071/article/details/52028945

5.Dubbo Springboot Project

参考:https://github.com/apache/incubator-dubbo-spring-boot-project

这里要注意的是,version版本号不能选择2.0以上的,因为官方非正式版的更新速度太慢,很多接口在Spring5中已经发生了变动,如下所示:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version><relativePath/> <!-- lookup parent from repository -->
</parent>

 

 

 

 

 

这篇关于Dubbo学习入门的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1147802

相关文章

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

Dubbo之SPI机制的实现原理和优势分析

《Dubbo之SPI机制的实现原理和优势分析》:本文主要介绍Dubbo之SPI机制的实现原理和优势,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Dubbo中SPI机制的实现原理和优势JDK 中的 SPI 机制解析Dubbo 中的 SPI 机制解析总结Dubbo中

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis