Dubbo-概述,示例Demo

来源:互联网 发布:什么叫软件饱和 编辑:程序博客网 时间:2024/05/16 11:04

因为公司要使用dubbo,所以学习一下。随着互联网的发展,程序架构由最先看是的单一架构,到垂直应用架构,再到分布式服务架构(RPC),最后到流式计算架构(SOA)。而dubbo最主要的就是应用于,当服务越来越多,服务URL配置管理也越来越复杂,单点压力越来越大。以及服务之间的依赖关系错综复杂,服务调用量越来越大,那么这个服务需要多少机器支撑,该什么时候加机器呢?

dubbo分为服务提供者,服务消费者,以及监视器和注册中心。服务提供者在启动服务的同时,会注册到注册中心。服务消费者也会注册到注册中心,当注册中心的服务者列表发生变更时,注册中心会推送到消费者,注册中心与消费者之间是长连接。消费者每次调用服务者的服务时是不需要经过注册中心的,而是直接调用服务者的接口。服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。监控中心会将这些数据形成报表展示,发送给调度中心。

接下面参照官网demo在本地写了一个小demo.

第一步,定义服务接口

package org.lic.dubbo.demo;public interface DemoService {String sayHello(String name); }
这个接口是在服务提供者和服务消费者之间共享的,所以我把它单独放到了一个项目里面打包。下面是pom.xml文件内容
<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>org.lic</groupId>  <artifactId>dubboDemo</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>dubboDemo 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>${project.artifactId}</finalName>  <plugins>    <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-compiler-plugin</artifactId>      <configuration>        <source>1.7</source>        <target>1.7</target>      </configuration>    </plugin>  </plugins>  </build></project>
第二步,创建服务提供者。
首先看pom.xml文件内容
<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>org.lic</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.version>4.2.5.RELEASE</spring.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><jdk.version>1.7</jdk.version>  </properties>    <dependencies>          <dependency>              <groupId>com.alibaba</groupId>              <artifactId>dubbo</artifactId>              <version>2.5.3</version>              <exclusions>                  <exclusion>                      <groupId>org.springframework</groupId>                      <artifactId>spring</artifactId>                  </exclusion>              </exclusions>          </dependency>          <dependency>              <groupId>com.github.sgroschupf</groupId>              <artifactId>zkclient</artifactId>              <version>0.1</version>          </dependency>          <!-- spring相关 -->          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-core</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-beans</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-context</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-web</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-tx</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-orm</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>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-jms</artifactId>              <version>${spring.version}</version>          </dependency>    <dependency>      <groupId>org.lic</groupId>      <artifactId>dubboDemo</artifactId>      <version>0.0.1-SNAPSHOT</version>    </dependency>        <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>  </dependencies>    <build>    <finalName>${project.artifactId}</finalName>  <plugins>    <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-compiler-plugin</artifactId>      <configuration>        <source>1.7</source>        <target>1.7</target>      </configuration>    </plugin>  </plugins>  </build></project>
这里我引入了dubbo包,已经先关以来包。还有服务接口。如果在一个项目里面引用自己创建的另外一个项目,只需要在dependecy里面把groupId, artifactId,version换成项目对应的就可以了,如
    <dependency>      <groupId>org.lic</groupId>      <artifactId>dubboDemo</artifactId>      <version>0.0.1-SNAPSHOT</version>    </dependency>  
接下来我们实现这个服务接口
package org.lic.dubbo.demo.provider;import org.lic.dubbo.demo.DemoService;public class DemoServiceImpl implements DemoService{public String sayHello(String name){return "Hello," + name;}}
官方文档说对消息放隐藏实现,这想到的方式就是把接口单独成项目,打包共享。不知道大家有没有其他想法。

接着使用spring配置声明暴露服务

