dubbo 初体验

我的原文地址 http://www.inspires.cn/note/27

 上一家公司使用阿里巴巴的DubboSOA的基础框架,并在其上做集成,由于之前环境都是架构师搭建的,所以我还不是很了解,只是会用,前几天心血来潮自己搭建了一个helloworld版,这里记录一下搭建过程。

         第一步,要选择dubbo的中间件,之前用的是zookeeper来做注册中心的,所以我这边也使用它来搭建注册中心,下载地址去apache的官网下载,可以戳我直接去到官网下载稳定版本。而后解压到本地文件夹,解压出来的结构如下

打开conf文件夹,copy zoo_sample.cfg副本,重命名为zoo.cfg,然后可以修改里面的内容,也可以不修改。

打开bin目录下的zkServer.cmd文件,如果报错,请设置好环境变量。

 

正常启动如下

注册中心就弄好了,

         第二步,创建service工程与其中的服务,本案例使用maven构建系统,怎么创建maven系统这边就不做赘述了,主要上下其中的代码

pom.xml

        

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
< project  xmlns = "http://maven.apache.org/POM/4.0.0"  xmlns:xsi=" 
  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.inspires</ groupId >
          < artifactId >dubbo-service</ artifactId >
          < version >0.0.1-SNAPSHOT</ version >
          < packaging >jar</ packaging >
  
          < name >dubbo-service</ name >
          < url >http://maven.apache.org</ url >
  
          < properties >
                    < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
          </ properties >
          < dependencies >
         
                    < dependency >
                             < groupId >junit</ groupId >
                             < artifactId >junit</ artifactId >
                             < version >4.12</ version >
                    </ dependency >
  
                    < dependency >
                             < groupId >org.springframework</ groupId >
                             < artifactId >spring-context</ artifactId >
                             < version >3.2.5.RELEASE</ version >
                    </ dependency >
  
                    < dependency >
                             < groupId >com.alibaba</ groupId >
                             < artifactId >dubbo</ artifactId >
                             < version >2.4.9</ version >
                    </ dependency >
  
                    < dependency >
                             < groupId >org.apache.zookeeper</ groupId >
                             < artifactId >zookeeper</ artifactId >
                             < version >3.4.6</ version >
                    </ dependency >
                    < dependency >
                             < groupId >com.101tec</ groupId >
                             < artifactId >zkclient</ artifactId >
                             < version >0.4</ version >
                    </ dependency >
          </ dependencies >
</ project >

然后创建一个spring的配置文件命名为service-dubbo.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<? 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 = "service-test-dubbo" />
          <!-- 使用multicast广播注册中心暴露服务地址 -->
          < dubbo:registry  address = "zookeeper://127.0.0.1:2181"  />
          <!-- 用dubbo协议在20880端口暴露服务 -->
          < dubbo:protocol  name = "dubbo"  port = "20880"  />
          <!-- 声明需要暴露的服务接口 -->
          < dubbo:service  interface = "com.inspires.dubbo.service.IDemoService"  ref = "demoService"  />
          <!-- 和本地bean一样实现服务 -->
          < bean  id = "demoService"  class = "com.inspires.dubbo.service.impl.DemoService"  />
</ beans >

接下来创建一个接口与实现类IDemoServiceDemoService,只创建一个sayHello方法以做校验

IDemoService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
  *
  */
package  com.inspires.dubbo.service;
  
/**
  * @author Jon Chiang
  * @project dubbo-service
  * @create_date 2014-12-30 下午5:06:52
  */
public  interface  IDemoService {
  
          /**
            * @author Jon Chiang
            * @create_date 2014-12-30 下午5:09:27
            * @param name
            * @return
            */
          String sayHello(String name);
}

DemoService.java 不多说

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
  *
  */
package  com.inspires.dubbo.service.impl;
  
import  com.inspires.dubbo.service.IDemoService;
  
/**
  * @author Jon Chiang
  * @project dubbo-service
  * @create_date 2014-12-30 下午5:07:29
  */
