dubbo学习
1、dubbo简介
背景:
a、单一应用架构:为了简化CURD,ORM框架是关键
b、垂直应用架构:为了加速前端页面的开发,MVC框架是关键
c、分布式服务器架构:垂直应用越来越多,应用之间交互不可避免,将核心业务抽出来,作为独立的服务,做紧啊形成稳定的服务中,使前端应用能快速的响应多变的市场需求。业务服用及整合的分布式服务框架(RPC)是关键。
d、流动计算架构:当服务越来越多,容量的评估,小服务器资源的浪费等问题逐渐显现,此时增加一个调度中心基于调度中心基于访问眼泪实时管理集群容量,提高集群的利用率。提高及其利用率的资源调度和治理中心(SOA)是关键。
2、dubbo架构
a、角色说明
Provider:暴露服务的服务提供方
Consumer:调用远程服务的服务消费者
Registry:服务注册与发现的注册中心
Monitor:统计服务的调用次调和调用时间的监控中心
Container:服务运行服务容器
b、调度关系
0. 服务容器负责启动,加载,运行服务提供者。
1. 服务提供者在启动时,向注册中心注册自己提供的服务。
2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
3、调度配置
服务者
package com.alibaba.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}
package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
配置
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">
消费者
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">
调用
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.demo.DemoService;
public class Consumer {
public static void main(String\[\] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String\[\] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}
}
4、配置参数
5、调用覆盖
a、上图中以timeout为例,显示了配置的查找顺序,其它retries, loadbalance, actives等类似。
方法级优先,接口级次之,全局配置再次之。
如果级别一样,则消费方优先,提供方次之。
b、 其中,服务提供方配置,通过URL经由注册中心传递给消费方。
c、建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚,如果一个消费方同时引用多个服务,就不需要关心每个服务的超时设置。
d、理论上ReferenceConfig的非服务标识配置,在ConsumerConfig,ServiceConfig, ProviderConfig均可以缺省配置。