<?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-provide"  />    <!-- 使用multicast广播注册中心暴露服务地址 -->    <dubbo:registry address="multicast://224.5.6.7:1234" />        <!-- 使用zookeeper注册中心暴露服务地址 -->    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"/> -->    <!-- 用dubbo协议在20880端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20880" />        <!-- 用dubbo协议在20880端口暴露服务 -->    <!-- <dubbo:protocol name="dubbo" port="29014"/> -->    <!-- 声明需要暴露的服务接口 -->    <dubbo:service interface="org.lic.dubbo.demo.DemoService" ref="demoService" />    <!-- 和本地bean一样实现服务 -->    <bean id="demoService" class="org.lic.dubbo.demo.provider.DemoServiceImpl" /></beans>
这里可以看到我把zookeeper注册中心暴露服务地址注释掉了,官方建议是dubbo与zookeeper一起使用的。zookeeper是一个分布式的协调服务,为分布式提供了配置微信,名字服务,分布式同步,组服务等这些功能。我们先做做简单的,不加入zookeeper.

还要注意一点

    <!-- 使用multicast广播注册中心暴露服务地址 -->    <dubbo:registry address="multicast://224.5.6.7:1234" />
这里的ip是应该填D类IP地址,D类地址多用于广播(Multicast),D类IP地址第一个字节以“1110”开始,它是一个专门保留的地址。它并不指向特定的网络,目前这一类地址呗用在多点广播(Multicast)中。多点广播地址用来一次寻址一组计算机,它标志共享同一协议的一组计算机。地址范围是224.0.0.1-239.255.255.254,所以我们这里能填写的地址范围也是这个。

最后是启动方法

package org.lic.dubbo.demo.provider;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main(String[] args) throws Exception{ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/provider.xml"});context.start();System.out.println("提供者服务已注册成功");System.out.println("请按任意键取消提供者服务");System.in.read(); // 按任意键退出}}
直接启动,可以看到控制台输出
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).log4j:WARN Please initialize the log4j system properly.提供者服务已注册成功请按任意键取消提供者服务
第三步,创建服务消费者

还是一样,先看pom.xml,和服务提供者的一样

<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>org.lic</groupId>  <artifactId>dubbo-consumer</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>dubbo-consumer Maven Webapp</name>  <url>http://maven.apache.org</url>    <properties>    <spring.version>4.2.5.RELEASE</spring.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><jdk.version>1.7</jdk.version>  </properties>    <dependencies>          <!-- 添加dubbo依赖 -->          <dependency>              <groupId>com.alibaba</groupId>              <artifactId>dubbo</artifactId>              <version>2.5.3</version>              <exclusions>                  <exclusion>                      <groupId>org.springframework</groupId>                      <artifactId>spring</artifactId>                  </exclusion>              </exclusions>          </dependency>          <!-- 添加zk客户端依赖 -->          <dependency>              <groupId>com.github.sgroschupf</groupId>              <artifactId>zkclient</artifactId>              <version>0.1</version>          </dependency>          <!-- spring相关 -->          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-core</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-beans</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-context</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-web</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-tx</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-orm</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>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-jms</artifactId>              <version>${spring.version}</version>          </dependency>        <dependency>      <groupId>org.lic</groupId>      <artifactId>dubboDemo</artifactId>      <version>0.0.1-SNAPSHOT</version>    </dependency>      <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>  </dependencies>      <build>    <finalName>${project.artifactId}</finalName>  <plugins>    <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-compiler-plugin</artifactId>      <configuration>        <source>1.7</source>        <target>1.7</target>      </configuration>    </plugin>  </plugins>  </build></project>
再使用spring配置引用远程服务
<?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-consumer"  />    <!-- 使用multicast广播注册中心暴露发现服务地址 -->    <dubbo:registry address="multicast://224.5.6.7:1234" />    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->    <dubbo:reference id="demoService" interface="org.lic.dubbo.demo.DemoService" /></beans>
最后是启动类
package org.lic.dubbo.demo.consumer;import org.lic.dubbo.demo.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Consumer {public static void main(String[] args) throws Exception{ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/consumer.xml"});context.start();DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理String hello = demoService.sayHello("world"); // 执行远程方法System.out.println(hello); // 显示调用结果}}
可以从控制台看到输出
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).log4j:WARN Please initialize the log4j system properly.Hello,world
这个是demo代码下载地址http://download.csdn.net/download/licheng989/9946140,欢迎大家扔砖。









原创粉丝点击