国内分布式框架Dubbo使用详解

作者:肥学 时间:2022-05-10 13:38:27 

介绍

Dubbo 是一款高性能、轻量级的 Java RPC 框架,由阿里巴巴开源并贡献至 Apache 基金会。它能够提供服务的注册与发现、负载均衡、服务治理等功能,简化了分布式系统的开发过程。下面我们将详细介绍 Dubbo 的原理和使用方法,并附上相关的 Java 代码示例。

Dubbo的原理

Dubbo 的核心是一个基于 Java 序列化的远程过程调用(RPC)框架,它的工作流程可以分为如下几个步骤:

  • 服务提供者向注册中心注册自己提供的服务。

  • 服务消费者从注册中心获取服务提供者的地址,并建立连接。

  • 服务消费者通过 RPC 调用远程服务,实现分布式调用

Dubbo 的架构中包含以下几个重要组件:

  • Provider:服务提供者,将服务发布到注册中心,供 Consumer 调用。

  • Consumer:服务消费者,从注册中心获取 Provider 的地址,并发起 RPC 调用。

  • Registry:注册中心,存储 Provider 的地址信息,供 Consumer 获取。

Monitor:监控中心,用于统计 Provider 的运行状态和性能指标。

国内分布式框架Dubbo使用详解

Dubbo 支持多种协议和序列化方式,包括 Dubbo 协议、HTTP 协议、Hessian 协议、Thrift 协议等。同时,它还提供了负载均衡、服务容错、动态路由等功能,可以根据不同的需求进行配置。

基本使用

  • 编写服务接口

public interface HelloService {
   String sayHello(String name);
}
  • 实现服务接口

public class HelloServiceImpl implements HelloService {
   public String sayHello(String name) {
       return "Hello, " + name;
   }
}
  • 配置Dubbo 在Dubbo的XML配置文件中定义服务提供者和注册中心,配置服务接口和实现类的关联。

<?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://dubbo.apache.org/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
   <!-- 指定服务提供者应用名 -->
   <dubbo:application name="hello-provider"/>
   <!-- 指定注册中心地址 -->
   <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
   <!-- 指定通信协议和端口号 -->
   <dubbo:protocol name="dubbo" port="20880"/>
   <!-- 暴露服务 -->
   <dubbo:service interface="com.example.HelloService" ref="helloService"/>
   <!-- 服务接口和实现类的关联 -->
   <bean id="helloService" class="com.example.provider.HelloServiceImpl"/>
</beans>
  • 启动服务提供者 在服务提供者的main方法中启动Dubbo。

public class Provider {
   public static void main(String[] args) throws Exception {
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
       context.start();
       System.in.read(); // 按任意键退出
   }
}

服务提供者通过启动 Spring 容器来启动 Dubbo 服务,这里使用的是 ClassPathXmlApplicationContext,它会从类路径下加载 provider.xml 文件,初始化 Spring 容器并启动 Dubbo 服务。

  • 编写服务消费者

public class Consumer {
   public static void main(String[] args) {
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
       HelloService helloService = (HelloService) context.getBean("helloService");
       String result = helloService.sayHello("world");
       System.out.println(result);
   }
}
  • 配置Dubbo 在Dubbo的XML配置文件中定义服务消费者和注册中心。

<?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://dubbo.apache.org/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
   <!-- 指定服务消费者应用名 -->
   <dubbo:application name="hello-consumer"/>
   <!-- 指定注册中心地址 -->
   <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
   <!-- 引用远程服务 -->
   <dubbo:reference id="helloService" interface="com.example.HelloService"/>
</beans>
  • 启动服务消费者

public class Consumer {
   public static void main(String[] args) {
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
       HelloService helloService = (HelloService) context.getBean("helloService");
       String result = helloService.sayHello("world");
       System.out.println(result);
   }
}

简单的使用就是这样,关于zookeeper我们下次在详细说一下。

来源:https://juejin.cn/post/7209919510722265149

标签:Dubbo,分布式,框架
0
投稿

猜你喜欢

  • java简单坦克大战制作代码

    2023-02-07 05:08:37
  • SSM项目使用拦截器实现登录验证功能

    2023-06-17 16:12:38
  • SpringBoot 配置文件总结

    2021-09-06 13:12:57
  • 使用java生成字母验证码

    2021-10-29 23:50:25
  • Spring整合junit的配置过程图解

    2022-12-18 16:37:48
  • Java 十大排序算法之冒泡排序刨析

    2022-07-05 19:30:29
  • 关于ObjectUtils.isEmpty() 和 null 的区别

    2022-05-07 17:10:56
  • JAVA熔断和降级真实关系的图文详解

    2023-11-30 12:39:20
  • IDEA 2020.3最新永久激活码(免费激活到 2099 年,亲测有效)

    2023-07-14 05:37:43
  • springboot 2.0 mybatis mapper-locations扫描多个路径的实现

    2023-07-12 02:30:53
  • 详解C# TimeSpan 计算时间差(时间间隔)

    2021-07-12 11:00:10
  • Android TextView实现跑马灯效果的方法

    2023-07-30 20:44:12
  • 通过Java连接SQL Server数据库的超详细操作流程

    2022-08-01 12:46:01
  • Java开发实现人机猜拳游戏

    2023-10-17 16:01:35
  • 浅谈Java常见的排序算法

    2023-09-08 11:11:11
  • 老生常谈Java String字符串(必看篇)

    2023-06-20 19:56:20
  • Java原生服务器接收上传文件 不使用MultipartFile类

    2023-11-09 19:31:36
  • spring mvc 实现获取后端传递的值操作示例

    2023-08-10 12:55:52
  • Java代码实现矩形覆盖实例

    2022-02-04 22:02:02
  • JAVA遍历map的几种实现方法代码

    2023-08-27 22:06:04
  • asp之家 软件编程 m.aspxhome.com