ÔõÑùÔÚThinkPHP6ÖоÙÐÐÂþÑÜʽÊÂÎñ´¦Öóͷ££¿
Ëæ×Å»¥ÁªÍøµÄÉú³¤ºÍÓ¦Óó¡¾°µÄÒ»Ö±À©´ó£¬¹ØÓÚϵͳµÄÐÔÄܺͿɿ¿ÐÔµÄÒªÇóÒ²Ô½À´Ô½¸ß¡£¶øÔÚÖØ´óµÄÓªÒµ³¡¾°Ï£¬ÍùÍùÐèÒª¶à¸öЧÀÍÐͬÍê³É£¬Õâ¾ÍÐèÒª¶ÔÂþÑÜʽÊÂÎñ¾ÙÐд¦Öóͷ£¡£±¾ÎĽ«ÏÈÈÝÔÚ thinkphp6 ÖÐÔõÑù¾ÙÐÐÂþÑÜʽÊÂÎñ´¦Öóͷ£¡£
Ò»¡¢ÂþÑÜʽÊÂÎñµÄ»ù±¾¿´·¨
1.ÂþÑÜʽÊÂÎñ
ÂþÑÜʽϵͳÊÇÖ¸ÔÚ¶ą̀ÅÌËã»úÉϵijÌÐòºÍÊý¾Ý×ÊÔ´£¬Í¨¹ýÍøÂç¾ÙÐÐÅþÁ¬ºÍͨѶ£¬²¢ÐͬÍê³ÉijһÏîʹÃü¡£ÔÚÕâÖÖÇéÐÎÏ£¬Èô¶à¸öÊÂÎñÐèÒªÉæ¼°µ½¶à¸ö×ÊÔ´£¬¾ÍÐèÒª¾ÙÐÐÂþÑÜʽÊÂÎñµÄе÷¡£¶øÂþÑÜʽÊÂÎñÊÇÖ¸Óɶà¸öÊÂÎñÐͬÍê³ÉµÄÊÂÎñ£¬ÐèÒªÖª×ãACIDÐÔ×Ó¡£
2.ACIDÊôÐÔ
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚÊý¾Ý¿âÖУ¬ACIDÊÇÖ¸Ô×ÓÐÔ¡¢Ò»ÖÂÐÔ¡¢¸ôÀëÐԺͳ¤ÆÚÐÔËĸöÊôÐÔ¡£
Ô×ÓÐÔ£¨Atomicity£©£ºÖ¸ÊÂÎñÊÇÒ»¸ö²»¿ÉÖ§½âµÄÊÂÇ鵥λ£¬ÒªÃ´ËùÓÐÍê³É£¬ÒªÃ´ËùÓв»Íê³É£¬²»±£´æ²¿·ÖÍê³ÉµÄÇéÐΡ£
Ò»ÖÂÐÔ£¨Consistency£©£ºÖ¸ÊÂÎñÖ´ÐÐÇ°ºó£¬Êý¾Ý¿âµÄ״̬±ØÐè¼á³ÖÒ»ÖÂÐÔ£¬ÈçתÕËÊÂÎñ£¬ÔÚÖ´ÐÐתÕËÇ°ºÍºó£¬ÕË»§Óà¶îÖ®ºÍ¼á³ÖÎȹ̡£
¸ôÀëÐÔ£¨Isolation£©£ºÖ¸¶à¸öÊÂÎñ²¢ÐÐÖ´ÐÐʱ£¬Ò»¸öÊÂÎñµÄÖ´Ðв»Ó¦¸ÃÊܵ½ÆäËûÊÂÎñµÄ×ÌÈÅ¡£
³¤ÆÚÐÔ£¨Durability£©£ºÖ¸ÊÂÎñÒ»µ©Ìá½»£¬ÆäЧ¹û¾ÍÓ¦¸ÃÓÀÊÀÉúÑÄÔÚÊý¾Ý¿âÖС£
¶þ¡¢ThinkPHP6ÖÐÂþÑÜʽÊÂÎñµÄʵÏÖ
1.ÂþÑÜʽÊÂÎñµÄÎÊÌâ
ÔڹŰåµÄ¹ØϵÐÍÊý¾Ý¿âÖУ¬ÂþÑÜʽÊÂÎñµÄʵÏÖÐèҪʹÓÃÁ½½×¶ÎÌá½»£¨2PC£©ÐÒ飬µ«ÕâÖÖ·½·¨±£´æһЩÎÊÌ⣬Èçµ¥µã¹ÊÕÏ¡¢ÐÔÄÜÆ¿¾±µÈ¡£Òò´Ë£¬ÔÚ´óÊý¾Ý¡¢¸ß²¢·¢µÄÓ¦Óó¡¾°Ï£¬ÂþÑÜʽÊÂÎñµÄʵÏÖÐèҪʹÓÃÆäËûµÄ·½·¨¡£
2.ÂþÑÜʽÊÂÎñµÄ½â¾ö¼Æ»®
ÔÚThinkPHP6ÖоÙÐÐÂþÑÜʽÊÂÎñ´¦Öóͷ££¬¿ÉÒÔʹÓÿªÔ´µÄseataÖÐÐļþ£¬seata½«Ó¦Ó÷ÖΪÈý¸ö½ÇÉ«£¬»®·ÖÊÇTC£¨ÊÂÎñе÷Æ÷£©¡¢TM£¨ÊÂÎñÖÎÀíÆ÷£©ºÍRM£¨×ÊÔ´ÖÎÀíÆ÷£©£º
TC£¨Transaction Coordinator£©£ºÊÂÎñе÷Æ÷£¬ÈÏÕæе÷ÂþÑÜʽÊÂÎñÄ£¿éµÄ×ÊÔ´ºÍʵÏÖÊÂÎñµÄÒ»ÖÂÐÔ¡£
TM£¨Transaction Manager£©£ºÊÂÎñÖÎÀíÆ÷£¬ÈÏÕæÊÂÎñµÄ¿ªÆô¡¢Ìá½»¡¢»Ø¹öµÈÓëÊÂÎñÏà¹ØµÄ²Ù×÷¡£
RM£¨Resource Manager£©£º×ÊÔ´ÖÎÀíÆ÷£¬ÈÏÕæÖÎÀí×ÊÔ´£¬ÈçÊý¾Ý¿âµÄ²Ù×÷¡¢MQµÄ²Ù×÷µÈ¡£
3.seataµÄʹÓÃ
ÔÚʹÓÃseataÇ°£¬ÐèÒªÏȾÙÐÐseataµÄ×°ÖúÍÉèÖ㬰üÀ¨½¨ÉèTC¡¢RMµÈ×ÊÔ´¡£ÔÚ×°ÖúÍÉèÖÃÍê³Éºó£¬¾Í¿ÉÒÔʹÓÃseataÀ´¾ÙÐÐÂþÑÜʽÊÂÎñµÄ´¦Öóͷ££¬Ïêϸ°ì·¨ÈçÏ£º
£¨1£©ÒýÈëseataµÄÒÀÀµ¿â
<!-- seataÒÀÀµ¿â --> <dependency> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> <version>${seata.version}</version> </dependency>
µÇ¼ºó¸´ÖÆ
£¨2£©ÉèÖÃseataµÄÎļþ
ÔÚÐèÒª¾ÙÐÐÂþÑÜʽÊÂÎñµÄÄ£¿éÖУ¬ÐèÒªÔÚapplication.propertiesÖÐÌí¼ÓÈçÏÂÉèÖãº
# ÉèÖÃseataµÄÈ«¾ÖÊÂÎñIDÌìÉúÆ÷ seata.tx-service-group=my_group # type£¬ATÌåÏÖATģʽ£¬XAÌåÏÖXAģʽ seata.tx-type=AT # ×Ô¶¯ÊðÀíÊý¾ÝÔ´ seata.autoproxy.datasource=true
µÇ¼ºó¸´ÖÆ
£¨3£©ÔÚÊÂÎñ×îÏÈʱ£¬¾ÙÐÐÈ«¾Ö¿ªÆô
ÔÚÊÂÎñ×îÏÈʱ£¬ÐèÒª¾ÙÐÐÈ«¾Ö¿ªÆô£º
// ¿ªÆôÈ«¾ÖÊÂÎñ GlobalTransactionContext.begin(transactionName);
µÇ¼ºó¸´ÖÆ
£¨4£©ÔÚÊÂÎñÖÐʹÓÃRM
ÔÚÊÂÎñÖÐʹÓÃRM£¨ÈçÊý¾Ý¿âRDMS£©Ê±£¬ÐèҪʹÓÃseataÌṩµÄÊðÀí£¬¶Ô×ÊÔ´¾ÙÐÐÖÎÀí£º
// ʹÓÃÊðÀí»ñÈ¡connection conn = ((DataSourceProxy) dataSource).getConnection();
µÇ¼ºó¸´ÖÆ
£¨5£©ÔÚÊÂÎñ¿¢ÊÂʱ£¬¾ÙÐÐÈ«¾ÖÌá½»
ÔÚÊÂÎñ¿¢ÊÂʱ£¬ÐèÒª¾ÙÐÐÈ«¾ÖÌá½»£º
// Ìύȫ¾ÖÊÂÎñ GlobalTransactionContext.getCurrentOrCreate().commit();
µÇ¼ºó¸´ÖÆ
ÓÉÓÚseata½«ÂþÑÜʽÊÂÎñµÄÄÚÈÝ·â×°ÔÚÖÐÐļþÖУ¬ÒÔÊÇÔÚʹÓÃseataʱ£¬¿ÉÒÔ½«ÓªÒµÂß¼ºÍÂþÑÜʽÊÂÎñ´¦Öóͷ£ÍÑÀ룬Àû±ãÖÎÀíºÍά»¤¡£
Èý¡¢×ܽá
±¾ÎÄÍŽáThinkPHP6ºÍseataÖÐÐļþ£¬ÏÈÈÝÁËÔÚÂþÑÜʽϵͳÖоÙÐÐÂþÑÜʽÊÂÎñµÄ´¦Öóͷ£Àú³Ì£¬ÒÔ¼°seataÖÐÐļþµÄʹÓÃÒªÁì¡£ÔÚÏÖʵӦÓÃÖУ¬ÐèҪƾ֤ÏêϸӪҵ³¡¾°£¬ÔÚÐÔÄܺͿɿ¿ÐÔÖ®¼ä¾ÙÐÐÑ¡Ôñ£¬¾ÙÐÐÂþÑÜʽÊÂÎñ´¦Öóͷ£¡£
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖоÙÐÐÂþÑÜʽÊÂÎñ´¦Öóͷ££¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