dubbo整合springmvc 使用 学习二(spring+dubbo+zookeeper单机服务)

学习目录在上一篇中有了,这篇我们来写一个本地的单服务的demo,下面我们看看需要什么!!!

1、zookeeper的安装:

(1)、zookeeper 的安装很简单,下载架包解压缩,解压缩后的目录下有个conf目录也就是zookeeper的配置文件在该目录下,该目录下有一个zoo_sample.cfg 文件,复制一个修改为zoo.cfg,像我们这里是单服务的所以这里的配置只需要修改dataDir的目录就可以了其他的可以不用修改,dataDir的目录可以自己随便定义!!!

2、服务提供者

创建一个maven 工程,配置spring ,dubbo,zookeeper等相关的jar包,下面来具体看看!

(1)、目录结构

(2)、我们配置pom.xml下载我们需要的jar包,pom.xml配置文件如下:


[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   <groupId>com.test</groupId>  
  6.   <artifactId>dubboser</artifactId>  
  7.   <version>0.0.1</version>  
  8.   <packaging>jar</packaging>  
  9.   
  10.   <name>dubboserver</name>  
  11.   <url>http://maven.apache.org</url>  
  12.   
  13.   <properties>  
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     <spring.version>3.1.4.RELEASE</spring.version>  
  16.     <slf4j.version>1.6.6</slf4j.version>  
  17.   </properties>  
  18.   
  19.   <dependencies>  
  20.     <dependency>  
  21.       <groupId>junit</groupId>  
  22.       <artifactId>junit</artifactId>  
  23.       <version>3.8.1</version>  
  24.       <scope>test</scope>  
  25.     </dependency>  
  26.     <!-- Spring -->  
  27.     <dependency>  
  28.         <groupId>org.springframework</groupId>  
  29.         <artifactId>spring-aop</artifactId>  
  30.         <version>${spring.version}</version>  
  31.     </dependency>  
  32.     <dependency>  
  33.         <groupId>org.springframework</groupId>  
  34.         <artifactId>spring-asm</artifactId>  
  35.         <version>${spring.version}</version>  
  36.     </dependency>  
  37.     <dependency>  
  38.         <groupId>org.springframework</groupId>  
  39.         <artifactId>spring-core</artifactId>  
  40.         <version>${spring.version}</version>  
  41.     </dependency>  
  42.     <dependency>  
  43.         <groupId>org.springframework</groupId>  
  44.         <artifactId>spring-beans</artifactId>  
  45.         <version>${spring.version}</version>  
  46.     </dependency>  
  47.     <dependency>  
  48.         <groupId>org.springframework</groupId>  
  49.         <artifactId>spring-context</artifactId>  
  50.         <version>${spring.version}</version>  
  51.     </dependency>  
  52.     <dependency>  
  53.         <groupId>org.springframework</groupId>  
  54.         <artifactId>spring-expression</artifactId>  
  55.         <version>${spring.version}</version>  
  56.     </dependency>  
  57.     <!-- spring end -->  
  58.     <!-- log -->  
  59.         <dependency>  
  60.             <groupId>log4j</groupId>  
  61.             <artifactId>log4j</artifactId>  
  62.             <version>1.2.16</version>  
  63.         </dependency>  
  64.         <dependency>  
  65.             <groupId>org.slf4j</groupId>  
  66.             <artifactId>slf4j-api</artifactId>  
  67.             <version>${slf4j.version}</version>  
  68.         </dependency>  
  69.         <dependency>  
  70.             <groupId>org.slf4j</groupId>  
  71.             <artifactId>slf4j-log4j12</artifactId>  
  72.             <version>${slf4j.version}</version>  
  73.         </dependency>  
  74.     <!-- dubbo -->  
  75.     <dependency>  
  76.       <groupId>com.alibaba</groupId>  
  77.       <artifactId>dubbo</artifactId>  
  78.       <version>2.5.3</version>  
  79.     </dependency>  
  80.     <!-- zkclient  -->  
  81.     <dependency>  
  82.       <groupId>com.github.sgroschupf</groupId>  
  83.       <artifactId>zkclient</artifactId>  
  84.       <version>0.1</version>  
  85.     </dependency>  
  86.     <!--  zookeeper -->  
  87.     <dependency>  
  88.       <groupId>org.apache.zookeeper</groupId>  
  89.       <artifactId>zookeeper</artifactId>  
  90.       <version>3.4.5</version>  
  91.     </dependency>  
  92.   </dependencies>  
  93.    
  94.    <build>    
  95.         <finalName>dubbo-demo</finalName>  
  96.         <plugins>    
  97.             <!-- 非多个资源配置 start-->  
  98.             <plugin>    
  99.                 <groupId>org.apache.maven.plugins</groupId>    
  100.                 <artifactId>maven-compiler-plugin</artifactId>    
  101.                 <version>2.1</version>    
  102.                 <configuration>    
  103.                     <source>1.5</source>    
  104.                     <target>1.5</target>    
  105.                     <encoding>UTF-8</encoding>    
  106.                     <failOnError>false</failOnError>    
  107.                 </configuration>    
  108.             </plugin>  
  109.             <!-- 非多个资源配置 end-->  
  110.         </plugins>    
  111.     </build>  
  112. </project>  

(3)、写服务接口,服务接口的实现



[java]  view plain  copy
  1. package com.test.dubboser;  
  2.   
  3. public interface ServiceDemo {  
  4. public String say(String str);  
  5. }  



[java]  view plain  copy
  1. package com.test.dubboser;  
  2.   
  3. public class ServiceImp implements ServiceDemo{  
  4.     public String say(String str) {  
  5.         System.err.println("server: "+str);  
  6.         return "hello: "+str;  
  7.     }  
  8. }  

(4)、向注册中心注册服务(将接口暴露出去),这里的注册中心是zookeeper


下面我们来看看,服务的配置也就是applicationProvider.xml这个配置文件中配置……


[html]  view plain  copy
  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.      <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->  
  11.     <dubbo:application name="hello-world-app-my" />  
  12.      <!-- 使用multicast广播注册中心暴露服务地址 -->  
  13.      <!-- <dubbo:registry address="multicast://192.168.0.101:2181" /> -->  
  14.      <!-- 使用zookeeper广播注册中心暴露服务地址 -->  
  15.     <dubbo:registry  protocol="zookeeper"  address="192.168.0.101:2181"/>  
  16.      <!--和上面的配置是一样的效果  -->  
  17.      <!-- <dubbo:registry address="zookeeper://192.168.0.101:2181" />  -->  
  18.      <!-- 本机 伪集群 测试 -->       
  19.      <!--  <dubbo:registry  protocol="zookeeper"  address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183"  /> -->  
  20.      <!-- 用dubbo协议在20880端口暴露服务 -->  
  21.     <dubbo:protocol name="dubbo" port="20880" />       
  22.      <!-- 声明需要暴露的服务接口 -->  
  23.     <dubbo:service interface="com.test.dubboser.ServiceDemo"  
  24.         ref="demoService" />   
  25.      <!-- 和本地bean一样实现服务 -->  
  26.     <bean id="demoService" class="com.test.dubboser.ServiceImp"/>  
  27. </beans>  

(5)、所有的准备都好了,下面我们启动服务提供者(提前启动zookeeper)



[java]  view plain  copy
  1. package com.test.dubboser;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. public class Main {  
  7. public static void main(String[] args) throws IOException {  
  8.     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });  
  9.     context.start();  
  10.     System.out.println("按任意键退出");  
  11.     System.in.read();  
  12. }  
  13. }  

ok这样我们的服务提供者就好了,是不是很乱,我感觉也有点,所以之后会有个自己的小总结……


我们的服务提供者好了,我们来看看我们的服务消费者,也就是调用我们这里的服务的程序……

愿意了解框架技术或者源码的朋友直接求求交流分享技术:2042849237
分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨
更多详细源码参考来源:http://minglisoft.cn/technology



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31452580/viewspace-2147017/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31452580/viewspace-2147017/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值