dubbo入门教程-从零搭建dubbo服务

蔚落 2021-11-06 02:02 452阅读 0赞

【原创 转载请注明出处】

本文是学习了dubbo之后自己手动写的,比较通俗,很多都是自己学习之后的理解,写的过程中没有参考任何文章。

另外dubbo也有官方文档,但是比较官方,也可以多看看dubbo的官方中文文档。

代码示例连接:dubbodemo


一、dubbo的相关概念

dubbo是阿里的一个分布式服务开源框架,它的设计理念就是把一个大而全的项目模块化,每个模块都是一个独立的项目。

为什么要把大项目拆分成多个小项目呢?

因为随着项目越做越大,代码、功能越来越多,导致代码的复用性就会降低,项目变得庞大臃肿,维护起来比较麻烦,改一个功能所有的代码都需要重新打包发布,还可能会影响其他模块;对于开发者来说所有的开发者都在同一个项目里开发代码,虽然有版本管理软件(SVN,Git等),但即使这样也会使开发者开发时遇到许多问题。

所以就诞生了分布式,分布式的原则就是将项目拆分若干个小项目,实现模块化,每个项目只关注自己的功能(假如按功能划分模块),需要其他模块的数据时就去调用它,分工更加明确。dubbo就是一个基于spring的分布式框架,可以和spring无缝整合。

那么模块之间该如何调用呢?

我们称服务提供者为服务生产者,服务调用者为服务消费者,他们两个是如何通信呢?消费者如何找到生产者呢?显然是通过网络,通过网络就必须要遵循一定的协议,约定,也就时需要有一个第三者或叫中间人、中介,称为注册中心,他来定义通讯的协议、规则。服务生产者服务消费者二者必须都来遵循这个规则。

服务生产者在启动程序时把服务发布到注册中心,告诉注册中心他叫啥,他提供的服务的类型,他的IP和端口就行了,而服务消费者在启动程序时也去连接注册中心,告诉注册中心他叫啥,告诉注册中心他想要什么类型的服务。对于服务提供方和服务消费方来说,他们还有可能兼具这两种角色,即既需要提供服务,有需要消费服务。

具体怎么实现调用呢?在项目里的表现如何?

模块之间要想调用,提供服务方需要创建服务接口,打成jar包,发布这个jar包,服务提供者面向接口编程,服务消费者调用服务时也用这个服务接口的jar包,创建服务接口的实例。

为什么要创建服务接口并发布成jar包?因为服务的提供方和服务的调用方都要用到这个接口。服务消费者要使用这个服务,需要获得服务的实例(只关心服务的类型)来调用服务提供的方法,而服务的提供者也只需要实现接口就可以了。

有了协议为什么还用dubbo?

那我们直接遵循协议,去和注册中心打交道不就行了吗,为什么还产生了dubbo呢?

毕竟有需求就有市场,归根结底还是因为我们懒。因为所有的人都要遵循这个协议,怎么连接网络,怎么传参数,格式如何等这些工作都是重复性的,是所有项目共性的,每次都写不就很麻烦吗,所以dubbo来帮你实现,来帮你完成这些共性的、繁琐的工作,你只需要关注业务本身就行了,那些麻烦的协议dubbo来帮你遵循,所以就产生了dubbo,并且可能他做的这些工作比你自己写起来更高效比你写的代码好。

服务的管理

当项目越来越大,模块、服务越来越多,一个项目可能会调用很多服务,或者服务之间相互调用,这个时候你可能就不知道你这个服务调用了哪些服务或都被谁调用了,性能如何等,并且管理起来比较麻烦,别担心,有服务治理中心可以帮你解决这些问题。它是dubbo的一个后台监控系统,叫dubbo-admin,可以下载他的war包运行在web容器里就可以用了。

二、使用dubbo框架来完成一个小demo

我们用idea+spring+maven+zookeeper+JDK 8,来搭建dubbo项目

前提是你安装好zookeeper,idea安装maven插件(也可以用其他IDE,过程类似)。

先说一下搭建的整体思路,是在idea里创建一个maven project,然后在这个project里创建三个model,分别是定义api接口的(将来发布到maven仓库),实现api的(服务提供者)以及服务消费者。

1、创建project

在idea里【file】-【new】-【project】

1205690-20190601190453187-988279151.png

然后按下图选择

1205690-20190601190253637-706872674.png

填写完相关信息后一路Next就好了,然后在这个project上右键【new】-【model】,创建为student-api,然后一路next后项目结构就是这样的

