dubbo官网地址:http://dubbo.io/User+Guide-zh.htm
dubbo的作用:一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
dubbo的基本需求(摘自官网):
(1)“当服务越来越多时,这些服务对应的URL的配置管理就会变得越来越困难,F5硬件负载均衡器的单点压力也越来越大”,”通过一个服务注册中心来动态的注册和发现服务,并通过在消费方获取服务提供方地址列表,实现软负载均衡和Failover,降低对F5硬件负载均衡器的依赖,也能减少部分成本。”
(2)“当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系。 这时,需要自动画出应用间的依赖关系图,以帮助架构师理清理关系。”
(3)“接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?
为了解决这些问题,第一步,要将服务现在每天的调用量,响应时间,都统计出来,作为容量规划的参考指标。
其次,要可以动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程中记录响应时间的变化,直到响应时间到达阀值,记录此时的访问量,再以此访问量乘以机器数反推总容量。”
节点角色:
Provider:暴露服务的服务提供方。
Consumer:调用远程服务的服务消费方。
Registry:服务注册与发现的注册中心。
Monitor:统计服务的调用次数和调用时间的监控中心。
Container:服务运行容器。
(安装方法已在另一篇博文中有详细描述)
使用:
首先你需要提供一个服务(提供者)
public interface IHelloRemoteService {
void hello(String name);
}
实现这个接口:
public class HelloRemoteService implements IHelloRemoteService {
@Override
public void hello(String name){
System.out.println(name+"say : hello");
}
}
在spring配置中声明暴露这个服务,你可以采用官网的配置方式,将配置写在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" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>
我是采用以下方式来配置,写一个dubbo的配置文件,我使用的是zookeeper注册中心,注册地址就是你安装dubbo的机器的IP:端口(默认为2181):
dubbo.properties
dubbo.container=log4j,spring
dubbo.application.name=test_provider
dubbo.application.owner=
dubbo.registry.address=zookeeper://192.168.0.37:2181
dubbo.monitor.protocol=registry
dubbo.provider.timeout=10000
dubbo.protocol.name=dubbo
dubbo.protocol.port=20883
dubbo.service.loadbalance=roundrobin
dubbo.log4j.file=C:/test/logs/test_provider/test_provider.log
dubbo.log4j.level=DEBUG
在你的spring配置中加载这些文件:
application-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:properties/*.properties" />
<property name="fileEncoding" value="UTF-8" />
</bean>
<!--其他配置省略-->
</beans>
你的提供者配置文件
application-dubbo-provider-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" 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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:service interface="com.andy.member.IHelloRemoteService" ref="helloRemoteService"/>
</beans>
之后就该配置你的消费者服务了
,需要的dubbo配置文件:dubbo.properties
dubbo.container=log4j,spring
dubbo.application.name=test_consumer
dubbo.application.owner=
dubbo.registry.address=zookeeper://192.168.0.37:2181
dubbo.monitor.protocol=registry
dubbo.provider.timeout=10000
dubbo.protocol.name=dubbo
dubbo.protocol.port=20882
dubbo.service.loadbalance=roundrobin
dubbo.log4j.file=C:/test/logs/test_consumer/test_consumer.log
dubbo.log4j.level=DEBUG
application-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:properties/*.properties" />
<property name="fileEncoding" value="UTF-8" />
</bean>
<!--省略其他配置-->
</beans>
消费者配置,你消费的接口名称当然要与提供的名称一致,否则它会找不到对应的提供者
application-dubbo-consumer-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" 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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:reference id="helloRemoteService" interface="com.andy.member.IHelloRemoteService" />
</beans>
接下来就可以调用远程服务:
@Controller
public class TestController{
@Autowired
private IHelloRemotrService helloRemoteService;
public static void main(String[] args){
helloRemoteService.hello("andy");//执行远程方法
}
}
为什么叫执行远程方法
,因为这时你的提供者接口在a服务器上,你的dubbo安装在b服务器上,你的消费者服务在c服务器上,通过dubbo来进行一个调度管理。
在本例中,zookeeper充当了Registry的角色,Monitor采用的是dubbo提供的dubbo-simple-monitor,服务运行容器Container是tomcat。
1.首先我们在服务运行容器上启动提供者服务。
2.服务提供者在启动时,向注册中心注册自己提供的服务。
3.服务消费者在启动时,向注册中心订阅自己所需的服务。
4.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
5.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
6.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。