Dubbo 入门搭建

青旅半醒 2023-02-14 02:40 120阅读 0赞

一、背景与架构(摘自官网)

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。
在这里插入图片描述
在这里插入图片描述
可以看出Dubbo的架构很像生产者-消费者模型。只是在这种模型上,加上了注册中心和监控中心,用于管理提供方提供的url,以及管理整个过程。

调用关系说明

  1. 服务容器负责启动,加载,运行服务提供者。
  2. 服务提供者在启动时,向注册中心注册自己提供的服务。
  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

二、上手搭建

一般以Zookeeper作为注册中心,所以在搭建项目之前需要先安装ZK,并启动,这里直接启动。
在这里插入图片描述
IDEA创建模块化项目
在这里插入图片描述
整体项目的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>dubbo-demo</groupId>
  7. <artifactId>dubbo-demo</artifactId>
  8. <packaging>pom</packaging>
  9. <version>1.0-SNAPSHOT</version>
  10. <modules>
  11. <module>dubbo-provider</module>
  12. <module>dubbo-consumer</module>
  13. <module>dubbo-api</module>
  14. </modules>
  15. <properties>
  16. <maven.compiler.source>1.8</maven.compiler.source>
  17. <maven.compiler.target>1.8</maven.compiler.target>
  18. </properties>
  19. </project>

dubbo-api模块:保存公共接口

以登录接口为例
在这里插入图片描述
登陆服务接口

  1. public interface LoginService {
  2. public boolean login();
  3. }

api 模块pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>dubbo-demo</artifactId>
  7. <groupId>dubbo-demo</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>dubbo-api</artifactId>
  12. </project>

dubbo-provider 模块: 服务提供方

主要实现公共接口中的LoginService 并使用xml进行配置。
在这里插入图片描述
登陆服务的实现类

  1. /**
  2. * @author 阳光大男孩!!!
  3. */
  4. public class LoginServiceImpl implements LoginService {
  5. @Override
  6. public boolean login() {
  7. return true;
  8. }
  9. }

springboot启动类

  1. public class ProviderApp
  2. {
  3. public static void main( String[] args ) throws IOException {
  4. //加载xml配置文件启动
  5. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/provider.xml");
  6. context.start();
  7. System.out.println("消费者已启动");
  8. System.in.read();
  9. }
  10. }

provider.xml

  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:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  9. <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
  10. <dubbo:application name="demotest-provider" owner="sunny" organization="dubbox"/>
  11. <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
  12. <dubbo:registry address="zookeeper://localhost:2181"/>
  13. <!-- 用dubbo协议在20880端口暴露服务 -->
  14. <dubbo:protocol name="dubbo" port="20880" />
  15. <!--使用 dubbo 协议实现定义好的 LoginService 接口-->
  16. <dubbo:service interface="com.sunny.dubbo.API.LoginService" ref="providerService" protocol="dubbo" />
  17. <!--具体实现该接口的 bean-->
  18. <bean id="providerService" class="com.sunny.dubbo.provider.service.LoginServiceImpl"/>
  19. </beans>

另外,需要注意的是,dubbo默认打开了qos,它是dubbo的运维命令支持。
使用的端口默认为 22222,可通过配置文件dubbo.properties 修改:

  1. dubbo.application.qos.port=33333

说这个的目的在于,如果不配置可能会报告端口冲突,如果出现了这个问题,你可以像上边那样更改端口,也可以像我一样,在我的这个demo中,我直接enable 了qos,是通过在dubbo.properties 中设置的。

  1. dubbo.application.qos.enable=false

