dubbo

来源:互联网 发布:淘宝培训班 编辑:程序博客网 时间:2024/06/05 21:16

1.win7安装Zookeeper  3.4.6(zookeeper-3.3.6.tar.gz)解压到d盘

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两个文件夹,如果不创建,后面运行脚本是地会报错。

3.启动 bin/zkServer.cmd

4.下载dubbo源码 dubbo-admin-2.5.3.war 放置tomcat webapps下

5.启动tomcat,访问root/root 



Dubbo是什么?

Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

其核心部分包含:
  • 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
  • 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
  • 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

Dubbo能做什么?

  • 透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
  • 软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
  • 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

Spring集成

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
1.创建一个接口API,打包成Jar

(创建interface  后,可以执行maven build了。  在项目上,右键-》runas-》 maven clean,执行完后,再执行 run as -》maven install)


2.服务端开发


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/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>    <groupId>com.study</groupId>    <artifactId>StudyDubboServer</artifactId>    <version>0.1</version>    <build/>    <dependencies>           <dependency>          <groupId>com.alibaba</groupId>          <artifactId>dubbo</artifactId>          <version>2.5.3</version>      </dependency>      <dependency>         <groupId>com.study</groupId>         <artifactId>StudyDubboApi</artifactId>         <version>0.0.1-SNAPSHOT</version>      </dependency>        <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring</artifactId>         <version>2.5.6.SEC03</version>      </dependency>      <dependency>          <groupId>org.apache.zookeeper</groupId>          <artifactId>zookeeper</artifactId>          <version>3.4.6</version>          <type>pom</type>      </dependency>      <dependency>          <groupId>com.101tec</groupId>          <artifactId>zkclient</artifactId>          <version>0.4</version>      </dependency>                 </dependencies>  </project>

applicationProvider.xml
(http://code.alibabatech.com/schema/dubbo/dubbo.xsd无效了,自行下载替换)

<?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="Provider_DubboHelloService" />             <!-- 使用zookeeper注册中心暴露服务地址 -->       <dubbo:registry address="zookeeper://127.0.0.1:2181" ></dubbo:registry>       <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>               <!-- 要暴露的服务API接口 -->       <dubbo:service interface="com.study.dubbo.demo.HelloServiceApi" ref="HelloService" />            <!-- 接口的实现,注意beanid 需要和服务接口的ref 一致 -->      <bean id="HelloService" class="com.study.dubbo.demo.HelloServiceApiImpl" />    </beans>


ProviderMain

package com.study.api;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ProviderMain {public static void main(String[] args) throws Exception {            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                  new String[] { "applicationProvider.xml" });          context.start();          System.out.println("按任意键退出");          System.in.read();      }  }

HelloServiceApiImpl

package com.study.dubbo.demo;public class HelloServiceApiImpl implements HelloServiceApi{ String myName="";           public  String  sayHello(String name){                    myName = name;                    String ret="Hello, "+name+"!";                    System.out.println(  ret );                    return  ret;                          }            public  String  getName ( ){                      System.out.println( "Now name is:"+ myName+";" );                    return  myName;                }  }


3.客户端


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/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.study</groupId>    <artifactId>StudyDubboCli</artifactId>    <version>0.1</version>    <build/>        <dependencies>           <dependency>          <groupId>com.alibaba</groupId>          <artifactId>dubbo</artifactId>          <version>2.5.3</version>      </dependency>      <dependency>         <groupId>com.study</groupId>         <artifactId>StudyDubboApi</artifactId>         <version>0.0.1-SNAPSHOT</version>      </dependency>        <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring</artifactId>         <version>2.5.6.SEC03</version>      </dependency>      <dependency>          <groupId>com.101tec</groupId>          <artifactId>zkclient</artifactId>          <version>0.4</version>      </dependency>                 </dependencies>  </project>

ConsumerMain

package com.study.api;import com.study.dubbo.demo.HelloServiceComsumer;public class ConsumerMain { public static void main(String[] args) throws Exception{  HelloServiceComsumer consumerService = new HelloServiceComsumer();      consumerService.getServiceObj();            String myFamily[]={ "Mom","Daddy","Honey","Pretty Girl"};            for(int i=0;i<myFamily.length;i++){          consumerService.sayHello(myFamily[i]  );          consumerService.getName();                }            System.out.println("按任意键退出");      System.in.read();  }  }
HelloServiceComsumer

package com.study.dubbo.demo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloServiceComsumer{HelloServiceApi demoService;          /*      * 获取服务的Provider对象      */      public void getServiceObj(){          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                  new String[] { "applicationConsumer.xml" });          context.start();            demoService = (HelloServiceApi) context                  .getBean("HelloService");                }        /*      * 调用对象的sayHello 方法      */      public void sayHello( String name) {            System.out.println(demoService.sayHello( name ));        }            /*      * 调用对象的getName 方法      */      public String getName(){                    String ret=demoService.getName( );          System.out.println("Now Provider Name is:"+ ret);                 return ret;                }  }
applicationConsumer.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-2.5.xsd     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">          <!-- consumer application name -->      <dubbo:application name="Consumer_DubboHelloService" />            <!-- registry address, used for consumer to discover services -->      <dubbo:registry address="zookeeper://127.0.0.1:2181" ></dubbo:registry>      <dubbo:consumer timeout="5000" />            <!-- 引用服务接口 -->        <dubbo:reference id="HelloService" interface="com.study.dubbo.demo.HelloServiceApi"/>    </beans>


0 0
原创粉丝点击