Category Archives: Architecture

关于集群的想法

上次参加TopLanguage聚会时,与土豆网的架构师聊天,当时想了解一下他们在使用J2EE时的集群部署是采用什么方式,得到的结果是他们不用集群。 这个结果让我很意外,然后他问我集群有什么好的?一台机器起多个JVM和一个JVM有什么区别? 当时我就无语了。 以前从来认真想过这个问题,在同一台服务器部署多个服务(JVM)组成的集群的作用?当然多台服务器形成的服务器集群几乎是肯定不需要的下面是我想到的一下优缺点。

各个服务采用集群部署的优点:

1、分开采用集群部署可以增加系统的鲁棒性,一个服务挂了之后,只有没有或者很少部分用户受影响。有的框架实现了Session复制,甚至可以做到在某个服务挂掉时,用户毫无感觉。目前所在的公司的框架也提供了session持久化及复制功能,但是大部分时候并没有使用该特性。
2、服务分开集群部署后,可以针对特定的服务进行水平扩展,比如app服务压力太大,可以通过再增加一个app服务来处理。当然这也要框架支持,当前大部分的框架应该都支持这种方式,公司内部的框架还支持向多个app服务发送请求时做简单的负载均衡。
3、服务分开部署便于管理。 一旦某个出了问题,通过日志和边界法可以很快定位到出现故障的服务,问题修复之后也只需要重启出问题的服务。如果是后台服务,用户根本感觉不到服务出了问题。

各个服务采用集群部署的缺点:
1、在同一台服务器上部署多个服务(JVM)会浪费大量内存。在一个集群中,每个JVM方法区中的数据有很大一部分是相同的,一台服务器上部署很多个JVM,会存在着很大的数据冗余,内存资源得不到充分的利用。
2、各个服务(JVM)之间的通信会显著增加性能开销。服务之间的采用TCP协议通信来交换数据比直接通过内存交换数据开销大太多了。因此如果服务边界定位不合理的话,会对系统性能造成巨大影响。根据”2-8法则”,只要对最消资料的服务进行水平扩展,在性能要求不是特别高的情况下,这条缺点不是大问题。
3、服务之间的通信增加了出错的可能性。服务(JVM)请求方通过TCP协议通信时,先要进行序列化,传输数据到服务接收方,服务接收方接收数据后行进行反序列化,处理完之后,又要将响应序列化返回请求方,这个过程增加了出错的可能性。

仔细思考了之后,还是觉得集群部署更好。但是要让集群更好的发挥优势,需要很好的定义各个服务的边界,部署太多的服务会浪费太多的资源。集群部署的另一个问题是各个服务的启动脚本不一致,有些人采用.bat或者.sh的方式来启动JVM,如果服务太多的话,难免遇到问题。目前所在公司里采用XML的方式来部署所有服务,通过一个python脚本解析这个XML来启动各个服务。从目前使用情况来看,这是个不错的方案。
由于从来没用过JBoss WebLogic等强大的应用服务器,不知道这些服务器采用什么方式来集群的,如果有人明白的话,可以和我留言,在此非常感谢。

Event-driven Architecture 初探

最近在看MINA的源码,里面的IO处理采用的是Event-driven的方式。 对于Event-driven虽然在公司的构架中用过,但是对其原理一直了解不深。 这次借这个机会读了不少相关的文献,算是有个比较清晰的认识了吧。但是对于Event-Driven方式为什么效率高,消耗资源少,仍然没有找到确切的答案。 Nginx也是采用Event-Driven 的方式来处理请求的,而取得了非常好的效果。

下面部分是从Wikipedia提取的信息,英文是原文,中文是我对这个的理解。
Event-driven Architecture
event a significant change in state

Event structure
An event can be made of two parts,header and body.The event header might include information such as event name, timestamp for the event, and type of event. The event body is the part that describes the fact that has happened in reality.
事件内部的数据组织结构,包括一些描述事情本身的信息(如名称,时间,类型,状态等)和需要进行业务处理的数据。

事件驱动构架的分层结构
Event flow layers
1.Event generator

用来生成 Event,生成一个包括当前fact的Event.需要将许多不同的应用程序产生的数据转换成标准化的数据是这一层设计和实现的一个难题。

2.Event channel
An event channel is a mechanism whereby the information from an event generator is transferred to the event engine or sink.
事件传播的一个渠道,实现方式可以有许多种。

Event processing engine 事件处理引擎
The event processing engine is where the event is identified, and the appropriate reaction is selected and executed.

事件处理引擎,接收事件,按照事件的类型来选择不同的处理策略,并处理事件。

downstream event-driven activity
This is where the consequences of the event are shown.

Event processing styles

Simple event processing

Simple event processing concerns events that are directly related to specific, measurable changes of condition.
这种方式是接收到事件后,根据事件的类型直接进行处理,也就是事件分类与处理放在一起处理。

the application has a main loop which is clearly divided down to two sections: the first is event selection (or event detection), and the second is event handling

这种方式把事件的分类与真正的业务处理分开。 事件分类可以看成是一个事件的分发器,将事件分发到相应的处理器上进行处理。 这种方式会有更好的效率,而且更易扩展。
下面图片来自http://eventdrivenpgm.sourceforge.net/

下面另外一个系列文章中整理出来的一些key points.
Architectures are composed of the following:
1. Components: The elements that form a system. For example, the components of a bridge are beams, cables, and trusses.
2. Compositional operators: The mechanisms for plugging components together to get other components. For example, smaller trusses can be welded together to form larger trusses.
3. Contracts: The specifications for what users of components can expect from them.

EDA has two components: streams and agents (also called processes). A stream is a sequence of messages that have a common schema. Streams are either event streams or control streams. Messages in event streams contain information about the system state.

There are three types of agents:
* Sensors a sensor is a source of data。
* Responders
* Processing agents (or EPAs for event processing agents)

Sensors monitor the environment and generate event messages that contain information about the attributes that they monitor.
A responder receives an event stream and modifies the state of the environment based on the stream.
One can think of responders in two ways: either as a software module that receives an event stream and controls a device based on the events it receives, or as the device itself.
Event-processing agents receive multiple streams of messages, process them, and generate message streams in turn

In animals, sensors are eyes and other sensory organs, the nervous system is a network of EPAs, and muscles are responders.

A key point in such an event-driven system is the absence of messages conveys information.

references:
http://www.developer.com/design/article.php/3490671
http://www.developer.com/design/article.php/10925_3499031_1
http://elementallinks.typepad.com/bmichelson/2006/02/eventdriven_arc.html