标签:
介绍:
Dubbo是一个被国内很多互联网公司广泛使用的开源分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA
服务治理方案,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
其核心部分包含:
Dubbo能做什么?
扩展:
dubbox相当于dubbo的扩展,最主要Dubbox增加了如RESTful remoting,Kyro/FST 系列化等额外功能,dubbox和dubbo2.x是兼容的, 没有改变dubbo的任何已有的功能和配置方式(除了升级了spring之类的版本)。
配置:
服务端接口实现类:
public classDemoServiceImpl implements DemoService {
public String sayHello(Stringname) {
return "Hello "+ name;
}
}
服务端spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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:applicationname="hello-world-app" />
<!--使用multicast广播注册中心暴露服务地址-->
<dubbo:registryprotocal=“zookeeper”address="192.168.152.100:2181,192.168.152.101:2181,192.168.152.102:2181" />
<!--用dubbo协议在20880端口暴露服务 -->
<dubbo:protocolname="dubbo"port="20880" />
<!--声明需要暴露的服务接口 -->
<dubbo:serviceinterface="com.alibaba.dubbo.demo.DemoService"ref="demoService" />
<!--和本地bean一样实现服务可以改为注解式-->
<beanid="demoService"class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>
消费端spring配置:
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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:applicationname="consumer-of-helloworld-app" />
<!--使用multicast广播注册中心暴露发现服务地址-->
<dubbo:registryprotocal=“zookeeper”address="192.168.152.100:2181,192.168.152.101:2181,192.168.152.102:2181" />
<!--生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:referenceid="demoService"interface="com.alibaba.dubbo.demo.DemoService" />
</beans>
服务调用代码:
public class Consumer{
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext(new String[] {"consumer.xml"});
context.start();
DemoService demoService=(DemoService)context.getBean("demoService");//获取远程服务代理
String hello =demoService.sayHello("world");//执行远程方法
System.out.println(hello ); //显示调用结果
}
}
优势:
健状性
伸缩性
升级性
管理界面:
原理:
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:
0. 服务容器负责启动,加载,运行服务提供者。
1. 服务提供者在启动时,向注册中心注册自己提供的服务。
2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求
经验:
while (true) {
try{
Main.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
推荐第二种方法,因为第一种方法在使用nohup命令时无法正常启动
标签:
原文地址:http://blog.csdn.net/gui66497/article/details/52337398