lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

这个博客是在https://blog.csdn.net/Crazer_cy/article/details/80397649篇文章上的基础上,自己学习用的。

  1. Zookeeper为dubbo的注册中心,dubbo服务的生产者和消费者都需要在Zookeeper进行注册;
  2. 下载zookeeper压缩包并解压;
  3. 进入conf目录将 zoo_sample.cfg 改名为 zoo.cfg;
  4. 进入bin目录双击zkServer.cmd,若启动成功,则windows单机版zookeeper搭建成功
  5. 使用duboo-admin 

 

 

  1. 使用IntelliJ IDEA搭建Dubbo:创建一个maven空项目,作为项目的父工程

  2. 在pom.xml添加以下公共的依赖:

    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5.  
      <modelVersion>4.0.0</modelVersion>
    6.  
       
    7.  
      <groupId>com.dubbo</groupId>
    8.  
      <artifactId>dubbo_demo</artifactId>
    9.  
      <packaging>pom</packaging>
    10.  
      <version>1.0-SNAPSHOT</version>
    11.  
       
    12.  
      <properties>
    13.  
      <motan.version>0.3.0</motan.version>
    14.  
      <dubbo.version>2.5.3</dubbo.version>
    15.  
      <dubbox.version>2.8.4</dubbox.version>
    16.  
      <spring.version>4.3.6.RELEASE</spring.version>
    17.  
      <java.version>1.8</java.version>
    18.  
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    19.  
      </properties>
    20.  
       
    21.  
      <dependencies>
    22.  
      <dependency>
    23.  
      <groupId>com.alibaba</groupId>
    24.  
      <artifactId>dubbo</artifactId>
    25.  
      <version>2.5.3</version>
    26.  
      <exclusions>
    27.  
      <exclusion>
    28.  
      <groupId>org.springframework</groupId>
    29.  
      <artifactId>spring</artifactId>
    30.  
      </exclusion>
    31.  
      </exclusions>
    32.  
      </dependency>
    33.  
      <dependency>
    34.  
      <groupId>com.github.sgroschupf</groupId>
    35.  
      <artifactId>zkclient</artifactId>
    36.  
      <version>0.1</version>
    37.  
      </dependency>
    38.  
      <!-- spring相关 -->
    39.  
      <dependency>
    40.  
      <groupId>org.springframework</groupId>
    41.  
      <artifactId>spring-core</artifactId>
    42.  
      <version>${spring.version}</version>
    43.  
      </dependency>
    44.  
      <dependency>
    45.  
      <groupId>org.springframework</groupId>
    46.  
      <artifactId>spring-beans</artifactId>
    47.  
      <version>${spring.version}</version>
    48.  
      </dependency>
    49.  
      <dependency>
    50.  
      <groupId>org.springframework</groupId>
    51.  
      <artifactId>spring-context</artifactId>
    52.  
      <version>${spring.version}</version>
    53.  
      </dependency>
    54.  
      <dependency>
    55.  
      <groupId>org.springframework</groupId>
    56.  
      <artifactId>spring-jdbc</artifactId>
    57.  
      <version>${spring.version}</version>
    58.  
      </dependency>
    59.  
      <dependency>
    60.  
      <groupId>org.springframework</groupId>
    61.  
      <artifactId>spring-web</artifactId>
    62.  
      <version>${spring.version}</version>
    63.  
      </dependency>
    64.  
      <dependency>
    65.  
      <groupId>org.springframework</groupId>
    66.  
      <artifactId>spring-webmvc</artifactId>
    67.  
      <version>${spring.version}</version>
    68.  
      </dependency>
    69.  
      <dependency>
    70.  
      <groupId>org.springframework</groupId>
    71.  
      <artifactId>spring-aop</artifactId>
    72.  
      <version>${spring.version}</version>
    73.  
      </dependency>
    74.  
      <dependency>
    75.  
      <groupId>org.springframework</groupId>
    76.  
      <artifactId>spring-tx</artifactId>
    77.  
      <version>${spring.version}</version>
    78.  
      </dependency>
    79.  
      <dependency>
    80.  
      <groupId>org.springframework</groupId>
    81.  
      <artifactId>spring-orm</artifactId>
    82.  
      <version>${spring.version}</version>
    83.  
      </dependency>
    84.  
      <dependency>
    85.  
      <groupId>org.springframework</groupId>
    86.  
      <artifactId>spring-context-support</artifactId>
    87.  
      <version>${spring.version}</version>
    88.  
      </dependency>
    89.  
      <dependency>
    90.  
      <groupId>org.springframework</groupId>
    91.  
      <artifactId>spring-test</artifactId>
    92.  
      <version>${spring.version}</version>
    93.  
      </dependency>
    94.  
      <dependency>
    95.  
      <groupId>org.springframework</groupId>
    96.  
      <artifactId>spring-jms</artifactId>
    97.  
      <version>${spring.version}</version>
    98.  
      </dependency>
    99.  
      <dependency>
    100.  
      <groupId>org.aspectj</groupId>
    101.  
      <artifactId>aspectjrt</artifactId>
    102.  
      <version>1.6.11</version>
    103.  
      </dependency>
    104.  
      <dependency>
    105.  
      <groupId>org.aspectj</groupId>
    106.  
      <artifactId>aspectjweaver</artifactId>
    107.  
      <version>1.6.11</version>
    108.  
      </dependency>
    109.  
      </dependencies>
    110.  
       
    111.  
      <modules>
    112.  
      <module>dubbo_api</module>
    113.  
      <module>dubbo_consumer</module>
    114.  
      <module>dubbo_provider</module>
    115.  
      </modules>
    116.  
      </project>
  3. 在刚才创建的dubbo_demo下创建一个新的Module:dubbo_api(服务接口)

  4. dubbo_api的pom.xml文件如下:

    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5.  
      <parent>
    6.  
      <artifactId>dubbo_demo</artifactId>
    7.  
      <groupId>com.dubbo</groupId>
    8.  
      <version>1.0-SNAPSHOT</version>
    9.  
      </parent>
    10.  
       
    11.  
      <modelVersion>4.0.0</modelVersion>
    12.  
       
    13.  
      <artifactId>dubbo_api</artifactId>
    14.  
       
    15.  
      <packaging>jar</packaging>
    16.  
       
    17.  
      </project>
  5. 重复3、4步骤,创建dubbo_provider(服务生产者),对应的pom.xml文件:

    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5.  
      <parent>
    6.  
      <artifactId>dubbo_demo</artifactId>
    7.  
      <groupId>com.dubbo</groupId>
    8.  
      <version>1.0-SNAPSHOT</version>
    9.  
      </parent>
    10.  
      <modelVersion>4.0.0</modelVersion>
    11.  
       
    12.  
      <artifactId>dubbo_provider</artifactId>
    13.  
       
    14.  
      <dependencies>
    15.  
      <dependency>
    16.  
      <groupId>com.dubbo</groupId>
    17.  
      <artifactId>dubbo_api</artifactId>
    18.  
      <version>1.0-SNAPSHOT</version>
    19.  
      <scope>compile</scope>
    20.  
      </dependency>
    21.  
      </dependencies>
    22.  
       
    23.  
      </project>
  6. 再重复3、4步骤,创建dubbo_consumer(服务消费者),对应pom.xml文件如下:

    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5.  
      <parent>
    6.  
      <artifactId>dubbo_demo</artifactId>
    7.  
      <groupId>com.dubbo</groupId>
    8.  
      <version>1.0-SNAPSHOT</version>
    9.  
      </parent>
    10.  
      <modelVersion>4.0.0</modelVersion>
    11.  
       
    12.  
      <artifactId>dubbo_consumer</artifactId>
    13.  
       
    14.  
      <dependencies>
    15.  
      <dependency>
    16.  
      <groupId>com.dubbo</groupId>
    17.  
      <artifactId>dubbo_api</artifactId>
    18.  
      <version>1.0-SNAPSHOT</version>
    19.  
      <scope>compile</scope>
    20.  
      </dependency>
    21.  
      </dependencies>
    22.  
       
    23.  
      </project>
  7. 最后项目结构如下:

  8. 框架搭建起来之后,开始写代码和配置文件,首先是dubbo_api:需要定义服务接口:

    1.  
      package com.api.service;
    2.  
       
    3.  
      /**
    4.  
      * 定义服务接口
    5.  
      */
    6.  
      public interface DemoService {
    7.  
      String sayHello(String name);
    8.  
      }
  9. 然后再dubbo_provider中实现上述接口(由于在pom.xml文件中,将dubbo_api作为依赖,故能引入上述接口):

    1.  
      package com.provider.service;
    2.  
       
    3.  
      import com.api.service.DemoService;
    4.  
       
    5.  
      public class DemoServiceImpl implements DemoService {
    6.  
      public String sayHello(String name) {
    7.  
      return "Hello "+name;
    8.  
      }
    9.  
      }
  10. dubbo_provider项目中的resource文件包含dubbo-provider.xml和springmvc.xml配置文件,其中dubbo-provider.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.  
       
    10.  
      <!-- 提供方应用信息,用于计算依赖关系 -->
    11.  
      <dubbo:application name="dubbo_provider" />
    12.  
       
    13.  
      <!-- 使用zookeeper注册中心暴露服务地址 -->
    14.  
      <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    15.  
       
    16.  
      <!-- 用dubbo协议在20880端口暴露服务 -->
    17.  
      <dubbo:protocol name="dubbo" port="20880" />
    18.  
       
    19.  
      <!-- 声明需要暴露的服务接口 -->
    20.  
      <dubbo:service interface="com.api.service.DemoService" ref="demoService" />
    21.  
       
    22.  
      <!-- 接口实现类-->
    23.  
      <bean id="demoService" class="com.provider.service.DemoServiceImpl"/>
    24.  
       
    25.  
      </beans>
    26.  
       
    springmvc.xml如下:
    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    3.  
      xmlns:context="http://www.springframework.org/schema/context"
    4.  
      xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5.  
      xsi:schemaLocation="http://www.springframework.org/schema/aop
    6.  
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    7.  
      http://www.springframework.org/schema/beans
    8.  
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    9.  
      http://www.springframework.org/schema/context
    10.  
      http://www.springframework.org/schema/context/spring-context-4.0.xsd
    11.  
      http://www.springframework.org/schema/util
    12.  
      http://www.springframework.org/schema/util/spring-util-4.0.xsd"
    13.  
      default-autowire="byName">
    14.  
       
    15.  
      <aop:aspectj-autoproxy />
    16.  
      <context:component-scan base-package="com" />
    17.  
      <import resource="classpath:dubbo-provider.xml" />
    18.  
      </beans>

    再在该工程下写一个测试类,后面测试用到:

    1.  
      package com.provider.test;
    2.  
       
    3.  
      import org.springframework.context.support.ClassPathXmlApplicationContext;
    4.  
       
    5.  
      import java.io.IOException;
    6.  
       
    7.  
      public class ProviderTest {
    8.  
      public static void main(String[] args) {
    9.  
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
    10.  
      context.start();
    11.  
       
    12.  
      System.out.println("Dubbo provider start...");
    13.  
       
    14.  
      try {
    15.  
      System.in.read(); // 按任意键退出
    16.  
      } catch (IOException e) {
    17.  
      e.printStackTrace();
    18.  
      }
    19.  
      }
    20.  
      }
  11. 接下来开始写消费者——dubbo_consumer的代码和配置文件:resource中的dubbo-consumer.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" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4.  
      xsi:schemaLocation="http://www.springframework.org/schema/beans
    5.  
      http://www.springframework.org/schema/beans/spring-beans.xsd
    6.  
      http://code.alibabatech.com/schema/dubbo
    7.  
      http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
    8.  
      <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    9.  
      <dubbo:application name="dubbo_consumer" />
    10.  
      <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    11.  
      <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
    12.  
      <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    13.  
      <dubbo:reference id="demoService" interface="com.api.service.DemoService" />
    14.  
      </beans>
    springmvc.xml:
    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    3.  
      xmlns:context="http://www.springframework.org/schema/context"
    4.  
      xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5.  
      xsi:schemaLocation="http://www.springframework.org/schema/aop
    6.  
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    7.  
      http://www.springframework.org/schema/beans
    8.  
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    9.  
      http://www.springframework.org/schema/context
    10.  
      http://www.springframework.org/schema/context/spring-context-4.0.xsd
    11.  
      http://www.springframework.org/schema/util
    12.  
      http://www.springframework.org/schema/util/spring-util-4.0.xsd"
    13.  
      default-autowire="byName">
    14.  
       
    15.  
      <aop:aspectj-autoproxy />
    16.  
      <context:component-scan base-package="com" />
    17.  
      <import resource="classpath:/dubbo-consumer.xml" />
    18.  
      </beans>
  12. 然后写消费者的测试

    1.  
      package com.consumer;
    2.  
       
    3.  
      import com.api.service.DemoService;
    4.  
      import org.springframework.context.support.ClassPathXmlApplicationContext;
    5.  
       
    6.  
      import java.io.IOException;
    7.  
       
    8.  
      public class ConsumerTest {
    9.  
      public static void main(String[] args) {
    10.  
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });
    11.  
       
    12.  
      context.start();
    13.  
      DemoService demoService = (DemoService) context.getBean("demoService");
    14.  
       
    15.  
      System.out.println(demoService.sayHello("哈哈哈"));
    16.  
      try {
    17.  
      System.in.read();
    18.  
      } catch (IOException e) {
    19.  
      e.printStackTrace();
    20.  
      }
    21.  
      }
    22.  
      }
  13. 到现在整个代码就开发完成了,按照先后顺序,首先启动Zookeeper:zkServer.cmd      然后启动服务生产者测试类,最后启动服务消费者的测试类,当控制台输出以下结果,则代表成功。

    服务生产者测试类控制台输出:
    服务消费者测试了控制台输出:

 

 

 

使用dubbo-admin管理平台查看运行情况。

posted on 2019-04-22 15:31  白露~  阅读(7268)  评论(0编辑  收藏  举报