【教程】dubbo+zookeeper项目Demo

超、凢脫俗 2022-04-23 05:48 235阅读 0赞

1.环境

jdk、maven等工具已安装。下面的例子基于eclipse,绿色版的zookeeper,win10系统。
工程GIT: https://github.com/ydfind/javademo/tree/master/maven/dubbo-demo

2.运行zookeeper

运行安装目录下“./bin/zkServer.cmd”,以启动zookper。
“./conf/zoo.cfg”配置文件可以配置一些属性,如下:
dataDir=D:\ProgramData\zookeeper
dataLogDir=D:\ProgramData\zookeeper\log
在这里插入图片描述

3.创建maven工程“dubbo-server”

目录结构和最终jar包,如下:
在这里插入图片描述在这里插入图片描述

1)ServerMain.java

  1. package com.ydfind;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import java.io.IOException;
  6. /**
  7. *
  8. * @author Yudi
  9. *
  10. */
  11. public class ServerMain {
  12. private static Logger logger = LoggerFactory.getLogger(ServerMain.class);
  13. public static void main(String[] args) throws IOException {
  14. logger.info("--------------------start------------------------");
  15. try {
  16. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "dubbo.xml" });
  17. context.start();
  18. logger.info("任意按键退出");
  19. System.in.read();
  20. context.close();
  21. logger.info("服务器正常关闭-----------------------------");
  22. }catch(Exception e) {
  23. logger.info("服务器报错了-------------------");
  24. e.printStackTrace();
  25. }
  26. }
  27. }

2)ElasticService.java

  1. package com.ydfind.service;
  2. /**
  3. *
  4. * @author Yudi
  5. *
  6. */
  7. public interface ElasticService {
  8. String helloDubbo(String msg);
  9. }

3)ElasticServiceImpl.java

  1. package com.ydfind.service.impl;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import com.ydfind.service.ElasticService;
  5. /**
  6. *
  7. * @author Yudi
  8. *
  9. */
  10. public class ElasticServiceImpl implements ElasticService {
  11. private static final Logger logger = LoggerFactory.getLogger(ElasticServiceImpl.class);
  12. public String helloDubbo(String msg) {
  13. logger.info("服务器收到:" + msg);
  14. return "hi!" + msg;
  15. }
  16. }

4)Dubbo.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  9. <dubbo:application name="dubbo-server" owner="ydfind" />
  10. <!-- zookeeper注册中心 -->
  11. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  12. <dubbo:protocol contextpath="dubbo" port="20881" />
  13. <dubbo:monitor protocol="registry"/>
  14. <!-- 服务具体实现 -->
  15. <bean id="elasticService" class="com.ydfind.service.impl.ElasticServiceImpl" />
  16. <!-- 向注册中心注册暴漏服务地址,注册服务 -->
  17. <dubbo:service interface="com.ydfind.service.ElasticService" ref="elasticService" protocol="dubbo" timeout="50000"/>
  18. </beans>

5)log4j.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE log4j:configuration SYSTEM "./log4j.dtd">
  3. <log4j:configuration>
  4. <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
  5. <layout class="org.apache.log4j.PatternLayout">
  6. <param name="ConversionPattern" value="[%-d{yyyy-MM-dd HH:mm:ss.SSS}][%p][%t] %X{operationName} %X{userId} - %m%n" />
  7. </layout>
  8. </appender>
  9. <root>
  10. <level value="INFO" />
  11. <appender-ref ref="consoleAppender" />
  12. </root>
  13. </log4j:configuration>

6)pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. 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.ydfind</groupId>
  5. <artifactId>dubbo-server</artifactId>
  6. <version>0.0.1-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>commons-logging</groupId>
  35. <artifactId>commons-logging</artifactId>
  36. <version>1.1.1</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>log4j</groupId>
  40. <artifactId>log4j</artifactId>
  41. <version>1.2.16</version>
  42. <exclusions>
  43. <exclusion>
  44. <groupId>com.sun.jdmk</groupId>
  45. <artifactId>jmxtools</artifactId>
  46. </exclusion>
  47. <exclusion>
  48. <groupId>com.sun.jmx</groupId>
  49. <artifactId>jmxri</artifactId>
  50. </exclusion>
  51. <exclusion>
  52. <artifactId>jms</artifactId>
  53. <groupId>javax.jms</groupId>
  54. </exclusion>
  55. <exclusion>
  56. <artifactId>mail</artifactId>
  57. <groupId>javax.mail</groupId>
  58. </exclusion>
  59. </exclusions>
  60. </dependency>
  61. <!-- log end -->
  62. <!-- other begin -->
  63. <dependency>
  64. <groupId>org.jboss.netty</groupId>
  65. <artifactId>netty</artifactId>
  66. <version>3.2.0.Final</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>com.101tec</groupId>
  70. <artifactId>zkclient</artifactId>
  71. <version>0.8</version>
  72. </dependency>
  73. <!-- other end -->
  74. </dependencies>
  75. </project>

4.maven的jar工程“dubbo-client”

注意
需要把dubbo-server的包加入

在这里插入图片描述在这里插入图片描述

1)ClientMain.java

  1. package com.ydfind;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.ydfind.service.ElasticService;
  6. /**
  7. *
  8. * @author Yudi
  9. *
  10. */
  11. public class ClientMain {
  12. private static Logger logger = LoggerFactory.getLogger(ClientMain.class);
  13. public static void main(String[] args) {
  14. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dubbo.xml" });
  15. try {
  16. context.start();
  17. ElasticService service = (ElasticService) context.getBean("elasticService");
  18. logger.info(service.helloDubbo("world"));
  19. context.close();
  20. logger.info("客户端正确结束了");
  21. } catch (Exception e) {
  22. logger.info("客户端 报错了");
  23. e.printStackTrace();
  24. }
  25. }
  26. }

2)dubbo.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  9. <dubbo:application name="consumer-of-dubbo-demo" />
  10. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  11. <!-- 向注册中心订阅服务 -->
  12. <dubbo:reference id="elasticService" interface="com.ydfind.service.ElasticService" />
  13. </beans>

3)log4j.xml

同上

4)pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. 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.ydfind</groupId>
  5. <artifactId>dubbo-client</artifactId>
  6. <version>0.0.1-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.16</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.ydfind</groupId>
  65. <artifactId>dubbo-server</artifactId>
  66. <version>0.0.1-SNAPSHOT</version>
  67. </dependency>
  68. <!-- other end -->
  69. </dependencies>
  70. </project>

5.运行测试

  • 1)运行zookeeper;
  • 2)运行dubbo-server的ServerMain.java;
  • 3)运行dubbo-client的ClientMain.java;
  • 4)将输出:
    在这里插入图片描述在这里插入图片描述
    注意:zookeeper和dubbo-server不能关闭,否则dubbo-client无法运行成功;

发表评论

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

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

相关阅读

    相关 开源商城项目前端demo

    大家都知道,我每天最重要的事就是帮大家在GitHub上挖掘出最受程序员欢迎的开源项目,这不,今天GitHub热榜上又有一个项目成功引起了小编的注意——mall-swarm 之

    相关 Vue项目demo汇总

    vue慢慢的成为了前端最受欢迎的框架之一,在很多项目之中开发都能用得到,如今也已经发展到3.0了,可能是因为这个框架可以提高工作效率,因此受到大家的追捧,在之前的文章里面也说过