1205690-20190601191222117-1663369029.png

2、编写api接口

创建完project和第一个model之后,在student-api编写我们的接口。我们创建一些实体类和接口。

1205690-20190601191443585-2082331567.png

具体代码:

ContractedBlock.gif ExpandedBlockStart.gif

  1. public interface StudentService{
  2. ResultVO<Student> getStudentByName(String name);
  3. }

StudentService:

ContractedBlock.gif ExpandedBlockStart.gif

  1. public class Student implements Serializable{
  2. private String name;
  3. private String sex;
  4. private int age;
  5. public Student() {
  6. }
  7. public Student(String name, String sex, int age) {
  8. this.name = name;
  9. this.sex = sex;
  10. this.age = age;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getSex() {
  19. return sex;
  20. }
  21. public void setSex(String sex) {
  22. this.sex = sex;
  23. }
  24. public int getAge() {
  25. return age;
  26. }
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30. }

Student

ContractedBlock.gif ExpandedBlockStart.gif

  1. public class ResultVO<T> implements Serializable{
  2. private T data;
  3. private String msg;
  4. private String status;
  5. public ResultVO() {
  6. }
  7. public ResultVO(T data, String msg, String status) {
  8. this.data = data;
  9. this.msg = msg;
  10. this.status = status;
  11. }
  12. public T getData() {
  13. return data;
  14. }
  15. public void setData(T data) {
  16. this.data = data;
  17. }
  18. public String getMsg() {
  19. return msg;
  20. }
  21. public void setMsg(String msg) {
  22. this.msg = msg;
  23. }
  24. public String getStatus() {
  25. return status;
  26. }
  27. public void setStatus(String status) {
  28. this.status = status;
  29. }
  30. }

ResultVO

3、api打包

我们在第2步写了一个简单的接口,和一些实体类,这一步我们需要将api整个项目打包,安装到本地maven仓库。因为服务提供者和消费者都依赖这个api项目。具体操作如下

选中student-api右键【maven】-【install -e】

1205690-20190601192052924-541107949.png

这一步操作的前提是,你的IDE安装了maven插件,如果没安装,右键是没有【maven】选项的,具体怎么安装这里不讲。

install -e是执行maven的命令,此操作会将项目打包并安装到maven本地仓库,-e是会在idea控制台打日志。

执行命令后过一会查看控制台

1205690-20190601192742547-1591176007.png

就是成功了,安装目录页告诉我们了。我们去对应目录验证下,已经有了。样我们就可以像以往引用别人的依赖那样引用我们自己的api了

1205690-20190601193017200-245237608.png

4、创建服务提供者

这个时候我们就可以创建服务提供者了,同样像创建student-api那样创建服务提供者命名为student-server

只不过,在选择时我们将它创建为webapp,因为这是一个spring web项目

1205690-20190601200856407-324453002.png

5、引入依赖

在project(父项目,即最外层的那个)的pom.xml里引入dubbo和zkClient的依赖,因为服务提供者和消费者都需要这个依赖,所以写在父项目里。

ContractedBlock.gif ExpandedBlockStart.gif

  1. <!--spring web-->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-webmvc</artifactId>
  5. <version>4.3.13.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>junit</groupId>
  9. <artifactId>junit</artifactId>
  10. <version>4.9</version>
  11. </dependency>
  12. <!--引入dubbo-->
  13. <dependency>
  14. <groupId>com.alibaba</groupId>
  15. <artifactId>dubbo</artifactId>
  16. <version>2.5.4</version>
  17. </dependency>
  18. <!--引入zookeeper-->
  19. <dependency>
  20. <groupId>com.github.sgroschupf</groupId>
  21. <artifactId>zkclient</artifactId>
  22. <version>0.1</version>
  23. <exclusions>
  24. <exclusion>
  25. <artifactId>junit</artifactId>
  26. <groupId>junit</groupId>
  27. </exclusion>
  28. <exclusion>
  29. <artifactId>log4j</artifactId>
  30. <groupId>log4j</groupId>
  31. </exclusion>
  32. </exclusions>
  33. </dependency>
  34. <dependency>
  35. <groupId>log4j</groupId>
  36. <artifactId>log4j</artifactId>
  37. <version>1.2.17</version>
  38. </dependency>
  39. <!--json数据转换-->
  40. <dependency>
  41. <groupId>com.fasterxml.jackson.core</groupId>
  42. <artifactId>jackson-databind</artifactId>
  43. <version>2.9.8</version>
  44. </dependency>

pom依赖

引入student-api的依赖

  1.   <dependency>
  2. <groupId>com.dubbodemo.me</groupId>
  3. <artifactId>student-api</artifactId>
  4. <version>1.0</version>
  5. </dependency>

6、实现服务

在student-server项目里实现StudentService接口

ContractedBlock.gif ExpandedBlockStart.gif

  1. @Service
  2. public class StudentServiceImpl implements StudentService{
  3. @Override
  4. public ResultVO<Student> getStudentByName(String name) {
  5. Student student = new Student(name,"m",24);
  6. ResultVO<Student> studentResultVO = new ResultVO<Student>(student,"success","1");
  7. return studentResultVO;
  8. }
  9. }

StudentServiceImpl

7、创建消费者

服务提供者已经写好,接下来我们写服务消费者。和student-server创建方式一样,只不过命名为student-client

在这个项目里就不用引入dubbo的依赖了,因为我们已经把依赖放到父项目里了,各个model都能引用

此时项目都创建完了,看看结构

1205690-20190601202528345-1244651783.png

然后我们在student-client里调用服务,我们写一个控制层StudentController.java

ContractedBlock.gif ExpandedBlockStart.gif

  1. @Controller
  2. public class StudentController{
  3. Logger logger = Logger.getLogger(StudentController.class);
  4. @Resource
  5. private StudentService studentService;
  6. @ResponseBody
  7. @RequestMapping("/getData")
  8. public ResultVO<Student> getData(String name){
  9. ResultVO<Student> studentByName = studentService.getStudentByName(name);
  10. Student student = studentByName.getData();
  11. logger.info("+++++++++返回码是:"+studentByName.getStatus()+" name:"+student.getName()+" resultSet:"+student.getSex());
  12. return studentByName;
  13. }
  14. }

StudentController

至此,我们的代码已经全部写完了,接下来就是把服务提供者和服务消费者都启动,来验证一下。

8、配置

你会发现直到我们把代码都写完了,还没用到有关dubbo的东西呢,没错,dubbo的关键是配置。

首先需要明确的是,student-server和student-client会以web项目启动,而student-client还要用到Spring MVC。

我们先来配置student-server,在resources下创建ApplicationContext.xml为什么是ApplicationContext.xml?因为服务提供者只用到了spring,现在是提供服务没有mvc)

