这边咱们就不介绍怎么安装这些外部环境了,你们自行从安装这些外部环境哈html
源码的话咱们上GitHub 上面直接拉取dubbo的源码便可, Dubbo 而后咱们 Fork出一个属于咱们本身的仓库,以后咱们就能够在咱们GitHub的仓库中看到这个项目了。而后咱们将这个项目down到本地进行调试java
具体Git操做不详细介绍,下面贴出几个比较好的供你们参考git
https://blog.csdn.net/qq_32040767/article/details/77096761github
https://blog.csdn.net/menggudaoke/article/details/77744541算法
https://juejin.im/entry/5a5f3b286fb9a01c9064e83bspring
https://www.cnblogs.com/MrJun/p/3351478.htmlapache
这里咱们为何使用Fork 而不是Star,这是由于star的项目你不是Pull Request 到源项目的,而Fork 是能够的。想一想一下要是你在看源码的过程当中发现了bug。而后提交给他们并审核这是一件多酷的事情架构
下面的这个 Dubbo 官网上最为经典的介绍,一张图就将整个Dubbo 过程大体的介绍的差很少。app
什么是Dubbo?负载均衡
一个分布式服务治理框架
Dubbo的做用是什么?
做用是解决下面的问题
单一应用架构
垂直应用架构
分布式服务架构
流动计算架构
节点 | 角色说明 |
---|---|
Provider |
暴露服务的服务提供方 |
Consumer |
调用远程服务的服务消费方 |
Registry |
服务注册与发现的注册中心 |
Monitor |
统计服务的调用次数和调用时间的监控中心 |
Container |
服务运行容器 |
服务容器负责启动,加载,运行服务提供者。
由于这三者是整个Dubbo 调用过程的链路核心
talk is cheap show me the code
光说不行,我们经过看下项目中的源码来看下dubbo整个结构
dubbo提供了 一个demo项目供你们测试调试 整个demo已经在你down的dubbo的项目中了
打开咱们的dubbo项目,咱们会看到这样的一个子项目
这里就是那个demo子项目 就是咱们能够直接运行测试的项目
将整个测试项目展开,咱们会看见这几个文件
咱们先进入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>