我的原文地址 http://www.inspires.cn/note/27
上一家公司使用阿里巴巴的Dubbo做SOA的基础框架,并在其上做集成,由于之前环境都是架构师搭建的,所以我还不是很了解,只是会用,前几天心血来潮自己搭建了一个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 >
|
接下来创建一个接口与实现类IDemoService、DemoService,只创建一个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 大功告成也!
附件为工程文件 戳我 下载
相关推荐
NULL 博文链接:https://mitchz.iteye.com/blog/2019890
dubbo资源 dubbo-admin dubbo demo
dubbo示例代码dubbo-sample
incubator-dubbo-dubbo-2.6.1
dubbo资源包
本套视频从分布式系统的基本概念出发,由浅入深,讲解了RPC原理,Dubbo基本使用,Dubbo高可用场景以及Dubbo原理,涉及了分布式系统中服务注册、服务发现、负载均衡、灰度发布、集群容错、服务降级等核心概念的讲解及...
前段时间排查某问题的时候,想要快速知道某些dubbo接口(三无)的响应结果,但不想启动项目(因为这些项目不是你负责的,不会部署而且超级笨重),也不想新建一个dubbo客户端项目(占地方),也不想开telnet客户端...
Dubbo是阿里巴巴开源的分布式服务化治理框架(微服务框架),久经阿里巴巴电商平台的大规模复杂业务的高并发考验,到目前为止Dubbo仍然是开源界中体系最完善的服务化治理框架,因此Dubbo被国内大量的的互联网公司和...
dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.dubbo源码解析2.
首先,从知识层面对Dubbo有一个了解和认识,请看《Dubbo培训与实战.pptx》,然后把Dubbo用到实际项目中来,请看实例代码《Dubbo实例代码(Sping+Dubbo+Maven).zip》,里面包括dubboDemoProvide和dubboDemoConsumer...
Dubbo监控系统配置Dubbo监控系统配置Dubbo监控系统配置
【BAT必备】dubbo面试题【BAT必备】dubbo面试题【BAT必备】dubbo面试题【BAT必备】dubbo面试题【BAT必备】dubbo面试题【BAT必备】dubbo面试题【BAT必备】dubbo面试题【BAT必备】dubbo面试题【BAT必备】dubbo面试题...
dubbo+zookeeper缓存方案 dubbo+zookeeper缓存方案dubbo+zookeeper缓存方案dubbo+zookeeper缓存方案
解决dubbo接口自定义异常的捕捉问题,dubbo消费者可以捕捉到提供者所抛出的自定义异常。
spring_dubbo spring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_...
dubbo-admin dubbo管理台
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.returnFromResponse(DefaultFuture.java:190) at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:110) at ...
Dubbo入门_实战 使用dubbo优化单点登录系统、 Dubbo的架构 监控中心
Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配置就能够实现...
构建dubbo项目的时候会遇到: Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - ...