dubbo入门案例搭建

深藏阁楼爱情的钟 2022-06-09 10:56 353阅读 0赞

spring与dubbo、Zookeeper案例

一、安装zookeeper

首先到官网下载zookeeper,版本自选点击打开链接https://zookeeper.apache.org/

1.下载完之后开始安装。这里在windows安装介绍(ps电脑性能不好,就在此演示,用法一样):

先搭建一个单机模式的的ZooKeeper环境。

2.进入到CONF目录下,将里面的.cfg文件重命名为zoo.cfg.

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=D:\\zookeeper-3.4.6\\data
dataLogDir=D:\\zookeeper-3.4.6\\log
#dataDir=/tmp/zookeeper

从上面代码可以看到添加了两行。在本机里。zookeeper放在D盘里,然后就添加了dataDir及dataLogDir两个变量。与此同时在zookeeper文件目录下新建data及log两个文件夹,如果不创建,后面运行脚本是地会报错。

完成后,进入bin目录,运行zkServer.cmd脚本,让后就可以在单机上将zookeeper跑起来了。

剩下来的事情就是去配置DUBBO的XML文件了,通过IP地址的设置,提供本地的服务。

二、实现案例代码

1、消费者服务代码

  1. public class IndexController {
  2. public static void main(String[] args) throws Exception {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{
  4. "dubbo-consumer.xml"});
  5. context.start();
  6. TestRegistryService testRegistryService = (TestRegistryService) context.getBean("testRegistryService"); // 获取远程服务代理 String hello = testRegistryService.hello("world"); // 执行远程方法 System.out.println(hello); // 显示调用结果 System.in.read();
  7. }
  8. }

2、消费者服务注册中心

  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管理页面比较清晰是哪个应用暴露出来的 --> <dubbo:application name="dubbo_consumer"></dubbo:application>
  4. <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>
  5. <dubbo:reference interface="com.stocsis.gavin.registry.service.TestRegistryService" id="testRegistryService"/>
  6. </beans>

3.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. <parent>
  4. <groupId>com.stocsis.gavin</groupId>
  5. <artifactId>microservice</artifactId>
  6. <version>1.0</version>
  7. </parent>
  8. <packaging>jar</packaging>
  9. <dependencies>
  10. <dependency>
  11. <groupId>com.stocsis.gavin</groupId>
  12. <artifactId>provider</artifactId>
  13. <version>1.0</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.11</version>
  19. <scope>test</scope>
  20. </dependency>
  21. </dependencies>
  22. <modelVersion>4.0.0</modelVersion>
  23. <artifactId>consumer</artifactId>
  24. </project>

4、提供者服务代码

(1)、接口(这应该单独为一个服务)

  1. public interface TestRegistryService {
  2. public String hello(String name);
  3. }

(2)实现

  1. public class TestRegistryServiceImpl implements TestRegistryService {
  2. public String hello(String name) {
  3. return "hello "+name;
  4. }
  5. }

(3)服务启动类

  1. public class DemoProvider {
  2. public static void main(String[] args)throws Exception {
  3. // new Thread(new Runnable() { // public void run() { // try { // Thread.sleep(20000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // ProtocolConfig.destroyAll(); // } // }).start(); ClassPathXmlApplicationContext content = new ClassPathXmlApplicationContext(new String[]{
  4. "dubbo-provider.xml"});
  5. content.start();
  6. System.in.read();
  7. // com.alibaba.dubbo.container.Main.main(args); }
  8. }

(5)服务注册中心

  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管理页面比较清晰是哪个应用暴露出来的 --> <dubbo:application name="dubbo_provider"></dubbo:application>
  4. <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  5. <!-- 要暴露的服务接口 --> <dubbo:service interface="com.stocsis.gavin.registry.service.TestRegistryService" ref="testRegistryService"/>
  6. <bean id="testRegistryService" class="com.stocsis.gavin.registry.serviceImpl.TestRegistryServiceImpl"></bean>
  7. </beans>

(6)日志

  1. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
  2. <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
  3. <layout class="org.apache.log4j.PatternLayout">
  4. <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
  5. </layout>
  6. </appender>
  7. <root>
  8. <level value="INFO" />
  9. <appender-ref ref="CONSOLE" />
  10. </root>
  11. </log4j:configuration>

(7)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. <parent>
  4. <groupId>com.stocsis.gavin</groupId>
  5. <artifactId>microservice</artifactId>
  6. <version>1.0</version>
  7. </parent>
  8. <packaging>war</packaging>
  9. <modelVersion>4.0.0</modelVersion>
  10. <artifactId>provider</artifactId>
  11. </project>

3、项目总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.stocsis.gavin</groupId>
  5. <artifactId>microservice</artifactId>
  6. <version>1.0</version>
  7. <packaging>pom</packaging>
  8. <dependencies>
  9. <dependency>
  10. <groupId>junit</groupId>
  11. <artifactId>junit</artifactId>
  12. <version>3.8.1</version>
  13. <scope>test</scope>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-context</artifactId>
  18. <version>4.0.8.RELEASE</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context-support</artifactId>
  23. <version>4.0.8.RELEASE</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-web</artifactId>
  28. <version>4.0.8.RELEASE</version>
  29. </dependency>
  30. <!-- dubbo start--> <dependency>
  31. <groupId>com.alibaba</groupId>
  32. <artifactId>dubbo</artifactId>
  33. <version>2.5.3</version>
  34. <exclusions>
  35. <exclusion>
  36. <artifactId>spring</artifactId>
  37. <groupId>org.springframework</groupId>
  38. </exclusion>
  39. </exclusions>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.apache.zookeeper</groupId>
  43. <artifactId>zookeeper</artifactId>
  44. <version>3.4.8</version>
  45. <exclusions>
  46. <exclusion>
  47. <groupId>log4j</groupId>
  48. <artifactId>log4j</artifactId>
  49. </exclusion>
  50. </exclusions>
  51. </dependency>
  52. <dependency>
  53. <groupId>log4j</groupId>
  54. <artifactId>log4j</artifactId>
  55. <version>1.2.16</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>com.github.sgroschupf</groupId>
  59. <artifactId>zkclient</artifactId>
  60. <version>0.1</version>
  61. </dependency>
  62. </dependencies>
  63. </project>

发表评论

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

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

相关阅读

    相关 Dubbo 入门

    一、背景与架构(摘自官网) 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不

    相关 Dubbo - Dubbo框架

    简单说明 Dubbo是一个基于RPC协议的分布式框架,RPC的全称为Remote Procedure Call,其目的为实现远程调用。而分布式的目的是为了能够更好的针对不

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

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