dubbo的学习
2016-08-08 15:28
232 查看
http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%E8%83%8C%E6%99%AF
Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的rpc(远程过程调用协议)远程服务调用方式,以及soa服务治理方案。(告别webservice的模式的wsdl,服务者与消费者的方式在Dubbo上注册)
Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和
Spring框架无缝集成.
Provider
暴露服务方称之为“服务提供者”。
Consumer
调用远程服务方称之为“服务消费者”。
Registry
服务注册与发现的中心目录服务称之为“服务注册中心”。
Monitor
统计服务的调用次调和调用时间的日志服务称之为“服务监控中心”。
· 1.服务提供者在启动时,向注册中心注册自己提供的服务。
· 2.服务消费者在启动时,向注册中心订阅自己所需的服务。
· 3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
· 4.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
· 5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
第三:Dubbo与Zookeeper、SpringMVC整合使用
第一步:在Linux上安装Zookeeper
Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求。我们先在linux上安装Zookeeper,我们安装最简单的单点,集群比较麻烦。
(1)下载Zookeeper-3.4.6.tar.gz 地址http://www.apache.org/dist/zookeeper/
(2) 我们放到Linux下的一个文件夹,然后解压:
#tar zxvf zookeeper-3.4.6.tar.gz
(3)然后在对应的zookeeper-3.4.6/conf 下有一个文件zoo_sample.cfg的这个文件里面配置了监听客户端连接的端口等一些信息,Zookeeper 在启动时会找zoo.cfg这个文件作为默认配置文件,所以我们复制一个名称为zoo.cfg的文件
说明:
clientPort:监听客户端连接的端口。
tickTime:基本事件单元,以毫秒为单位。它用来控制心跳和超时,默认情况下最小的会话超时时间为两倍的 tickTime。
我们可以对配置文件的端口等或者进行高级配置和集群配置例如:maxClientCnxns:限制连接到 ZooKeeper 的客户端的数量等
(4)启动Zookeeper 的服务
到这边Zookeeper的安装和配置完成
第二步:配置dubbo-admin的管理页面,方便我们管理页面
(1)下载dubbo-admin-2.4.1.war包,在Linux的tomcat部署,先把dubbo-admin-2.4.1放在tomcat的webapps/ROOT下,然后进行解压:
#jar -xvf dubbo-admin-2.4.1.war
(2)然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心
(3)然后启动tomcat服务,用户名和密码:root,并访问服务,显示登陆页面,说明dubbo-admin部署成功
第三步:SpringMVC与Dubbo的整合,这边使用的Maven的管理项目
第一:我们先开发服务注册的,就是提供服务
(1)test-maven-api项目加入了一个服务接口,代码如下:
public interface TestRegistryService {
public String hello(String name);
}
(2)test-maven-console在pom.xml加入Dubbo和Zookeeper的jar包、引用test-maven-api的jar包,代码如下:
<dependency>
<groupId>cn.test</groupId>
<artifactId>test-maven-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
(3)test-maven-console实现具体的服务,代码如下:
@Service("testRegistryService")
public class TestRegistryServiceImpl implements TestRegistryService {
public String hello(String name) {
return "hello"+name;
}
}
(4)我们服务以及实现好了,这时要暴露服务,代码如下:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
<span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span>
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" >
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="dubbo_provider"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" </dubbo:registry>
<!-- 要暴露的服务接口 -->
<dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" />
</beans>
说明:
dubbo:registry 标签一些属性的说明:
1)register是否向此注册中心注册服务,如果设为false,将只订阅,不注册。
2)check注册中心不存在时,是否报错。
3)subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅。
4)timeout注册中心请求超时时间(毫秒)。
5)address可以Zookeeper集群配置,地址可以多个以逗号隔开等。
dubbo:service标签的一些属性说明:
1)interface服务接口的路径
2)ref引用对应的实现类的Bean的ID
3)registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A
4)register 默认true ,该协议的服务是否注册到注册中心。
(5)启动项目,然后我们在Dubbo管理页面上显示
第二:我们在开发服务消费者,就是调用服务
(1)test-maven-server-console的pom.xml引入Dubbo和Zookeeper的jar包、test-maven-api的jar包,因为引入test-maven-api的jar包,我们在项目中调用像在本地调用一样。代码如下:
<dependency>
<groupId>cn.test</groupId>
<artifactId>test-maven-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
(2)test-maven-server-console项目的具体实现,代码如下:
@Controller
public class IndexController {
@Autowired
private TestRegistryService testRegistryService;
@RequestMapping("/hello")
public String index(Model model){
String name=testRegistryService.hello("zz");
System.out.println("xx=="+name);
return "";
}
}
(3)我们要引用的地址,代码如下:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
<span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span>
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" >
<dubbo:application name="dubbo_consumer"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://ip:2181" check="false"></dubbo:registry>
<!-- 要引用的服务 -->
<dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference>
</beans>
说明:
dubbo:reference 的一些属性的说明:
1)interface调用的服务接口
2)check 启动时检查提供者是否存在,true报错,false忽略
3)registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔
4)loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用
Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的rpc(远程过程调用协议)远程服务调用方式,以及soa服务治理方案。(告别webservice的模式的wsdl,服务者与消费者的方式在Dubbo上注册)
Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和
Spring框架无缝集成.
Dubbo工作原理:
Provider
暴露服务方称之为“服务提供者”。
Consumer
调用远程服务方称之为“服务消费者”。
Registry
服务注册与发现的中心目录服务称之为“服务注册中心”。
Monitor
统计服务的调用次调和调用时间的日志服务称之为“服务监控中心”。
调用关系说明:
· 0.服务容器负责启动,加载,运行服务提供者。· 1.服务提供者在启动时,向注册中心注册自己提供的服务。
· 2.服务消费者在启动时,向注册中心订阅自己所需的服务。
· 3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
· 4.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
· 5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
第三:Dubbo与Zookeeper、SpringMVC整合使用
第一步:在Linux上安装Zookeeper
Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求。我们先在linux上安装Zookeeper,我们安装最简单的单点,集群比较麻烦。
(1)下载Zookeeper-3.4.6.tar.gz 地址http://www.apache.org/dist/zookeeper/
(2) 我们放到Linux下的一个文件夹,然后解压:
#tar zxvf zookeeper-3.4.6.tar.gz
(3)然后在对应的zookeeper-3.4.6/conf 下有一个文件zoo_sample.cfg的这个文件里面配置了监听客户端连接的端口等一些信息,Zookeeper 在启动时会找zoo.cfg这个文件作为默认配置文件,所以我们复制一个名称为zoo.cfg的文件
说明:
clientPort:监听客户端连接的端口。
tickTime:基本事件单元,以毫秒为单位。它用来控制心跳和超时,默认情况下最小的会话超时时间为两倍的 tickTime。
我们可以对配置文件的端口等或者进行高级配置和集群配置例如:maxClientCnxns:限制连接到 ZooKeeper 的客户端的数量等
(4)启动Zookeeper 的服务
到这边Zookeeper的安装和配置完成
第二步:配置dubbo-admin的管理页面,方便我们管理页面
(1)下载dubbo-admin-2.4.1.war包,在Linux的tomcat部署,先把dubbo-admin-2.4.1放在tomcat的webapps/ROOT下,然后进行解压:
#jar -xvf dubbo-admin-2.4.1.war
(2)然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心
(3)然后启动tomcat服务,用户名和密码:root,并访问服务,显示登陆页面,说明dubbo-admin部署成功
第三步:SpringMVC与Dubbo的整合,这边使用的Maven的管理项目
第一:我们先开发服务注册的,就是提供服务
(1)test-maven-api项目加入了一个服务接口,代码如下:
public interface TestRegistryService {
public String hello(String name);
}
(2)test-maven-console在pom.xml加入Dubbo和Zookeeper的jar包、引用test-maven-api的jar包,代码如下:
<dependency>
<groupId>cn.test</groupId>
<artifactId>test-maven-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
(3)test-maven-console实现具体的服务,代码如下:
@Service("testRegistryService")
public class TestRegistryServiceImpl implements TestRegistryService {
public String hello(String name) {
return "hello"+name;
}
}
(4)我们服务以及实现好了,这时要暴露服务,代码如下:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
<span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span>
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" >
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="dubbo_provider"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" </dubbo:registry>
<!-- 要暴露的服务接口 -->
<dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" />
</beans>
说明:
dubbo:registry 标签一些属性的说明:
1)register是否向此注册中心注册服务,如果设为false,将只订阅,不注册。
2)check注册中心不存在时,是否报错。
3)subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅。
4)timeout注册中心请求超时时间(毫秒)。
5)address可以Zookeeper集群配置,地址可以多个以逗号隔开等。
dubbo:service标签的一些属性说明:
1)interface服务接口的路径
2)ref引用对应的实现类的Bean的ID
3)registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A
4)register 默认true ,该协议的服务是否注册到注册中心。
(5)启动项目,然后我们在Dubbo管理页面上显示
第二:我们在开发服务消费者,就是调用服务
(1)test-maven-server-console的pom.xml引入Dubbo和Zookeeper的jar包、test-maven-api的jar包,因为引入test-maven-api的jar包,我们在项目中调用像在本地调用一样。代码如下:
<dependency>
<groupId>cn.test</groupId>
<artifactId>test-maven-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
(2)test-maven-server-console项目的具体实现,代码如下:
@Controller
public class IndexController {
@Autowired
private TestRegistryService testRegistryService;
@RequestMapping("/hello")
public String index(Model model){
String name=testRegistryService.hello("zz");
System.out.println("xx=="+name);
return "";
}
}
(3)我们要引用的地址,代码如下:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
<span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span>
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" >
<dubbo:application name="dubbo_consumer"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://ip:2181" check="false"></dubbo:registry>
<!-- 要引用的服务 -->
<dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference>
</beans>
说明:
dubbo:reference 的一些属性的说明:
1)interface调用的服务接口
2)check 启动时检查提供者是否存在,true报错,false忽略
3)registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔
4)loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用
相关文章推荐
- dubbo学习之dubbo管理控制台装配及集成zookeeper集群部署(1)
- webservice学习-------Dubbo
- dubbo 学习笔记 -- consumer端
- dubbo学习(二)-dubbo与spring的整合及启动顺序
- dubbo源码学习专题
- dubbo学习(三)-dubbo的服务发布
- Dubbo学习二之简单示例
- Dubbo学习之一
- dubbo学习(一)-dubbo基于SPI思想的实现
- dubbo 学习笔记 -- consumer端
- dubbo 学习笔记 -- provider端
- Dubbo学习入门(一)
- dubbo 学习笔记 -- provider端
- dubbo-RPC学习(二) http protocol
- dubbo 学习笔记 -- provider端
- 服务化的时代已经来临:学习dubbo有感
- dubbo 学习笔记 -- consumer端
- 如何更好地学习dubbo源代码
- dubbo学习笔记
- dubbo 学习