dubbo简单入门(helloworld例子)

快来打我* 2022-06-01 02:56 341阅读 0赞

Dubbo现在支持的有三种方式:
1.multicast;
2.zookeeper;
3.redis
下面的Demo使用的是multicast方式。

提供者项目结构:
这里写图片描述
消费者项目结构:
这里写图片描述

服务端:

pom.xml配置

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.ming</groupId>
  4. <artifactId>dubboserver</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <dependencies>
  7. <dependency>
  8. <groupId>junit</groupId>
  9. <artifactId>junit</artifactId>
  10. <version>4.11</version>
  11. <scope>test</scope>
  12. </dependency>
  13. <dependency>
  14. <groupId>commons-logging</groupId>
  15. <artifactId>commons-logging</artifactId>
  16. <version>1.1.1</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.alibaba</groupId>
  20. <artifactId>dubbo</artifactId>
  21. <version>2.5.3</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.javassist</groupId>
  25. <artifactId>javassist</artifactId>
  26. <version>3.15.0-GA</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>log4j</groupId>
  30. <artifactId>log4j</artifactId>
  31. <version>1.2.15</version>
  32. <exclusions>
  33. <exclusion>
  34. <groupId>com.sun.jdmk</groupId>
  35. <artifactId>jmxtools</artifactId>
  36. </exclusion>
  37. <exclusion>
  38. <groupId>com.sun.jmx</groupId>
  39. <artifactId>jmxri</artifactId>
  40. </exclusion>
  41. <exclusion>
  42. <artifactId>jms</artifactId>
  43. <groupId>javax.jms</groupId>
  44. </exclusion>
  45. <exclusion>
  46. <artifactId>mail</artifactId>
  47. <groupId>javax.mail</groupId>
  48. </exclusion>
  49. </exclusions>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-context</artifactId>
  54. <version>4.1.6.RELEASE</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.slf4j</groupId>
  58. <artifactId>slf4j-api</artifactId>
  59. <version>1.7.5</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.slf4j</groupId>
  63. <artifactId>slf4j-log4j12</artifactId>
  64. <version>1.6.1</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.apache.zookeeper</groupId>
  68. <artifactId>zookeeper</artifactId>
  69. <version>3.3.6</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>com.github.adyliu</groupId>
  73. <artifactId>zkclient</artifactId>
  74. <version>2.0</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.jboss.netty</groupId>
  78. <artifactId>netty</artifactId>
  79. <version>3.2.0.Final</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>com.101tec</groupId>
  83. <artifactId>zkclient</artifactId>
  84. <version>0.4</version>
  85. </dependency>
  86. </dependencies>
  87. </project>
  88. 1
  89. 2
  90. 3
  91. 4
  92. 5
  93. 6
  94. 7
  95. 8
  96. 9
  97. 10
  98. 11
  99. 12
  100. 13
  101. 14
  102. 15
  103. 16
  104. 17
  105. 18
  106. 19
  107. 20
  108. 21
  109. 22
  110. 23
  111. 24
  112. 25
  113. 26
  114. 27
  115. 28
  116. 29
  117. 30
  118. 31
  119. 32
  120. 33
  121. 34
  122. 35
  123. 36
  124. 37
  125. 38
  126. 39
  127. 40
  128. 41
  129. 42
  130. 43
  131. 44
  132. 45
  133. 46
  134. 47
  135. 48
  136. 49
  137. 50
  138. 51
  139. 52
  140. 53
  141. 54
  142. 55
  143. 56
  144. 57
  145. 58
  146. 59
  147. 60
  148. 61
  149. 62
  150. 63
  151. 64
  152. 65
  153. 66
  154. 67
  155. 68
  156. 69
  157. 70
  158. 71
  159. 72
  160. 73
  161. 74
  162. 75
  163. 76
  164. 77
  165. 78
  166. 79
  167. 80
  168. 81
  169. 82
  170. 83
  171. 84
  172. 85
  173. 86
  174. 87
  175. 88
  176. 89

applicationProvider.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
  3. <dubbo:application name="hello-world" /><!-- 注册地址 -->
  4. <dubbo:registry address="multicast://224.5.6.7:1234"/>
  5. <!-- <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/> -->
  6. <dubbo:protocol name="dubbo" port="20880" />
  7. <bean id="demoService" class="com.ming.dubboserver.HelloWorldImpl" />
  8. <dubbo:service interface="com.ming.dubboserver.HelloWorld" ref="demoService" executes="10" />
  9. </beans>
  10. 1
  11. 2
  12. 3
  13. 4
  14. 5
  15. 6
  16. 7
  17. 8
  18. 9
  19. 10
  20. 11
  21. 12
  22. 13

helloworld接口:

  1. package com.ming.dubboserver;
  2. public interface HelloWorld {
  3. public String hello(String name);
  4. }
  5. 1
  6. 2
  7. 3
  8. 4
  9. 5
  10. 6

helloworld实现类

  1. package com.ming.dubboserver;
  2. public class HelloWorldImpl implements HelloWorld {
  3. public String hello(String name) {
  4. name = name + "小明测试";
  5. return name;
  6. }
  7. }
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5
  13. 6
  14. 7
  15. 8
  16. 9
  17. 10
  18. 11
  19. 12

服务启动类:

  1. package com.ming.dubboserver;
  2. import java.io.IOException;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class DubboProviderMain {
  5. public static void main(String[] args) throws IOException {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  7. new String[] { "applicationProvider.xml" });
  8. context.start();
  9. System.out.println("Press any key to exit.");
  10. System.in.read();
  11. }
  12. }
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6
  19. 7
  20. 8
  21. 9
  22. 10
  23. 11
  24. 12
  25. 13
  26. 14
  27. 15
  28. 16
  29. 17