ContractedBlock.gif ExpandedBlockStart.gif

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  10. <!--开启注解-->
  11. <context:annotation-config/>
  12. <!--扫描注解-->
  13. <context:component-scan base-package="com.dubbodemo.me.service"/>
  14. <!--dubbo可以和spring无缝整合-->
  15. <!--配置目的:找到注册中心,告诉注册中心,是谁在向它注册,IP是多少,提供服务的端口是多少-->
  16. <!--1、配置别名,目的是在后台好区分到底是谁, name 可以随便写,最好语义化-->
  17. <dubbo:application name="student-server"/>
  18. <!--2、注册服务, zookeeper 注册中心; address 注册中心的地址;protocol 注册中心的协议-->
  19. <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
  20. <!--3、告诉注册中心我要提供的服务 interface 代表发布的服务的类型 ref代表要发布的是哪个服务(具体的实现) timeout超时时间-->
  21. <dubbo:service interface="com.dubbodemo.me.api.StudentService" ref="studentServiceImpl" timeout="6000"/>
  22. <!--4、配置服务的端口,因为消费者必须通过IP+端口才能访问我的服务,我们在注册中心注册时注册中心就已经知道我们的IP了,
  23. 所以现在只需要告诉他端口 端口可以随便写,前提不可被其他程序占用。一个dubbo被发布时必须独占一个端口-->
  24. <dubbo:protocol name="dubbo" port="12003"/>
  25. </beans>

ApplicationContext.xml

服务提供者配置完了,我们再来配置消费者,在student-client的resources下创建spring-mvc.xml(为什么是spring-mvc.xml?因为服务消费者我们用的是spring mvc,到时候访问controller验证服务调用)

