Dubbo 源码分析系列之一环境搭建

时间:2023-03-09 16:34:47
Dubbo 源码分析系列之一环境搭建

环境搭建的步骤有哪些

  1. 依赖外部的环境
  2. 使用的开发工具
  3. 源码的拉取
  4. 结构大致介绍

1 依赖的外部环境

  • 安装JDK
  • 安装Git
  • 安装maven

这边我们就不介绍怎么安装这些外部环境了,大家自行从安装这些外部环境哈

2 使用的外部工具

  • 编辑器使用 IntelliJ IDEA (简单好用,快捷键丰富)
  • GitHub Desktop GitHub的客户端软件,用起来真顺手

3 源码的拉取

源码的话我们上GitHub 上面直接拉取dubbo的源码即可, Dubbo 然后我们 Fork出一个属于我们自己的仓库,之后我们就可以在我们GitHub的仓库中看到这个项目了。然后我们将这个项目down到本地进行调试

具体Git操作不详细介绍,下面贴出几个比较好的供大家参考

https://blog.****.net/qq_32040767/article/details/77096761

https://blog.****.net/menggudaoke/article/details/77744541

https://juejin.im/entry/5a5f3b286fb9a01c9064e83b

https://www.cnblogs.com/MrJun/p/3351478.html

这里我们为什么使用Fork 而不是Star,这是因为star的项目你不是Pull Request 到源项目的,而Fork 是可以的。想想一下要是你在看源码的过程中发现了bug。然后提交给他们并审核这是一件多酷的事情

4 Dubbo的大致介绍

下面的这个 Dubbo 官网上最为经典的介绍,一张图就将整个Dubbo 过程大致的介绍的差不多。

什么是Dubbo?

一个分布式服务治理框架

Dubbo的作用是什么?

作用是解决下面的问题

  • 单一应用架构

  • 垂直应用架构

  • 分布式服务架构

  • 流动计算架构

Dubbo 源码分析系列之一环境搭建

节点角色说明

节点 角色说明
Provider 暴露服务的服务提供方
Consumer 调用远程服务的服务消费方
Registry 服务注册与发现的注册中心
Monitor 统计服务的调用次数和调用时间的监控中心
Container 服务运行容器

调用关系说明

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

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

详细介绍下 Registry Provider Consumer

因为这三者是整个Dubbo 调用过程的链路核心

talk is cheap show me the code

光说不行,咱们通过看下项目中的源码来看下dubbo整个结构

dubbo提供了 一个demo项目供大家测试调试 整个demo已经在你down的dubbo的项目中了

打开我们的dubbo项目,我们会看到这样的一个子项目

Dubbo 源码分析系列之一环境搭建

这里就是那个demo子项目 就是我们可以直接运行测试的项目

将整个测试项目展开,我们会看见这几个文件

Dubbo 源码分析系列之一环境搭建

我们先进入consumer项目中的 Consumer类中,运行这个类。在这里是一个main函数,我们直接运行就ok了

之后我们再启动Provider 类。这样的话,我们就启动了一对消费者和生产类。这样的话,最简单的dubbo程序就 启动了。

下面我们根据这个最简单的类来分析下

package org.apache.dubbo.demo;
// 服务对外暴露接口
public interface DemoService { String sayHello(String name); }
package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { /**
* To get ipv6 address to work, add
* System.setProperty("java.net.preferIPv6Addresses", "true");
* before running your application.
*/
public static void main(String[] args) {
//加载配置的xml文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();
//从这个上下文bean中获取类
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
while (true) {
try {
Thread.sleep(1000);
String hello = demoService.sayHello("world"); // call remote method
System.out.println(hello); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}

消费者的测试demo,循环调用方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer"/> <!-- use multicast registry center to discover service -->
<!-- 测试的时候使用组播来进行注册发现 -->
<dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<!--生成一个代理,在这个方法调用远程方法就像调用本地方法一样 -->
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/> </beans>

我们通过配置文件xml来进行服务的发现和启用,还有对注册中心的配置

下面介绍下服务的提供者Provider

package org.apache.dubbo.demo.provider;

import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.rpc.RpcContext; import java.text.SimpleDateFormat;
import java.util.Date; public class DemoServiceImpl implements DemoService { @Override
public String sayHello(String name) {
//对外暴露的服务内部实现类
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
} }
package org.apache.dubbo.demo.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    /** * To get ipv6 address to work, add * System.setProperty("java.net.preferIPv6Addresses", "true"); * before running your application. */
public static void main(String[] args) throws Exception {
//加载xml配置
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
context.start();
System.in.read(); // press any key to exit
}
}
<?xml version="1.0" encoding="UTF-8"?>

    <!-- provider's application name, used for tracing dependency relationship -->
<!-- 提供一个应用名称,用于查找依赖的关系-->
<dubbo:application name="demo-provider"/> <!-- use multicast registry center to export service -->
<!-- 使用组播来进行服务的暴露-->
<dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- use dubbo protocol to export service on port 20880 -->
<!-- 使用dubbo协议对外暴露时的端口-->
<dubbo:protocol name="dubbo" port="20880"/> <!-- service implementation, as same as regular local bean -->
<!-- 业务实现类,对外暴露类的内部实现-->
<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/> <!-- declare the service interface to be exported -->
<!-- 对外暴露服务的接口 -->
<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/> </beans>

博客园展示效果不好,可以移步到GitHub 博客上

http://wsccoder.top/2018/09/14/Dubbo-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90%E7%B3%BB%E5%88%97%E4%B9%8B%E4%B8%80%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/