public  class  DemoService  implements  IDemoService {
          static  int  sayHelloCount =  0 ;
          @Override
          public  String sayHello(String name) {
                    String hello =  "Hello "  + name;
                    System.out.println(++sayHelloCount);
                    System.out.println(hello);
                    return  hello;
          }
}

还有web.xml里面要做spring配置文件和监听的配置,这里不做赘述(附件里面有,不会的同事可以参考附件)这里service工程基本弄好了

第三步,创建maven的访问者或者客户端工程。

pom.xml 注意要引用service工程,不然没法访问IDemoService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
< 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 >com.inspires</ groupId >
          < artifactId >dubbo-client</ artifactId >
          < packaging >war</ packaging >
          < version >0.0.1-SNAPSHOT</ version >
          < name >dubbo-client Maven Webapp</ name >
          < url >http://maven.apache.org</ url >
  
          < build >
                    < finalName >dubbo-client</ finalName >
          </ build >
          < dependencies >
                    < dependency >
                             < groupId >junit</ groupId >
                             < artifactId >junit</ artifactId >
                             < version >4.12</ version >
                    </ dependency >
  
                    < dependency >
                             < groupId >org.springframework</ groupId >
                             < artifactId >spring-context</ artifactId >
                             < version >3.2.5.RELEASE</ version >
                    </ dependency >
  
                    < dependency >
                             < groupId >com.alibaba</ groupId >
                             < artifactId >dubbo</ artifactId >
                             < version >2.4.9</ version >
                    </ dependency >
  
                    < dependency >
                             < groupId >org.apache.zookeeper</ groupId >
                             < artifactId >zookeeper</ artifactId >
                             < version >3.4.6</ version >
                    </ dependency >
                    < dependency >
                             < groupId >com.101tec</ groupId >
                             < artifactId >zkclient</ artifactId >
                             < version >0.4</ version >
                    </ dependency >
                    < dependency >
                             < groupId >com.inspires</ groupId >
                             < artifactId >dubbo-service</ artifactId >
                             < version >0.0.1-SNAPSHOT</ version >
                    </ dependency >
          </ dependencies >
</ project >

然后就是spring文件

client-dubbo.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<? 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 = "client-test-dubbo" />        <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
     < dubbo:registry  address = "zookeeper://127.0.0.1:2181"  />          <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
     < dubbo:reference  id = "demoService"  interface = "com.inspires.dubbo.service.IDemoService"  />
</ beans >
client-common.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:context = "http://www.springframework.org/schema/context"
          xmlns:tx = "http://www.springframework.org/schema/tx"  xmlns:util = "http://www.springframework.org/schema/util"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/util
          http://www.springframework.org/schema/util/spring-util-3.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
          default-lazy-init = "false" >
          < context:annotation-config  />
          < context:component-scan  base-package = "com.inspires.dubbo.service"  />   <!-- 自动扫描所有注解该路径 -->
</ beans >

让后我们在service里面写一个测试调用远程接口的方法。因为我这边只是测试接口是否通,所以使用@PostConstruct注解,让工程一启动就调用init方法访问接口,达到我测试的目的。

TestService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
  *
  */
package  com.inspires.dubbo.service.impl;
  
import  javax.annotation.PostConstruct;
  
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Service;
  
import  com.inspires.dubbo.service.IDemoService;
import  com.inspires.dubbo.service.ITestService;
  
/**
  * @author Jon Chiang
  * @project dubbo-client
  * @create_date 2014-12-30 下午6:54:12
  */
@Service
public  class  TestService  implements  ITestService{
  
          @Autowired
          IDemoService demoService;
         
          @PostConstruct
          @Override
          public  void  init(){
                    for  ( int  i =  0 ; i <  20 ; i++) {
                             demoService.sayHello( "Jon Chiang " );
                    }
                   
          }
}

然后可以开测了

启动service工程控制台无异常,然后启动client工程,系统调用远程方法。控制台答应hello Jon  Chiang 大功告成也!

附件为工程文件 戳我 下载

猜你喜欢

转载自inspires.iteye.com/blog/2173455