前言
saga单词翻译过来是指尤指古代挪威或冰岛讲述冒险经历和英雄业绩的长篇故事,对,这里强调长篇故事。许多系统都存在长时间运行的业务流程,nservicebus使用基于事件驱动的体系结构将容错性和可伸缩性融入这些业务处理过程中。
当然一个单一接口调用则算不上一个长时间运行的业务场景,那么如果在给定的用例中有两个或多个调用,则应该考虑数据一致性的问题,这里有可能第一个接口调用成功,第二次调用则可能失败或者超时,saga的设计以简单而健壮的方式处理这样的业务用例。
认识saga
先来通过一段代码简单认识一下saga,在nservicebus里,使用saga的话则需要实现抽象类saga ,sqlsaga ,这里的t的是saga业务实体,封装数据,用来在长时间运行过程中封装业务数据。
public class saga:saga<state>, iamstartedbymessages<startorder>, ihandlemessages<completeorder> { protected override void configurehowtofindsaga(sagapropertymapper<state> mapper) { mapper.configuremapping<startorder>(message=>message.orderid).tosaga(saga=>saga.orderid); mapper.configuremapping<completeorder>(message=>message.orderid).tosaga(saga=>saga.orderid); } public task handle(startorder message, imessagehandlercontext context) { return task.completedtask; } public task handle(completeorder message, imessagehandlercontext context) { markascomplete(); return task.completedtask; } }
临时状态
长时间运行则意味着有状态,任何涉及多个网络调用的进程都需要一个临时状态,这个临时状态可以存储在内存中,序列化在磁盘中,也可以存储在分布式缓存中。在nservicebus中我们定义实体,继承抽象类containsagadata即可,默认情况下,所有公开访问的属性都会被持久化。
public class state:containsagadata { public guid orderid { get; set; } }
添加行为
在nservicebus里,处理消息的有两种接口:ihandlermessages 、iamstartedbymessages 。
开启一个saga
在前面的代码片段里,我们看到已经实现了接口iamstartedbymessages ,这个接口告诉nservicebus,如果收到了startorder 消息,则创建一个saga实例(saga instance),当然saga长流程处理的实体至少有一个需要开启saga流程。
处理无序消息
如果你的业务用例中确实存在无序消息的情况,则还需要业务流程正常轮转,那么则需要多个messaeg都要事先接口iamstartedbymessages接口,也就是说多个message都可以创建saga实例。
依赖可恢复性
在处理无序消息和多个消息类型的时候,就存在消息丢失的可能,必须在你的saga状态完成以后,这个saga实例又收到一条消息,但这时saga状态已经是完结状态,这条消息则仍然需要处理,这里则实现nservicebus的ihandlesaganotfound接口。
public class saganotfoundhandler:ihandlesaganotfound { public task handle(object message, imessageprocessingcontext context) { return context.reply(new saganotfoundmessage()); } } public class saganotfoundmessage { }
结束saga
当你的业务用例不再需要saga实例时,则调用markcomplete()来结束saga实例。这个方法在前面的代码片段中也可以看到,其实本质也就是设置saga.complete属性,这是个bool值,你在业务用例中也可以用此值来判断saga流程是否结束。
namespace nservicebus { using system; using system.threading.tasks; using extensibility; public abstract class saga { /// <summary> /// the saga's typed data. /// </summary> public icontainsagadata entity { get; set; } public bool completed { get; private set; } internal protected abstract void configurehowtofindsaga(iconfigurehowtofindsagawithmessage sagamessagefindingconfiguration); protected task requesttimeout<ttimeoutmessagetype>(imessagehandlercontext context, datetime at) where ttimeoutmessagetype : new() { return requesttimeout(context, at, new ttimeoutmessagetype()); } protected task requesttimeout<ttimeoutmessagetype>(imessagehandlercontext context, datetime at, ttimeoutmessagetype timeoutmessage) { if (at.kind == datetimekind.unspecified) { throw new invalidoperationexception("kind property of datetime 'at' must be specified."); } verifysagacanhandletimeout(timeoutmessage); var options = new sendoptions(); options.donotdeliverbefore(at); options.routetothisendpoint(); settimeoutheaders(options); return context.send(timeoutmessage, options); } protected task requesttimeout<ttimeoutmessagetype>(imessagehandlercontext context, timespan within) where ttimeoutmessagetype : new() { return requesttimeout(context, within, new ttimeoutmessagetype()); } protected task requesttimeout<ttimeoutmessagetype>(imessagehandlercontext context, timespan within, ttimeoutmessagetype timeoutmessage) { verifysagacanhandletimeout(timeoutmessage); var sendoptions = new sendoptions(); sendoptions.delaydeliverywith(within); sendoptions.routetothisendpoint(); settimeoutheaders(sendoptions); return context.send(timeoutmessage, sendoptions); } protected task replytooriginator(imessagehandlercontext context, object message) { if (string.isnullorempty(entity.originator)) { throw new exception("entity.originator cannot be null. perhaps the sender is a sendonly endpoint."); } var options = new replyoptions(); options.setdestination(entity.originator); context.extensions.set(new attachcorrelationidbehavior.state { customcorrelationid = entity.originalmessageid }); options.context.set(new populateautocorrelationheadersforrepliesbehavior.state { sagatypetouse = null, sagaidtouse = null }); return context.reply(message, options); } //这个方法结束saga流程,标记completed属性 protected void markascomplete() { completed = true; } void verifysagacanhandletimeout<ttimeoutmessagetype>(ttimeoutmessagetype timeoutmessage) { var canhandletimeoutmessage = this is ihandletimeouts<ttimeoutmessagetype>; if (!canhandletimeoutmessage) { var message = $"the type '{gettype().name}' cannot request timeouts for '{timeoutmessage}' because it does not implement 'ihandletimeouts<{typeof(ttimeoutmessagetype).fullname}>'"; throw new exception(message); } } void settimeoutheaders(extendableoptions options) { options.setheader(headers.sagaid, entity.id.tostring()); options.setheader(headers.issagatimeoutmessage, bool.truestring); options.setheader(headers.sagatype, gettype().assemblyqualifiedname); } } }
saga持久化
本机开发环境我们使用learningpersistence,但是投产的话则需要使用数据库持久化,这里我们基于mysql,sql持久化需要引入nservicebus.persistence.sql。sql persistence会生成几种关系型数据库的sql scripts,然后会根据你的断言配置选择所需数据库,比如sql server、mysql、postgresql、oracle。
持久化saga自动创建所需表结构,你只需手动配置即可,配置后编译成功后项目执行目录下会生成sql脚本,文件夹名称是nservicebus.persistence.sql,下面会有saga子目录。
/* tablenamevariable */ set @tablenamequoted = concat('`', @tableprefix, 'saga`'); set @tablenamenonquoted = concat(@tableprefix, 'saga'); /* initialize */ drop procedure if exists sqlpersistence_raiseerror; create procedure sqlpersistence_raiseerror(message varchar(256)) begin signal sqlstate 'error' set message_text = message, mysql_errno = '45000'; end; /* createtable */ set @createtable = concat(' create table if not exists ', @tablenamequoted, '( id varchar(38) not null, metadata json not null, data json not null, persistenceversion varchar(23) not null, sagatypeversion varchar(23) not null, concurrency int not null, primary key (id) ) default charset=ascii; '); prepare script from @createtable; execute script; deallocate prepare script; /* addproperty orderid */ select count(*) into @exist from information_schema.columns where table_schema = database() and column_name = 'correlation_orderid' and table_name = @tablenamenonquoted; set @query = if( @exist <= 0, concat('alter table ', @tablenamequoted, ' add column correlation_orderid varchar(38) character set ascii'), 'select \'column exists\' status'); prepare script from @query; execute script; deallocate prepare script; /* verifycolumntype guid */ set @column_type_orderid = ( select concat(column_type,' character set ', character_set_name) from information_schema.columns where table_schema = database() and table_name = @tablenamenonquoted and column_name = 'correlation_orderid' ); set @query = if( @column_type_orderid <> 'varchar(38) character set ascii', 'call sqlpersistence_raiseerror(concat(\'incorrect data type for correlation_orderid. expected varchar(38) character set ascii got \', @column_type_orderid, \'.\'));', 'select \'column type ok\' status'); prepare script from @query; execute script; deallocate prepare script; /* writecreateindex orderid */ select count(*) into @exist from information_schema.statistics where table_schema = database() and index_name = 'index_correlation_orderid' and table_name = @tablenamenonquoted; set @query = if( @exist <= 0, concat('create unique index index_correlation_orderid on ', @tablenamequoted, '(correlation_orderid)'), 'select \'index exists\' status'); prepare script from @query; execute script; deallocate prepare script; /* purgeobsoleteindex */ select concat('drop index ', index_name, ' on ', @tablenamequoted, ';') from information_schema.statistics where table_schema = database() and table_name = @tablenamenonquoted and index_name like 'index_correlation_%' and index_name <> 'index_correlation_orderid' and table_schema = database() into @dropindexquery; select if ( @dropindexquery is not null, @dropindexquery, 'select ''no index to delete'';') into @dropindexquery; prepare script from @dropindexquery; execute script; deallocate prepare script; /* purgeobsoleteproperties */ select concat('alter table ', table_name, ' drop column ', column_name, ';') from information_schema.columns where table_schema = database() and table_name = @tablenamenonquoted and column_name like 'correlation_%' and column_name <> 'correlation_orderid' into @droppropertiesquery; select if ( @droppropertiesquery is not null, @droppropertiesquery, 'select ''no property to delete'';') into @droppropertiesquery; prepare script from @droppropertiesquery; execute script; deallocate prepare script; /* completesagascript */
生成的表结构:
持久化配置
saga持久化需要依赖nservicebus.persistence.sql。引入后需要实现sqlsaga抽象类,抽象类需要重写configuremapping,配置saga工作流程业务主键。
public class saga:sqlsaga<state>, iamstartedbymessages<startorder> { protected override void configuremapping(imessagepropertymapper mapper) { mapper.configuremapping<startorder>(message=>message.orderid); } protected override string correlationpropertyname => nameof(startorder.orderid); public task handle(startorder message, imessagehandlercontext context) { console.writeline($"receive message with orderid:{message.orderid}"); markascomplete(); return task.completedtask; } } static async task mainasync() { console.title = "client-ui"; var configuration = new endpointconfiguration("client-ui"); //这个方法开启自动建表、自动创建rabbitmq队列 configuration.enableinstallers(); configuration.useserialization<newtonsoftserializer>(); configuration.usetransport<learningtransport>(); string connectionstring = "server=127.0.0.1;uid=root;pwd=000000;database=nservicebus;port=3306;allowuservariables=true;autoenlist=false"; var persistence = configuration.usepersistence<sqlpersistence>(); persistence.sqldialect<sqldialect.mysql>(); //配置mysql连接串 persistence.connectionbuilder(()=>new mysqlconnection(connectionstring)); var instance = await endpoint.start(configuration).configureawait(false); var command = new startorder() { orderid = guid.newguid() }; await instance.sendlocal(command).configureawait(false); console.readkey(); await instance.stop().configureawait(false); }
saga timeouts
在消息驱动类型的环境中,虽然传递的无连接特性可以防止在线等待过程中消耗资源,但是毕竟等待时间需要有一个上线。在nservicebus里已经提供了timeout方法,我们只需订阅即可,可以在你的handle方法中根据需要订阅timeout,可参考如下代码:
public class saga:saga<state>, iamstartedbymessages<startorder>, ihandlemessages<completeorder>, ihandletimeouts<timeoutmessage> { public task handle(startorder message, imessagehandlercontext context) { var model=new timeoutmessage(); //订阅超时消息 return requesttimeout(context,timespan.fromminutes(10)); } public task handle(completeorder message, imessagehandlercontext context) { markascomplete(); return task.completedtask; } protected override string correlationpropertyname => nameof(startorder.orderid); public task timeout(timeoutmessage state, imessagehandlercontext context) { //处理超时消息 } protected override void configurehowtofindsaga(sagapropertymapper<state> mapper) { mapper.configuremapping<startorder>(message=>message.orderid).tosaga(saga=>saga.orderid); mapper.configuremapping<completeorder>(message=>message.orderid).tosaga(saga=>saga.orderid); } }
//从timeout的源码看,这个方法是通过设置sendoptions,然后再把当前这个消息发送给自己来实现 protected task requesttimeout<ttimeoutmessagetype>(imessagehandlercontext context, timespan within, ttimeoutmessagetype timeoutmessage) { verifysagacanhandletimeout(timeoutmessage); var sendoptions = new sendoptions(); sendoptions.delaydeliverywith(within); sendoptions.routetothisendpoint(); settimeoutheaders(sendoptions); return context.send(timeoutmessage, sendoptions); }
总结
nservicebus因为是商业产品,对分布式消息系统所涉及到的东西都做了实现,包括分布式事务(outbox)、dtc都有,还有心跳检测,监控都有,全而大,目前我们用到的也只是nservicebus里很小的一部分功能。