客户端:
pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.ming</groupId>
  4. <artifactId>dubboconsumer</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <name>dubboconsumer</name>
  7. <dependencies>
  8. <dependency>
  9. <groupId>junit</groupId>
  10. <artifactId>junit</artifactId>
  11. <version>4.11</version>
  12. <scope>test</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>commons-logging</groupId>
  16. <artifactId>commons-logging</artifactId>
  17. <version>1.1.1</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>com.alibaba</groupId>
  21. <artifactId>dubbo</artifactId>
  22. <version>2.5.3</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.javassist</groupId>
  26. <artifactId>javassist</artifactId>
  27. <version>3.15.0-GA</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>log4j</groupId>
  31. <artifactId>log4j</artifactId>
  32. <version>1.2.15</version>
  33. <exclusions>
  34. <exclusion>
  35. <groupId>com.sun.jdmk</groupId>
  36. <artifactId>jmxtools</artifactId>
  37. </exclusion>
  38. <exclusion>
  39. <groupId>com.sun.jmx</groupId>
  40. <artifactId>jmxri</artifactId>
  41. </exclusion>
  42. <exclusion>
  43. <artifactId>jms</artifactId>
  44. <groupId>javax.jms</groupId>
  45. </exclusion>
  46. <exclusion>
  47. <artifactId>mail</artifactId>
  48. <groupId>javax.mail</groupId>
  49. </exclusion>
  50. </exclusions>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework</groupId>
  54. <artifactId>spring-context</artifactId>
  55. <version>4.1.6.RELEASE</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.slf4j</groupId>
  59. <artifactId>slf4j-api</artifactId>
  60. <version>1.7.5</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.slf4j</groupId>
  64. <artifactId>slf4j-log4j12</artifactId>
  65. <version>1.6.1</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>com.github.adyliu</groupId>
  69. <artifactId>zkclient</artifactId>
  70. <version>2.0</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>org.apache.zookeeper</groupId>
  74. <artifactId>zookeeper</artifactId>
  75. <version>3.3.6</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>com.ming</groupId>
  79. <artifactId>dubboserver</artifactId>
  80. <version>0.0.1-SNAPSHOT</version>
  81. </dependency>
  82. </dependencies>
  83. </project>
  84. 1
  85. 2
  86. 3
  87. 4
  88. 5
  89. 6
  90. 7
  91. 8
  92. 9
  93. 10
  94. 11
  95. 12
  96. 13
  97. 14
  98. 15
  99. 16
  100. 17
  101. 18
  102. 19
  103. 20
  104. 21
  105. 22
  106. 23
  107. 24
  108. 25
  109. 26
  110. 27
  111. 28
  112. 29
  113. 30
  114. 31
  115. 32
  116. 33
  117. 34
  118. 35
  119. 36
  120. 37
  121. 38
  122. 39
  123. 40
  124. 41
  125. 42
  126. 43
  127. 44
  128. 45
  129. 46
  130. 47
  131. 48
  132. 49
  133. 50
  134. 51
  135. 52
  136. 53
  137. 54
  138. 55
  139. 56
  140. 57
  141. 58
  142. 59
  143. 60
  144. 61
  145. 62
  146. 63
  147. 64
  148. 65
  149. 66
  150. 67
  151. 68
  152. 69
  153. 70
  154. 71
  155. 72
  156. 73
  157. 74
  158. 75
  159. 76
  160. 77
  161. 78
  162. 79
  163. 80
  164. 81
  165. 82
  166. 83
  167. 84
  168. 85

applicationConsumer.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  3. <dubbo:application name="consumer-of-helloworld-app" />
  4. <dubbo:registry address="multicast://224.5.6.7:1234" />
  5. <dubbo:reference id="demoService" interface="com.ming.dubboserver.HelloWorld" />
  6. </beans>
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 7
  14. 8
  15. 9

测试类:

  1. package com.ming.dubboconsumer;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import com.ming.dubboserver.HelloWorld;
  4. public class ConsumerThd implements Runnable {
  5. public void run() {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  7. new String[] { "applicationConsumer.xml" });
  8. context.start();
  9. context.getBean("demoService");
  10. HelloWorld helloWorld = (HelloWorld) context.getBean("demoService");
  11. String hello = helloWorld.hello("小明");
  12. System.out.println(hello);
  13. System.out.println("执行完毕");
  14. }
  15. public static void main(String[] args) {
  16. new Thread(new ConsumerThd()).start();
  17. }
  18. }
  19. 1
  20. 2
  21. 3
  22. 4
  23. 5
  24. 6
  25. 7
  26. 8
  27. 9
  28. 10
  29. 11
  30. 12
  31. 13
  32. 14
  33. 15
  34. 16
  35. 17
  36. 18
  37. 19
  38. 20
  39. 21
  40. 22
  41. 23

还有一种方式是将Zookeeper作为注册中心,需要下载并安装zookeeper。将配置文件中的注册中心修改为如下即可:

其他代码不用修改,推荐使用zookeeper作为注册中心。如此可以通过Dubbo+注册中心实现分布式服务。服务消费者不用知道服务的具体位置,只要知道注册中心位置即可,消费者只要能消费,不用管消费的是哪的服务,服务提供方与服务消费方解耦。
使用zookeeper注册中心,与dubbo结合,可以查看服务提供者和消费者的信息,将dubbo管理的war包部署到tomcat上,查看服务提供者

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读

    相关 dubbo HelloWorld

    Apache Dubbo  是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现 !