Dubbo (http://dubbo.io/)是阿里巴巴公司开源的一个高性能优秀的分布式服务框架,高性能和透明化的RPC远程服务调用方案 、 SOA服务治理方案、并且和 Spring框架无缝集成。
主要核心部件:
Remoting:网络通信框架,实现了 sync-over-async 和 request-response 消息机制。
RPC:一个远程过程调用的抽象,支持负载均衡、容灾和集群功能。
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。
本项目更新在GitHub:地址(https://github.com/AndyCZY/dubbo-first)
- 当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
- 此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键。
- 当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
- 此时,用于加速前端页面开发的 Web框架(MVC) 是关键。
- 当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
- 此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。
- 当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
- 此时,用于提高机器利用率的 资源调度和治理中心(SOA) 是关键。
在大规模服务化之前,应用可能只是通过RMI或Hessian等工具,简单的暴露和引用远程服务,通过配置服务的URL地址进行调用,通过F5等硬件进行负载均衡。
(1) 当服务越来越多时,服务URL配置管理变得非常困难,F5硬件负载均衡器的单点压力也越来越大。
此时需要一个服务注册中心,动态的注册和发现服务,使服务的位置透明。并通过在消费方获取服务提供方地址列表,实现软负载均衡和Failover,降低对F5硬件负载均衡器的依赖,也能减少部分成本。
(2) 当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系。这时,需要自动画出应用间的依赖关系图,以帮助架构师理清理关系。
(3) 接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?
为了解决这些问题,第一步,要将服务现在每天的调用量,响应时间,都统计出来,作为容量规划的参考指标。
其次,要可以动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程中记录响应时间的变化,直到响应时间到达阀值,记录此时的访问量,再以此访问量乘以机器数反推总容量。
A、透明化的远程方法调用 :就像调用本地方法一样调用远程方法 – 只需简单配置,没有任何API侵入。
B、软负载均衡及容错机制 :可在内网替代F5等硬件负载均衡器 。
C、服务自动注册与发现 :不再需要写死服务提供方地址,注册中心基于接口名查询服务提 供者的IP地址,并且能够平滑添加或删除服务提供者。
代码下载地址:https://github.com/AndyCZY/dubbo-first(以后代码更新就在GitHub)
package com.czy.dubbo.provider;
/**
* @auther 陈郑游
* @create 2017/3/25 0025
* @功能 定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
* @问题
* @说明
* @URL地址
* @进度描述
*/
public interface HelloService {
String sayHello(String name);
}
package com.czy.dubbo.provider.impl;
import com.czy.dubbo.provider.HelloService;
/**
* @auther 陈郑游
* @create 2017/3/25 0025
* @功能 在服务提供方实现接口:(对服务消费方隐藏实现)
* @问题
* @说明
* @URL地址
* @进度描述
*/
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "Hello " + name;
}
}
package com.czy.dubbo.consumer;
import com.czy.dubbo.provider.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @auther 陈郑游
* @create 2017/3/25 0025
* @功能 消费者
* @问题
* @说明
* @URL地址
* @进度描述
*/
public class ConsumerTest {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationConsumer.xml"});
context.start();
// 获取远程服务代理
HelloService helloService = (HelloService) context.getBean("HelloService");
// 执行远程方法
String hello = helloService.sayHello("ChenZhengYou!");
// 显示调用结果
System.out.println(hello);
//为保证服务一直开着,利用输入流的阻塞来模拟
System.in.read();
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @auther 陈郑游
* @create 2017/3/25 0025
* @功能 Main方法运行(Spring容器)
* @问题
* @说明
* @URL地址
* @进度描述
*/
public class DubboProviderTest {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationProvider.xml"});
// 按任意键退出
context.start();
System.out.println("Press any key to exit!");
//为保证服务一直开着,利用输入流的阻塞来模拟
System.in.read();
}
}
package com.czy.dubbo.consumer;
import com.czy.dubbo.provider.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @auther 陈郑游
* @create 2017/3/25 0025
* @功能 消费者
* @问题
* @说明
* @URL地址
* @进度描述
*/
public class ConsumerTest {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationConsumer.xml"});
context.start();
// 获取远程服务代理
HelloService helloService = (HelloService) context.getBean("HelloService");
// 执行远程方法
String hello = helloService.sayHello("ChenZhengYou!");
// 显示调用结果
System.out.println(hello);
//为保证服务一直开着,利用输入流的阻塞来模拟
System.in.read();
}
}
Archetype Created Web Application
index.html
index.jsp
log4jConfigLocation
classpath:log4j.properties
log4jLocation
6000
org.springframework.web.util.Log4jConfigListener
contextConfigLocation
classpath*:applicationProvider.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
15
dubbo的控制台也是跟上面一样!
项目代码地址:https://github.com/AndyCZY/dubbo-first/