Dubbo入门教程

桃扇骨 2024-04-18 09:49 175阅读 0赞

服务端(dubbo-server)

1. pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.lwb</groupId>
  5. <artifactId>dubbo-server</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <properties>
  9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  10. </properties>
  11. <dependencies>
  12. <!-- spring begin -->
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-context</artifactId>
  16. <version>4.1.6.RELEASE</version>
  17. </dependency>
  18. <!-- spring end -->
  19. <!-- dubbo begin -->
  20. <dependency>
  21. <groupId>com.alibaba</groupId>
  22. <artifactId>dubbo</artifactId>
  23. <version>2.5.3</version>
  24. </dependency>
  25. <!-- dubbo end -->
  26. <!-- 注册中心zookeeper begin -->
  27. <dependency>
  28. <groupId>org.apache.zookeeper</groupId>
  29. <artifactId>zookeeper</artifactId>
  30. <version>3.3.6</version>
  31. </dependency>
  32. <!-- 注册中心zookeeper end -->
  33. <!-- log begin -->
  34. <dependency>
  35. <groupId>commons-logging</groupId>
  36. <artifactId>commons-logging</artifactId>
  37. <version>1.1.1</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>log4j</groupId>
  41. <artifactId>log4j</artifactId>
  42. <version>1.2.15</version>
  43. <exclusions>
  44. <exclusion>
  45. <groupId>com.sun.jdmk</groupId>
  46. <artifactId>jmxtools</artifactId>
  47. </exclusion>
  48. <exclusion>
  49. <groupId>com.sun.jmx</groupId>
  50. <artifactId>jmxri</artifactId>
  51. </exclusion>
  52. <exclusion>
  53. <artifactId>jms</artifactId>
  54. <groupId>javax.jms</groupId>
  55. </exclusion>
  56. <exclusion>
  57. <artifactId>mail</artifactId>
  58. <groupId>javax.mail</groupId>
  59. </exclusion>
  60. </exclusions>
  61. </dependency>
  62. <!-- log end -->
  63. <!-- other begin -->
  64. <dependency>
  65. <groupId>org.jboss.netty</groupId>
  66. <artifactId>netty</artifactId>
  67. <version>3.2.0.Final</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>com.101tec</groupId>
  71. <artifactId>zkclient</artifactId>
  72. <version>0.8</version>
  73. </dependency>
  74. <!-- other end -->
  75. </dependencies>
  76. </project>

2. dubbo.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="dubbo-server" owner="lwb" />
  4. <!-- zookeeper注册中心 -->
  5. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  6. <dubbo:protocol contextpath="dubbo" port="20881" />
  7. <dubbo:monitor protocol="registry"/>
  8. <!-- 服务具体实现 -->
  9. <bean id="elasticService" class="com.lwb.service.impl.ElasticServiceImpl" />
  10. <!-- 向注册中心注册暴漏服务地址,注册服务 -->
  11. <dubbo:service interface="com.lwb.service.ElasticService" ref="elasticService" protocol="dubbo" timeout="50000"/>

3. 代码

  1. package com.lwb.service;
  2. /** * @author liuweibo * @date 2018/5/9 */
  3. public interface ElasticService {
  4. String helloDubbo(String msg);
  5. }
  6. package com.lwb.service.impl;
  7. import com.lwb.service.ElasticService;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.stereotype.Service;
  11. /** * @author liuweibo * @date 2018/5/9 */
  12. public class ElasticServiceImpl implements ElasticService {
  13. private static final Logger LOGGER = LoggerFactory.getLogger(ElasticServiceImpl.class);
  14. public String helloDubbo(String msg) {
  15. System.out.println(msg);
  16. return "hi!" + msg;
  17. }
  18. }

4. 项目结构

在这里插入图片描述

5. 启动类(Application.java)

这里就直接用main方法启动了

  1. package com.lwb;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import java.io.IOException;
  4. /** * @author liuweibo * @date 2018/5/9 */
  5. public class Application {
  6. public static void main(String[] args) throws IOException {
  7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "dubbo.xml" });
  8. context.start();
  9. System.out.println("任意按键退出");
  10. System.in.read();
  11. context.close();
  12. }
  13. }

