基于Dubbo的分布式服务

Dubbo简介


Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东西,其实就是个远程服务调用的分布式框架(告别Web Service模式中的WSdl,以服务者与消费者的方式在dubbo上注册并调用)。

其核心部分包含:

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

Dubbo使用


注册中心:Dubbo将注册中心进行抽象,使得它可以外接不同的存储媒介给注册中心提供服务,有ZooKeeperMemcachedRedis等。

这里将采用zookeeper的方式作为注册中心。

接口和实现分离:为了达到分布式服务的作用,下面将会对接口和服务分别建立不同的项目,然后在实现处引用接口。

定义分布式服务

建立一个TestAttach-api的项目作为接口的定义。

接口的定义如下:

1
2
3
4
5
package com.test.attachment;
public interface Attachment {
String getAttachmentById (String Id);
}

实现分布式服务


建立一个TestAttach-service的项目作为接口的实现,提供服务。

写一个实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.test.attachment.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.test.attachment.Attachment;
@Service
public class AttachmentImpl implements Attachment {
@Override
public String getAttachmentById (String Id){
return "Attachment: " + Id;
}
}


建立三个dubbo的配置文件,放在stc/main/resources资源文件夹

dubbo.properties

1
2
3
dubbo.container=spring
dubbo.shutdown.hook=true
dubbo.spring.config=spring.xml


env.properties

1
2
3
4
dubbo.application.name=TestAttach-service
dubbo.application.port=8889
dubbo.registry.address=zookeeper://198.11.180.44:2181?backup=198.11.180.44:2182,198.11.180.44:2183
dubbo.registry.timeout=10000


spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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: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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:property-placeholder location="env.properties" />
<dubbo:application name="${dubbo.application.name}" />
<dubbo:registry address="${dubbo.registry.address}" timeout="${dubbo.registry.timeout}" />
<dubbo:protocol name="dubbo" server="netty"/>
<dubbo:provider id="default" port="${dubbo.application.port}" protocol="dubbo" />
<dubbo:monitor protocol="registry" />
<dubbo:annotation package="com.test.attachment" />
</beans>


Maven的配置中加入对API的引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.test.attachment</groupId>
<artifactId>TestAttach-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
</dependencies>


写一个服务的启动类

1
2
3
4
5
6
7
package com.test.attachment;
public class StartDebug {
public static void main(String[] args) {
com.alibaba.dubbo.container.Main.main(args);
}
}


运行StartDebug,启动服务

1
2
3
log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
[2015-12-16 14:14:30] Dubbo service server started!


客户端测试

建立一个TestAttach-client的项目作为服务的测试客户端。

建立dubbo的配置文件,放在stc/main/resources资源文件夹

dubbo.properties

1
2
3
4
5
6
7
dubbo.container=spring
dubbo.shutdown.hook=true
dubbo.spring.config=spring.xml
dubbo.application.name=TestAttach-client
dubbo.registry.address=zookeeper://198.11.180.44:2181?backup=198.11.180.44:2182,198.11.180.44:2183
dubbo.registry.timeout=10000

spring.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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:monitor protocol="registry" />
<dubbo:annotation package="com.test.attachment" />
</beans>

Maven配置和上面服务实现类同

写一个测试类

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
package com.test.attachment.client;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.test.attachment.Attachment;
@Service
public class AttachmentClientDemo implements InitializingBean {
@Reference
private Attachment test;
public Attachment getTest(){
return test;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(getTest().getAttachmentById("3"));
System.in.read();//等待输入,使得客户端不退出
}
public static void main(String[] args) {
com.alibaba.dubbo.container.Main.main(args);
}
}

启动测试类,调用远程服务,输出结果如下:

1
2
3
log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
Attachment: 3

Dubbo Admin


现在可以方便在Dubbo控制台上看到我们服务的提供者和消费者了(见参考

参考


http://dubbo.io/Dubbo官方网站

Dubbo源码Dubbo的Github地址

Dubbo扩展当当网开源Dubbox,扩展Dubbo服务框架支持REST风格远程调用

Dubbo控制台Dubbo控制台项目包下载

Dubbo控制台报错解决方案Dubbo控制台项目和JDK兼容问题解决方案