提供方module 的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <groupId>dubbo-demo</groupId>
  7. <artifactId>dubbo-demo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>dubbo-provider</artifactId>
  12. <dependencies>
  13. <!-- 导入公共接口的依赖-->
  14. <dependency>
  15. <groupId>dubbo-demo</groupId>
  16. <artifactId>dubbo-api</artifactId>
  17. <version>1.0-SNAPSHOT</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>3.8.1</version>
  23. <scope>test</scope>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
  26. <dependency>
  27. <groupId>com.alibaba</groupId>
  28. <artifactId>dubbo</artifactId>
  29. <version>2.6.6</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.apache.zookeeper</groupId>
  33. <artifactId>zookeeper</artifactId>
  34. <version>3.4.10</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>com.101tec</groupId>
  38. <artifactId>zkclient</artifactId>
  39. <version>0.5</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>io.netty</groupId>
  43. <artifactId>netty-all</artifactId>
  44. <version>4.1.32.Final</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.apache.curator</groupId>
  48. <artifactId>curator-framework</artifactId>
  49. <version>2.8.0</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.apache.curator</groupId>
  53. <artifactId>curator-recipes</artifactId>
  54. <version>2.8.0</version>
  55. </dependency>
  56. </dependencies>
  57. </project>

dubbo- consumer :服务消费方

服务消费方就很简单了,只需要调用服务即可。
在这里插入图片描述
服务消费方xml配置,主要是指定ZK的地址以及调用的接口即可。

  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:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  6. <dubbo:application name="consumer" owner="sunny"/>
  7. <dubbo:registry address="zookeeper://localhost:2181" check="false"/>
  8. <dubbo:reference id="providerService"
  9. interface="com.sunny.dubbo.API.LoginService"/>
  10. </beans>

启动类

  1. /**
  2. * xml的方式调用
  3. * 服务消费方
  4. * @author 阳光大男孩!!!
  5. */
  6. public class ConsumerApp
  7. {
  8. public static void main(String[] args) {
  9. //测试常规服务
  10. ClassPathXmlApplicationContext context =
  11. new ClassPathXmlApplicationContext("consumer.xml");
  12. context.start();
  13. System.out.println("consumer start");
  14. LoginService loginService = context.getBean(LoginService.class);
  15. System.out.println("登录的结果为"+loginService.login());
  16. }
  17. }

pom.xml 文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>dubbo-demo</artifactId>
  7. <groupId>dubbo-demo</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>dubbo-consumer</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>dubbo-demo</groupId>
  15. <artifactId>dubbo-api</artifactId>
  16. <version>1.0-SNAPSHOT</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>3.8.1</version>
  22. <scope>test</scope>
  23. </dependency>
  24. <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
  25. <dependency>
  26. <groupId>com.alibaba</groupId>
  27. <artifactId>dubbo</artifactId>
  28. <version>2.6.6</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.apache.zookeeper</groupId>
  32. <artifactId>zookeeper</artifactId>
  33. <version>3.4.10</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>com.101tec</groupId>
  37. <artifactId>zkclient</artifactId>
  38. <version>0.5</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>io.netty</groupId>
  42. <artifactId>netty-all</artifactId>
  43. <version>4.1.32.Final</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.apache.curator</groupId>
  47. <artifactId>curator-framework</artifactId>
  48. <version>2.8.0</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.apache.curator</groupId>
  52. <artifactId>curator-recipes</artifactId>
  53. <version>2.8.0</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>dubbo-demo</groupId>
  57. <artifactId>dubbo-provider</artifactId>
  58. <version>1.0-SNAPSHOT</version>
  59. <scope>compile</scope>
  60. </dependency>
  61. </dependencies>
  62. </project>

运行测试

在打开Zookeeper的情况下,运行服务提供方,然后再运行消费方即可。
在这里插入图片描述
在这里插入图片描述

总结

总台来看,dubbo主体还是生产者消费者模型的思想来实现分布式的,通过ZK作为注册中心,生产者向ZK注册服务,消费者订阅服务。

本次demo的git地址:https://github.com/SunnyBoy-WYH/Dubbo-demo

发表评论

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

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

相关阅读

    相关 Dubbo 入门

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

    相关 Dubbo - Dubbo框架

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

    相关 dubbo入门教程-从零dubbo服务

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