客户端(dubbo-client)

1. pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.lwb</groupId>
  5. <artifactId>dubbo-client</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. </properties>
  10. <dependencies>
  11. <!-- spring begin -->
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-context</artifactId>
  15. <version>4.1.6.RELEASE</version>
  16. </dependency>
  17. <!-- spring end -->
  18. <!-- dubbo begin -->
  19. <dependency>
  20. <groupId>com.alibaba</groupId>
  21. <artifactId>dubbo</artifactId>
  22. <version>2.5.3</version>
  23. </dependency>
  24. <!-- dubbo end -->
  25. <!-- 注册中心zookeeper begin -->
  26. <dependency>
  27. <groupId>org.apache.zookeeper</groupId>
  28. <artifactId>zookeeper</artifactId>
  29. <version>3.3.6</version>
  30. </dependency>
  31. <!-- 注册中心zookeeper end -->
  32. <!-- log begin -->
  33. <dependency>
  34. <groupId>log4j</groupId>
  35. <artifactId>log4j</artifactId>
  36. <version>1.2.15</version>
  37. <exclusions>
  38. <exclusion>
  39. <groupId>com.sun.jdmk</groupId>
  40. <artifactId>jmxtools</artifactId>
  41. </exclusion>
  42. <exclusion>
  43. <groupId>com.sun.jmx</groupId>
  44. <artifactId>jmxri</artifactId>
  45. </exclusion>
  46. <exclusion>
  47. <artifactId>jms</artifactId>
  48. <groupId>javax.jms</groupId>
  49. </exclusion>
  50. <exclusion>
  51. <artifactId>mail</artifactId>
  52. <groupId>javax.mail</groupId>
  53. </exclusion>
  54. </exclusions>
  55. </dependency>
  56. <!-- log end -->
  57. <!-- other begin -->
  58. <dependency>
  59. <groupId>com.101tec</groupId>
  60. <artifactId>zkclient</artifactId>
  61. <version>0.8</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>com.lwb</groupId>
  65. <artifactId>dubbo-server</artifactId>
  66. <version>1.0-SNAPSHOT</version>
  67. </dependency>
  68. <!-- other end -->
  69. </dependencies>
  70. </project>

2. dubbo.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-dubbo-demo" />
  4. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  5. <!-- 向注册中心订阅服务 -->
  6. <dubbo:reference id="elasticService" interface="com.lwb.service.ElasticService" />
  7. </beans>

3. 服务调用(这里为了简单,就在main方法里面处理了哈,Application.java)

  1. package com.lwb;
  2. import com.lwb.service.ElasticService;
  3. import com.lwb.service.impl.ElasticServiceImpl;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. /** * @author liuweibo * @date 2018/5/9 */
  6. public class Application {
  7. public static void main(String[] args) {
  8. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  9. new String[] { "dubbo.xml" });
  10. context.start();
  11. ElasticService service = (ElasticService) context.getBean("elasticService");
  12. System.out.println(service.helloDubbo("world"));
  13. context.close();
  14. }
  15. }

4. 项目结构

在这里插入图片描述

发表评论

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

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

相关阅读

    相关 Dubbo入门

    Dubbo开始于电商项目,是用Java语言写的, 首先介绍一下电商项目的古老历史: 从刚开始的ORM框架,全称是Object Relational Mapping中文那,全

    相关 Dubbo入门

    Dubbo入门 1. 服务容器负责启动,加载,运行服务提供者,这个图上没标识出来,服务端启动就是0. 2. 服务提供者在启动时,向注册中心注册自己提供的服务。 3. 服

    相关 dubbo入门教程-从零搭建dubbo服务

    【原创 转载请注明出处】 本文是学习了dubbo之后自己手动写的,比较通俗,很多都是自己学习之后的理解,写的过程中没有参考任何文章。 另外dubbo也有官方文档,但是比较官