dubbo学习
2016-03-31 15:40
267 查看
Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点:
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
其核心部分包含:
远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
实例:
服务提供方:HuoDong
定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
Java代码
public interface DemoService {
public void sayHello();
}
在服务提供方实现接口:(对服务消费方隐藏实现)
Java代码
public class DemoServiceImpl implements DemoService{
@Override
public void sayHello() {
System.out.println("hello zy!");
}
}
用Spring配置声明暴露服务:
applicationProvider.xml:
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: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="hello-world-app" />
<dubbo:registry protocol="zookeeper" address="172.17.0.119:2181,172.17.0.120:2181,172.17.0.121:2181,172.17.0.122:2181,172.17.0.123:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="org.huodong.service.DemoService"
ref="demoService" /> <!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="org.huodong.service.DemoServiceImpl" />
</beans>
ps:这里是把服务器方和提供方都注册到了zookeeper上统一管理。上面的IP为安装了zookeeper的服务器地址。如果不想用zookeeper管理的话,可以改为
<dubbo:registry address="multicast://224.5.6.7:1234" />
服务消费者方 Fashion:
applicationConsumer.xml:
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: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="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="172.17.0.119:2181,172.17.0.120:2181,172.17.0.121:2181,172.17.0.122:2181,172.17.0.123:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="org.huodong.service.DemoService" />
</beans>
调用远程服务(ChatAction.java):
Java代码
public class ChatAction implements Controller {
private DemoService demoService;
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
demoService.sayHello(); //调用提供方的方法
return null;
}
public DemoService getDemoService() {
return demoService;
}
public void setDemoService(DemoService demoService) {
this.demoService = demoService;
}
}
注:需要的jar包,消费方和服务方都需要
然后把服务方的DemoService打成一个jar包,这里只定义了一个方法,如果定义了好多个service或者service的实现中需要用到辅助类或javabean的话,那么都要打包进去。
ps:在公司的zookeeper中通过页面http://172.17.0.121:8080/governance/services可以找到所有注册的服务,然后可以在这里设置每个服务所使用的提供者(IP地址等信息),一个服务可以设置好多提供者(比如一个是服务器的,一个是本地的),但是只能一个是启用状态,其余都是禁用状态。
ps: 如果通过http://172.17.0.121:8080/governance/services 页面可以看到既有提供者,又有消费者,但是连接不通的话,可以telnet一下对方,如果不通,说明是对方开了防火墙,需要把这个服务的端口放在白名单中,如果是linux系统的话需要修改/etc/sysconfig/iptables文件, 然后执行service
iptables restart
ps: 如果在zookper上部署过服务,现在又把此服务更新了版本,但是在控制台上却没有看到有新的相应方法,那么很有可能是部署提供者项目时在weblogic里有缓存,把原项目删除后重新部署一下就好了
那么,Dubbo是什么?
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。其核心部分包含:
远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
Dubbo能做什么?
透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
实例:
服务提供方:HuoDong
定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
Java代码
public interface DemoService {
public void sayHello();
}
在服务提供方实现接口:(对服务消费方隐藏实现)
Java代码
public class DemoServiceImpl implements DemoService{
@Override
public void sayHello() {
System.out.println("hello zy!");
}
}
用Spring配置声明暴露服务:
applicationProvider.xml:
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: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="hello-world-app" />
<dubbo:registry protocol="zookeeper" address="172.17.0.119:2181,172.17.0.120:2181,172.17.0.121:2181,172.17.0.122:2181,172.17.0.123:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="org.huodong.service.DemoService"
ref="demoService" /> <!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="org.huodong.service.DemoServiceImpl" />
</beans>
ps:这里是把服务器方和提供方都注册到了zookeeper上统一管理。上面的IP为安装了zookeeper的服务器地址。如果不想用zookeeper管理的话,可以改为
<dubbo:registry address="multicast://224.5.6.7:1234" />
服务消费者方 Fashion:
applicationConsumer.xml:
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: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="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="172.17.0.119:2181,172.17.0.120:2181,172.17.0.121:2181,172.17.0.122:2181,172.17.0.123:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="org.huodong.service.DemoService" />
</beans>
调用远程服务(ChatAction.java):
Java代码
public class ChatAction implements Controller {
private DemoService demoService;
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
demoService.sayHello(); //调用提供方的方法
return null;
}
public DemoService getDemoService() {
return demoService;
}
public void setDemoService(DemoService demoService) {
this.demoService = demoService;
}
}
注:需要的jar包,消费方和服务方都需要
然后把服务方的DemoService打成一个jar包,这里只定义了一个方法,如果定义了好多个service或者service的实现中需要用到辅助类或javabean的话,那么都要打包进去。
ps:在公司的zookeeper中通过页面http://172.17.0.121:8080/governance/services可以找到所有注册的服务,然后可以在这里设置每个服务所使用的提供者(IP地址等信息),一个服务可以设置好多提供者(比如一个是服务器的,一个是本地的),但是只能一个是启用状态,其余都是禁用状态。
ps: 如果通过http://172.17.0.121:8080/governance/services 页面可以看到既有提供者,又有消费者,但是连接不通的话,可以telnet一下对方,如果不通,说明是对方开了防火墙,需要把这个服务的端口放在白名单中,如果是linux系统的话需要修改/etc/sysconfig/iptables文件, 然后执行service
iptables restart
ps: 如果在zookper上部署过服务,现在又把此服务更新了版本,但是在控制台上却没有看到有新的相应方法,那么很有可能是部署提供者项目时在weblogic里有缓存,把原项目删除后重新部署一下就好了
相关文章推荐
- javaMail创建邮件和发送邮件总结
- Xcode 查找 TODO 清单
- 现代程序设计之网页前端开发作业
- 常见问题之加法操作符(读书笔记)
- 网络爬虫基本原理(一)
- 安卓下拉刷新开源库对比
- Java中 一个类实现了某接口 则必须实现该接口中的所有方法么?
- Linux centos 下在线安装mysql
- Git 文件忽略
- unity3d js 和web js的区别
- Hibernate4映射总结(2)——Annotation配置
- 使用github出现的问题
- base64转码工具类
- iOS开发-搜索栏UISearchBar和UISearchController
- 安卓下拉刷新开源库对比
- 多线程:简化自定义操作(模仿 SDWebImage)
- com.sun.mail.smtp.SMTPAddressFailedException: 550 Invalid User: "xxxxxxx@163.com"
- Android开源项目分类汇总(四)TextView、ScrollView、TimeView、TipView、FlipView
- iOS开发中地图开发的简单应用
- 获取SQL Server的安装时间