dubbo的架设

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:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <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">  
  2.    <modelVersion>4.0.0</modelVersion>    
  3.   <groupId>com.study</groupId>    
  4.   <artifactId>StudyDubboServer</artifactId>    
  5.   <version>0.1</version>    
  6.   <build/>    
  7.   <dependencies>     
  8.       
  9.     <dependency>    
  10.         <groupId>com.alibaba</groupId>    
  11.         <artifactId>dubbo</artifactId>    
  12.         <version>2.5.3</version>    
  13.     </dependency>    
  14.     <dependency>    
  15.        <groupId>com.study</groupId>    
  16.        <artifactId>StudyDubboApi</artifactId>    
  17.        <version>0.0.1-SNAPSHOT</version>    
  18.     </dependency>      
  19.     <dependency>    
  20.        <groupId>org.springframework</groupId>    
  21.        <artifactId>spring</artifactId>    
  22.        <version>2.5.6.SEC03</version>    
  23.     </dependency>    
  24.     <dependency>    
  25.         <groupId>org.apache.zookeeper</groupId>    
  26.         <artifactId>zookeeper</artifactId>    
  27.         <version>3.4.6</version>    
  28.         <type>pom</type>    
  29.     </dependency>    
  30.     <dependency>    
  31.         <groupId>com.101tec</groupId>    
  32.         <artifactId>zkclient</artifactId>    
  33.         <version>0.4</version>    
  34.     </dependency>    
  35.                
  36.   </dependencies>    
  37. </project>  

applicationProvider.xml
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. (http://code.alibabatech.com/schema/dubbo/dubbo.xsd无效了,自行下载替换)  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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="Provider_DubboHelloService" />    
  12.           
  13.    <!-- 使用zookeeper注册中心暴露服务地址 -->      
  14.    <dubbo:registry address="zookeeper://127.0.0.1:2181" ></dubbo:registry>    
  15.     
  16.    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>     
  17.            
  18.    <!-- 要暴露的服务API接口 -->      
  19.    <dubbo:service interface="com.study.dubbo.demo.HelloServiceApi" ref="HelloService" />      
  20.        
  21.    <!-- 接口的实现,注意beanid 需要和服务接口的ref 一致 -->     
  22.    <bean id="HelloService" class="com.study.dubbo.demo.HelloServiceApiImpl" />    
  23.     
  24. </beans>  


ProviderMain

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.study.api;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class ProviderMain {  
  6.     public static void main(String[] args) throws Exception {    
  7.             
  8.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
  9.                 new String[] { "applicationProvider.xml" });    
  10.         context.start();    
  11.         System.out.println("按任意键退出");    
  12.         System.in.read();    
  13.     }    
  14. }  

HelloServiceApiImpl

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.study.dubbo.demo;  
  2.   
  3. public class HelloServiceApiImpl implements HelloServiceApi{  
  4.      String myName="";    
  5.        
  6.         public  String  sayHello(String name){    
  7.                 
  8.             myName = name;    
  9.                 
  10.             String ret="Hello, "+name+"!";    
  11.                 
  12.             System.out.println(  ret );    
  13.                 
  14.             return  ret;    
  15.                 
  16.                 
  17.         }    
  18.             
  19.         public  String  getName ( ){    
  20.         
  21.                 
  22.             System.out.println( "Now name is:"+ myName+";" );    
  23.                 
  24.             return  myName;    
  25.                 
  26.         }    
  27. }  


3.客户端


pom.xml

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <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">    
  2.   <modelVersion>4.0.0</modelVersion>    
  3.   <groupId>com.study</groupId>    
  4.   <artifactId>StudyDubboCli</artifactId>    
  5.   <version>0.1</version>    
  6.   <build/>      
  7.     
  8.   <dependencies>     
  9.       
  10.     <dependency>    
  11.         <groupId>com.alibaba</groupId>    
  12.         <artifactId>dubbo</artifactId>    
  13.         <version>2.5.3</version>    
  14.     </dependency>    
  15.     <dependency>    
  16.        <groupId>com.study</groupId>    
  17.        <artifactId>StudyDubboApi</artifactId>    
  18.        <version>0.0.1-SNAPSHOT</version>    
  19.     </dependency>      
  20.     <dependency>    
  21.        <groupId>org.springframework</groupId>    
  22.        <artifactId>spring</artifactId>    
  23.        <version>2.5.6.SEC03</version>    
  24.     </dependency>    
  25.     <dependency>    
  26.         <groupId>com.101tec</groupId>    
  27.         <artifactId>zkclient</artifactId>    
  28.         <version>0.4</version>    
  29.     </dependency>    
  30.                
  31.   </dependencies>    
  32. </project>  

ConsumerMain

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.study.api;  
  2.   
  3. import com.study.dubbo.demo.HelloServiceComsumer;  
  4.   
  5. public class ConsumerMain {  
  6.       
  7.      public static void main(String[] args) throws Exception{    
  8.         HelloServiceComsumer consumerService = new HelloServiceComsumer();    
  9.           
  10.         consumerService.getServiceObj();    
  11.             
  12.         String myFamily[]={ "Mom","Daddy","Honey","Pretty Girl"};    
  13.             
  14.         for(int i=0;i<myFamily.length;i++){    
  15.             consumerService.sayHello(myFamily[i]  );    
  16.             consumerService.getName();    
  17.                 
  18.         }    
  19.             
  20.         System.out.println("按任意键退出");    
  21.         System.in.read();    
  22.       
  23.     }    
  24. }  
HelloServiceComsumer

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.study.dubbo.demo;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class HelloServiceComsumer{  
  6.     HelloServiceApi demoService;    
  7.       
  8.     /*  
  9.      * 获取服务的Provider对象  
  10.      */    
  11.     public void getServiceObj(){    
  12.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
  13.                 new String[] { "applicationConsumer.xml" });    
  14.         context.start();    
  15.     
  16.         demoService = (HelloServiceApi) context    
  17.                 .getBean("HelloService");    
  18.             
  19.     }    
  20.     
  21.     /*  
  22.      * 调用对象的sayHello 方法  
  23.      */    
  24.     public void sayHello( String name) {    
  25.     
  26.         System.out.println(demoService.sayHello( name ));    
  27.     
  28.     }    
  29.         
  30.     /*  
  31.      * 调用对象的getName 方法  
  32.      */    
  33.     public String getName(){    
  34.             
  35.         String ret=demoService.getName( );    
  36.         System.out.println("Now Provider Name is:"+ ret);           
  37.         return ret;    
  38.             
  39.     }    
  40. }  
applicationConsumer.xml

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  6.     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">   
  7.         
  8.    <!-- consumer application name -->    
  9.     <dubbo:application name="Consumer_DubboHelloService" />    
  10.         
  11.     <!-- registry address, used for consumer to discover services -->    
  12.     <dubbo:registry address="zookeeper://127.0.0.1:2181" ></dubbo:registry>    
  13.     <dubbo:consumer timeout="5000" />    
  14.         
  15.     <!-- 引用服务接口 -->      
  16.     <dubbo:reference id="HelloService" interface="com.study.dubbo.demo.HelloServiceApi"/>    
  17.     
  18. </beans>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值