zoukankan      html  css  js  c++  java
  • Dubbo(五) Dubbo入门demo——helloworld

    前言

    前面我已经介绍了dubbo的一些基本工具和知识,让大家简单的了解了下RPC框架和Dubbo。接下来就是重点了,Dubbo的helloworld项目。

    一、搭建项目

    首先我们新建三个maven项目如下图:

    • dubbo-provider(服务提供者)
    • dubbo-api(api提供者)
    • dubbo-consumer(消费者)

    OKOK!这里我们再简单介绍下API项目。我前面说了,只有消费者和生产者是没错的。but你想啊,你在生产者项目中要用接口,消费者中要用接口,是是不是两个项目都TM要写同样的代码啊,那多麻烦!!

    所以这里我们用一个项目把需要的公共代码丢进去,一些接口啊,实体啊啥的,然后打成jar包导入到生产者和消费者中,多完美。


    API项目:

    由于helloworld的demo过于简单,这里我们就丢一个公共接口放里面。

    package com.provider;
    /**
    * ClassName:DemoService
    * Function: TODO ADD FUNCTION.
    * Date: 2017年11月28日 下午6:43:49
    * @author HXY
    */
    public interface DemoService {
    String sayHello(String name);
    }

    然后pom文件只需要注明版本号信息啥的:

    <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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>hxy.superFather.forJava</groupId>
    <artifactId>dubbo-api</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-api Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    <build>
    <finalName>dubbo-api</finalName>
    </build>
    </project>

    provider项目:

    接口实现类:

    package com.provider.impl;
    import com.bean.User;
    import com.provider.DemoService;
    /**
    * ClassName:DemoServiceImpl
    * Function: TODO ADD FUNCTION.
    * Date: 2017年11月29日 上午9:31:23
    * @author HXY
    */
    public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
    return "Hello " + name;
    }
    }

    服务注册启动类:

    package com;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class Provider {
    public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "provider.xml" });
    context.start();
    System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
    }

    spring整合dubbo配置文件provider.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-provider-app" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.provider.DemoService" ref="demoService" />
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.provider.impl.DemoServiceImpl" />
    </beans>

    pom配置文件:

    <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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>dubbo</groupId>
    <artifactId>dubbo-provider</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-provider Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
    <!-- spring版本号 -->
    <spring.version>4.0.2.RELEASE</spring.version>
    <!-- mybatis版本号 -->
    <mybatis.version>3.2.6</mybatis.version>
    <!-- log4j日志文件管理包版本 -->
    <slf4j.version>1.7.7</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    <!-- shiro版本号 -->
    <shiro.version>1.2.3</shiro.version>
    </properties>
    <dependencies>
    <!-- 6 dubbo zookeeper相关依赖start -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    </exclusion>
    <exclusion>
    <groupId>org.jboss.netty</groupId>
    <artifactId>netty</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.6</version>
    </dependency>
    <dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
    </dependency>
    <dependency>
    <groupId>hxy.superFather.forJava</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- junit配置 -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>
    <!-- 导入java ee jar 包 -->
    <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    </dependency>
    <!-- spring核心包 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-oxm</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <!-- json数据 -->
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
    </dependency>
    <dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
    </dependency>
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.5</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.7</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.9</version>
    </dependency>
    <!-- JSTL标签类 -->
    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>
    </dependencies>
    <build>
    <finalName>dubbo-provider</finalName>
    </build>
    </project>

    DubboConsummer项目:

    消费类:

    public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "spring-common.xml" });
    context.start();
    for(int i = 0;i<10;i++){
    DemoService demoService = (DemoService) context.getBean("demoService");
    String hello = demoService.sayHello("tom");
    System.out.println("-------------"+hello);
    Thread.sleep(5000);
    }
    System.in.read();
    System.out.println("end");
    }

    spring整合dubbo配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    <import resource="classpath:spring-dubbo.xml"/>
    </beans>
    <?xml version="1.0" encoding="UTF-8"?>
    <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
    ">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="counmser" />
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <!-- 使用zookeeper注册中心暴露服务地址 即zookeeper的所在服务器ip地址和端口号 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183" />
    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
    <dubbo:reference id="demoService"
    interface="com.provider.DemoService" />
    </beans>


    二、运行

    在正常项目部署中,我们会打包成可执行的jar文件启动,就还需要配置pom才能让maven打包。

    但是这个我们后续再讲,今天的demo我们就用eclipse的main方法执行了,原理其实一样的。

    1. 首先打开zookeeper工具
    2. 启动服务提供者,他会自动注册到配置文件配置的zookeeper中
    3. 启动消费者,调用服务

    然后看到这样的打印,说明成功了,dubbo的基本使用原理在做helloworld项目过程中,也应该有了大致了解。

    三、其他问题解决

    • spring-dubbo整合的配置文件中,dubbo配置项红叉报错问题。
      原因
      因为dubbo.xsd文件找不到吧,好像是这个原因!
      解决方案
        1. 到maven库中找到dubbo的jar包,将其解压。
        2. 找到解压文件夹下的META-INF文件夹下的dubbo.xsd。
        3. eclipse选择windows->preferrence->xml->xml Catalog,右侧add-xml文件,在文件目录中选择dubbo.xsd。
        4. 下面的key值填写配置文件中一致的 http://code.alibabatech.com/schema/dubbo/dubbo.xsd
          如下图:
          确定完成之后重新验证配置文件即可。



  • 相关阅读:
    如何看Linux服务器的load average
    java中File读取文件中文乱码
    LR代理录制
    虚拟机打开虚拟文件报不兼容,无法使用
    面向对象示例-注册登录例子
    pickle 模块
    item方法
    加密方法
    @classmethod 和 @staticmethod 用法
    python 中 @property 的用法
  • 原文地址:https://www.cnblogs.com/tohxyblog/p/8251591.html
Copyright © 2011-2022 走看看