dubbo官网地址:http://dubbo.io/User+Guide-zh.htm
(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中加载:
<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 address="multicast://224.5.6.7:1234" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
<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
<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
<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.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
<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
<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");//执行远程方法
}
}