ContractedBlock.gif ExpandedBlockStart.gif

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  10. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  11. <!--开启注解-->
  12. <context:annotation-config/>
  13. <!--扫描路径-->
  14. <context:component-scan base-package="com.dubbodemo.me.controller"/>
  15. <!--1、配置别名,目的是在后台好区分到底是谁, name 可以随便写,最好语义化-->
  16. <dubbo:application name="student-client"/>
  17. <!--2找到注册中心-->
  18. <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper" />
  19. <!--3告诉注册中心你要什么 id可以随便写-->
  20. <dubbo:reference interface="com.dubbodemo.me.api.StudentService" id="studentServiceImpl"/>
  21. <!--因为我是消费者,不提供服务,所以不用配置端口 当我也是服务提供者时需要配置-->
  22. <!--json转换 我们将实体类直接返回 它会帮我们转成json-->
  23. <mvc:annotation-driven />
  24. <mvc:annotation-driven>
  25. <mvc:message-converters>
  26. <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
  27. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  28. <property name="objectMapper">
  29. <bean class="com.fasterxml.jackson.databind.ObjectMapper">
  30. <property name="dateFormat">
  31. <bean class="java.text.SimpleDateFormat">
  32. <constructor-arg type="java.lang.String" value="yyyy-MM-dd" />
  33. </bean>
  34. </property>
  35. </bean>
  36. </property>
  37. </bean>
  38. </mvc:message-converters>
  39. </mvc:annotation-driven>
  40. </beans>

spring-mvc.xml

student-server的web.xml配置:读取spring配置文件,加载spring

ContractedBlock.gif ExpandedBlockStart.gif

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <filter>
  7. <filter-name>encodingFilter</filter-name>
  8. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  9. <!-- <async-supported>true</async-supported> -->
  10. <init-param>
  11. <param-name>encoding</param-name>
  12. <param-value>UTF-8</param-value>
  13. </init-param>
  14. <init-param>
  15. <param-name>forceEncoding</param-name>
  16. <param-value>true</param-value>
  17. </init-param>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>encodingFilter</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. <context-param>
  24. <param-name>contextConfigLocation</param-name>
  25. <param-value>classpath:ApplicationContext.xml</param-value>
  26. </context-param>
  27. <listener>
  28. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  29. </listener>
  30. </web-app>

web.xml

student-client的web.xml配置: 读取spring配置文件,加载spring mvc模块

ContractedBlock.gif ExpandedBlockStart.gif

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <filter>
  7. <filter-name>encodingFilter</filter-name>
  8. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  9. <!-- <async-supported>true</async-supported> -->
  10. <init-param>
  11. <param-name>encoding</param-name>
  12. <param-value>UTF-8</param-value>
  13. </init-param>
  14. <init-param>
  15. <param-name>forceEncoding</param-name>
  16. <param-value>true</param-value>
  17. </init-param>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>encodingFilter</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. <servlet>
  24. <servlet-name>springMVC</servlet-name>
  25. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  26. <init-param>
  27. <param-name>contextConfigLocation</param-name>
  28. <param-value>classpath:spring-mvc.xml</param-value>
  29. </init-param>
  30. <!--启动项目时就创建这个servlet对象,这个参数必须在init-param之后出现-->
  31. <load-on-startup>2</load-on-startup>
  32. </servlet>
  33. <servlet-mapping>
  34. <servlet-name>springMVC</servlet-name>
  35. <url-pattern>/</url-pattern>
  36. </servlet-mapping>
  37. </web-app>

web.xml

9、验证

验证之前,我们先启动zookeeper

然后再依次启动student-server项目和student-client项目

启动完成后,访问student-client的接口localhost:9001/getData?name=tom

1205690-20190601221555333-2140070440.png

控制台日志

1205690-20190601222242498-1898392900.png

调用成功了。本教程源码已经上传到GitHub上了,传送门

题外话:为什么两个web项目的web.xml配置不一样?

student-client里的controller是交给SpringMVC来处理,并不是spring容器。若交给spring容器来处理,代表项目一启动就要创建出来controller,这个时候可能还没有链接网络(没连zookeeper)可能还拿不到远程对象,就会注入失败,所以不能交给spring容器来处理。

转载于:https://www.cnblogs.com/ibigboy/p/10954791.html

发表评论

表情:
评论列表 (有 0 条评论,452人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Dubbo 入门

    一、背景与架构(摘自官网) 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不

    相关 Dubbo - Dubbo框架

    简单说明 Dubbo是一个基于RPC协议的分布式框架,RPC的全称为Remote Procedure Call,其目的为实现远程调用。而分布式的目的是为了能够更好的针对不

    相关 dubbo入门教程-dubbo服务

    【原创 转载请注明出处】 本文是学习了dubbo之后自己手动写的,比较通俗,很多都是自己学习之后的理解,写的过程中没有参考任何文章。 另外dubbo也有官方文档,但是比较官