一、初识dubbo:

架构图

  Provider: 暴露服务的服务提供方。

       Consumer: 调用远程服务的服务消费方。

       Registry: 服务注册与发现的注册中心。

       Monitor: 统计服务的调用次调和调用时间的监控中心。

       Container: 服务运行容器。

流程:

  服务容器负责启动,加载,运行服务提供者。

  服务提供者在启动时,向注册中心注册自己提供的服务。

  服务消费者在启动时,向注册中心订阅自己所需的服务。

  注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。 

  服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

  服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

注册中心选择

 dubbo支持多种类型的注册中心:

  •   Multicast注册中心
  •   Zookeeper注册中心
  •   Redis注册中心
  •   Simple注册中心

二、ZooKeeper

下载:https://archive.apache.org/dist/zookeeper/

windows安装:https://blog.csdn.net/tlk20071/article/details/52028945

三、服务Demo搭建

1、新建一个maven工程:结构如下

其中模块dubbo-api用于给提供服务模块dubbo-provider和消费服务dubbo-cosumer提供公用接口,各目录结构为:

模块dubbo-api简单测试代码如下:

  1. package com.example.demo;
  2. public interface DemoService {
  3. String testService();
  4. }

提供服务模块dubbo-provider:

  pom文件:

  

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.example</groupId>
  7. <artifactId>dubbo-provider</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>dubbo-provider</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>2.0.0.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. </properties>
  26.  
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter</artifactId>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  1.     <!-- dubbo-api依赖 -->
  1. <dependency>
  2. <groupId>com.example</groupId>
  3. <artifactId>dubbo-api</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </dependency>
  1.     <!-- dubbo依赖 -->
  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>dubbo</artifactId>
  4. <version>2.4.10</version>
  5. <exclusions>
  6. <exclusion>
  7. <artifactId>spring</artifactId>
  8. <groupId>org.springframework</groupId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12.     <!-- zkclient -->
  13. <dependency>
  14. <groupId>com.101tec</groupId>
  15. <artifactId>zkclient</artifactId>
  16. <version>0.3</version>
  17. </dependency>
  18. </dependencies>
  19.  
  20. <build>
  21. <plugins>
  22. <plugin>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-maven-plugin</artifactId>
  25. </plugin>
  26. </plugins>
  27. </build>
  28.  
  29.  
  30. </project>

  实现接口类:

  1. package com.example.demo.impl;
  2. import com.example.demo.DemoService;
  3. public class DemoServiceimpl implements DemoService {
  4. @Override
  5. public String testService() {
  6. return "provider testService";
  7. }
  8. }

  启动类:

  1. package com.example.demo.provider;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import java.io.IOException;
  4. public class Provider {
  5. public static void main(String[] args) {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  7. "classpath*:provider.xml");
  8. context.start();
  9. System.out.println("dubbo service begin to start");
  10. try {
  11. System.in.read();
  12. } catch (IOException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. }
  17. }

  配置文件:

  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="demo-provider" owner="programmer" organization="dubbox"/>
  11. <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
  12. <dubbo:registry address="zookeeper://localhost:2181"/>
  13. <!-- 用dubbo协议在20880端口暴露服务 -->
  14. <dubbo:protocol name="dubbo" port="20880" />
  15. <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
  16. <dubbo:service interface="com.example.demo.DemoService" ref="demoService" protocol="dubbo" />
  17. <!--具体实现该接口的 bean-->
  18. <bean id="demoService" class="com.example.demo.impl.DemoServiceimpl"/>
  19. </beans>

消费服务dubbo-cosumer:

  pom文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.example</groupId>
  7. <artifactId>dubbodemo</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>dubbodemo</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>2.0.0.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. </properties>
  26.  
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
  39.  
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48.  
  49.  
  50. </project>

  启动类: 

  1. package com.example.demo.consumer;
  2. import com.example.demo.DemoService;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Consumer {
  5. public static void main(String[] args) {
  6. //测试服务
  7. ClassPathXmlApplicationContext context =
  8. new ClassPathXmlApplicationContext("consumer.xml");
  9. context.start();
  10. System.out.println("consumer start");
  11. DemoService demoService = context.getBean(DemoService.class);
  12. System.out.println("consumer");
  13. System.out.println(demoService.testService());
  14. }
  15. }

  配置文件:

  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
  6. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  7. <dubbo:application name="demo-consumer" owner="programmer" organization="dubbox"/>
  8. <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
  9. <dubbo:registry address="zookeeper://localhost:2181"/>
  10. <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
  11. <dubbo:reference id="demoService" interface="com.example.demo.DemoService"/>
  12. </beans>

四、测试:

zookeeper服务启动后,先启动服务提供方dubbo-provider,再运行dubbo-consuner,效果如下:

 

版权声明:本文为kingsonfu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/kingsonfu/p/8669393.html