dubbo 源码分析汇总

3.1 Dubbo 内核解剖

dubbo 源码分析 1 之 架构原理探索
dubbo 源码分析 2 之 内核 SPI 实现
dubbo 源码分析 3 之 动态编译
dubbo 源码分析 4 之 与 Spring 集成
dubbo 源码分析 5 之 SPI 分析

3.2 Dubbo 服务发布原理解剖

dubbo 源码分析 6 之 服务暴露概述
dubbo 源码分析 7 之 服务本地暴露
dubbo 源码分析 8 之 服务远程暴露 (上)
dubbo 源码分析 9 之 服务远程暴露 (中)
dubbo 源码分析 10 之 服务远程暴露 (下)

3.3 Dubbo 服务引用原理解剖

dubbo 源码分析 11 之 服务引用
dubbo 源码分析 12 之 Listener & Filter

3.4 Dubbo 集群容错的设计解剖

dubbo 源码分析 13 之 集群容错 Invoke
dubbo 源码分析 14 – 集群容错之 Directory
dubbo 源码分析 15 – 集群容错之 Route
dubbo 源码分析 16 – 集群容错之 LoadBalance
dubbo 源码分析 17 – 集群容错总结

3.5 Dubbo 服务治理设计解剖

dubbo 源码分析 18 – 服务监控
dubbo 源码分析 19 – 服务治理

3.6 Dubbo 网络通信架构解剖

dubbo 源码分析 20 – 远程调用概述
dubbo 源码分析 21 – 远程通信 netty
dubbo 源码分析 22 – consumer 发送与接收原理
dubbo 源码分析 23 – provider 接收与发送原理
dubbo 源码分析 24 – 调用核心 Invoke

3.7 Dubbo 网络通信编解码解剖

dubbo 源码分析 25 – 序列化与反序列化
dubbo 源码分析 26 – 网络编解码 参考文章:

  1. public class ConsumerApi {
  2. public static void main(String[] args) {
  3. ReferenceConfig<UserService> reference = new ReferenceConfig<>();
  4. reference.setApplication(new ApplicationConfig("api-consumer"));
  5. reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
  6. reference.setInterface(UserService.class);
  7. UserService orderService = reference.get();
  8. String username = orderService.getUsername();
  9. System.out.println("result: " + username);
  10. }
  11. }
  12. public class ReferenceConfig<T> extends AbstractReferenceConfig {
  13. public synchronized T get() {
  14. if (destroyed) {
  15. throw new IllegalStateException("Already destroyed!");
  16. }
  17. if (ref == null) {
  18. init();
  19. }
  20. return ref;
  21. }
  22. }

image.png

  1. public class ReferenceConfig<T> extends AbstractReferenceConfig {
  2. ...
  3. private T createProxy(Map<String, String> map) {
  4. URL tmpUrl = new URL("temp", "localhost", 0, map);
  5. final boolean isJvmRefer;
  6. if (isInjvm() == null) {
  7. if (url != null && url.length() > 0) { // if a url is specified, don't do local reference
  8. isJvmRefer = false;
  9. } else if (InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl)) {
  10. // by default, reference local service if there is
  11. isJvmRefer = true;
  12. } else {
  13. isJvmRefer = false;
  14. }
  15. } else {
  16. isJvmRefer = isInjvm().booleanValue();
  17. }
  18. if (isJvmRefer) {
  19. URL url = new URL(Constants.LOCAL_PROTOCOL, NetUtils.LOCALHOST, 0, interfaceClass.getName()).addParameters(map);
  20. invoker = refprotocol.refer(interfaceClass, url);
  21. if (logger.isInfoEnabled()) {
  22. logger.info("Using injvm service " + interfaceClass.getName());
  23. }
  24. } else {
  25. if (url != null && url.length() > 0) { // user specified URL, could be peer-to-peer address, or register center's address.
  26. String[] us = Constants.SEMICOLON_SPLIT_PATTERN.split(url);
  27. if (us != null && us.length > 0) {
  28. for (String u : us) {
  29. URL url = URL.valueOf(u);
  30. if (url.getPath() == null || url.getPath().length() == 0) {
  31. url = url.setPath(interfaceName);
  32. }
  33. if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
  34. urls.add(url.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
  35. } else {
  36. urls.add(ClusterUtils.mergeUrl(url, map));
  37. }
  38. }
  39. }
  40. } else { // assemble URL from register center's configuration
  41. List<URL> us = loadRegistries(false);
  42. if (us != null && !us.isEmpty()) {
  43. for (URL u : us) {
  44. URL monitorUrl = loadMonitor(u);
  45. if (monitorUrl != null) {
  46. map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
  47. }
  48. urls.add(u.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
  49. }
  50. }
  51. if (urls.isEmpty()) {
  52. throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config <dubbo:registry address=\"...\" /> to your spring config.");
  53. }
  54. }
  55. if (urls.size() == 1) {
  56. invoker = refprotocol.refer(interfaceClass, urls.get(0));
  57. } else {
  58. List<Invoker<?>> invokers = new ArrayList<Invoker<?>>();
  59. URL registryURL = null;
  60. for (URL url : urls) {
  61. invokers.add(refprotocol.refer(interfaceClass, url));
  62. if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
  63. registryURL = url; // use last registry url
  64. }
  65. }
  66. if (registryURL != null) { // registry url is available
  67. // use AvailableCluster only when register's cluster is available
  68. URL u = registryURL.addParameter(Constants.CLUSTER_KEY, AvailableCluster.NAME);
  69. invoker = cluster.join(new StaticDirectory(u, invokers));
  70. } else { // not a registry url
  71. invoker = cluster.join(new StaticDirectory(invokers));
  72. }
  73. }
  74. }
  75. Boolean c = check;
  76. if (c == null && consumer != null) {
  77. c = consumer.isCheck();
  78. }
  79. if (c == null) {
  80. c = true; // default true
  81. }
  82. if (c && !invoker.isAvailable()) {
  83. throw new IllegalStateException("Failed to check the status of the service " + interfaceName + ". No provider available for the service " + (group == null ? "" : group + "/") + interfaceName + (version == null ? "" : ":" + version) + " from the url " + invoker.getUrl() + " to the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion());
  84. }
  85. if (logger.isInfoEnabled()) {
  86. logger.info("Refer dubbo service " + interfaceClass.getName() + " from url " + invoker.getUrl());
  87. }
  88. // create service proxy
  89. return (T) proxyFactory.getProxy(invoker);
  90. }
  91. }