Kuvaka inovimbika, inowanikwa zvakanyanya, inopararira yakagovaniswa system inoda kuteedzera kune chaiwo matekiniki, misimboti, uye mapatani. Kugadzirwa kwemaitiro akadaro kunosanganisira kugadzirisa matambudziko akawanda. Pakati penyaya dzakapararira uye dzehwaro idambudziko rekunyora maviri .
Iyo "dambudziko rekunyora maviri" idambudziko rinomuka mumasisitimu akagoverwa, kunyanya kana uchibata neakawanda data masosi kana dhatabhesi inoda kuchengetwa mukuwirirana. Zvinoreva kuomerwa kwekuona kuti shanduko yedata inongonyorwa kuzvitoro zvakasiyana siyana, senge dhatabhesi kana cache, pasina kuunza nyaya senge kusawirirana kwedata, kukakavara, kana mabhodhoro ekuita.
Iyo microservices dhizaini uye pateni dhatabhesi pasevhisi inounza iwe akawanda mabhenefiti, akadai sekuzvimiririra kuendesa uye kuyera, kutadza kwakasarudzika, uye inogona kuwedzera yekusimudzira velocity. Nekudaro, mashandiro anoda shanduko pakati peakawanda mamicroservices, zvichikumanikidza kuti ufunge nezve yakavimbika mhinduro yekugadzirisa dambudziko iri.
Ngatitarisei mamiriro ezvinhu umo nzvimbo yedu inosanganisira kugamuchira zvikumbiro zvechikwereti, kuzviongorora, uyezve kutumira chenjedzo kuvatengi.
Mune mumweya wemusimboti webasa rimwe chete, mutemo waConway, uye dhizaini-inotungamirwa nedhizaini, mushure menguva yakati wandei-yedutu zviitiko, dura rese rakakamurwa kuita matatu subdomains ane akatsanangurwa akaganhurirwa mamiriro ane miganhu yakajeka, domain modhi, uye ubiquitous mutauro.
Yekutanga inopihwa basa rekukwira nekunyora zvikumbiro zvitsva zvechikwereti. Yechipiri sisitimu inoongorora mashandisirwo aya uye inoita sarudzo zvichienderana nedata rakapihwa. Maitiro ekuongorora aya, anosanganisira KYC/KYB, anti fraud, uye cheki chenjodzi yechikwereti, anogona kutora nguva, zvichidikanwa kugona kubata zviuru zvezvikumbiro panguva imwe chete. Nekuda kweizvozvo, kushanda uku kwakaiswa kune yakatsaurirwa microservice ine yayo dhatabhesi, ichibvumira yakazvimirira kuyera.
Uyezve, aya ma subsystems anotungamirwa nezvikwata zviviri zvakasiyana, imwe neimwe iine yayo yekuburitsa mitsetse, sevhisi level zvibvumirano (SLA), uye scalability zvinodiwa.
Chekupedzisira , imwe nyanzvi yekuzivisa sevhisi iripo yekutumira chenjedzo kune vatengi.
Heino tsananguro yakakwenenzverwa yeiyo yekutanga kushandiswa kesi yeiyo system:
Iyo yakanaka yakapfava uye yechinyakare sisitimu pekutanga kuona, asi ngatinyure mukati mekuti iyo Chikwereti application sevhisi inoita sei kuendesa chikumbiro chechikwereti.
Tinogona kutarisa nzira mbiri dzekudyidzana kwesevhisi:
First-Local-Commit-Then-Publish: Nenzira iyi, sevhisi inogadziridza dhatabhesi yenzvimbo (inozvipira) uye yobva yaburitsa chiitiko kana meseji kune mamwe masevhisi.
Chekutanga-Publish-Then-Local-Commit: Zvakasiyana, nzira iyi inosanganisira kuburitsa chiitiko kana meseji usati waita shanduko kudhatabhesi renzvimbo.
Nzira dzose dziri mbiri dzine zvimhingamupinyi zvadzo uye dzinongokundikana zvishoma-dzakachengeteka pakukurukurirana mumasitimu akagoverwa.
Uyu ndiwo dhayagiramu yekutevedzana kwekushandisa nzira yekutanga.
Mune mamiriro ezvinhu aya, iyo Chikwereti Chikumbiro Sevhisi inoshandisa yekutanga-Local-Commit-Then-Publish maitiro, apo inotanga kuita kutengeserana uye yoedza kutumira chiziviso kune imwe system. Nekudaro, maitiro aya anogona kutadza kana, semuenzaniso, paine nyaya dzenetiweki, iyo Yekuongorora Sevhisi isingawanikwe, kana iyo Loan Application Service inosangana neKubuda kweMemory (OOM) kukanganisa uye kubondera. Mumamiriro ezvinhu akadaro, meseji inorasika, ichisiya Ongororo isina chiziviso chechikumbiro chitsva chechikwereti, kunze kwekunge mamwe matanho aitwa.
Uye wechipiri.
MuFirst-Publish-Then-Local-Commit scenario, iyo Loan Application Service inotarisana nenjodzi dzakakura. Inogona kuzivisa iyo Yekuongorora Sevhisi nezvechishandiso chitsva asi ichitadza kuchengetedza iyi gadziriso munharaunda nekuda kwezvinetso zvakaita senyaya dzedatabase, zvikanganiso zvendangariro, kana kodhi bugs. Iyi nzira inogona kutungamirira mukusawirirana kwakakosha mune data, izvo zvinogona kukonzera matambudziko makuru, zvichienderana nekuti iyo Loan Review Service inobata sei maapplication anouya.
Naizvozvo, isu tinofanirwa kuona mhinduro inopa yakasimba nzira yekuburitsa zviitiko kune vatengi vekunze. Asi, tisati tapinda mumhinduro dzinobvira, tinofanira kutanga tajekesa mhando dzevimbiso dzekutumira meseji dzinogoneka mumasisitimu akagoverwa.
Pane mhando ina dzevimbiso dzataigona kuwana.
Hapana vimbiso
Hapana vimbiso yekuti meseji ichaendeswa kunzvimbo yacho. Nzira yekutanga-Yemunharaunda-Commit-Then-Publish ndeyechokwadi pamusoro peizvi. Vatengi vanogona kugamuchira mameseji kamwe, kakawanda, kana kusambowana zvachose.
Kakawanda kutumirwa
Kazhinji kutumirwa kunoreva kuti meseji ichaunzwa kunzvimbo inoenda kazhinji kamwechete. Nzira yekutanga-Yemunharaunda-Commit-Then-Publish inogona kushandiswa nenzira iyi zvakare nemutemo wekuedza zvakare wekuedza neukoshi hwekutanga.
Kamwe kamwe chete kutumira\Vatengi vanogamuchira uye kugadzirisa meseji yega yega asi vanogona kugamuchira meseji imwechete kanopfuura kamwe.
Kamwe chete kutumirwa\Chaizvo kamwe kutumirwa kunoreva kuti mutengi anogashira meseji zvinobudirira kamwe chete.
Nehunyanzvi, zvinogoneka kuita neKafka transaction uye chaiyo idipont kuita kwemugadziri uye mutengi.
Kazhinji, 'kamwe kamwe' vimbiso yekutumira inogadzirisa nyaya dzakawanda nekuona kuti mameseji asvitswa kamwechete, asi vatengi vanofanirwa kunge vasina simba. Nekudaro, nekupihwa kusingadzivisike kutadza kwetiweki, pfungwa dzese dzemutengi dzinofanirwa kunge dzisina hunyanzvi kudzivirira kugadzirisa mameseji akapetwa, zvisinei nevimbiso yemugadziri. Naizvozvo, chinodiwa ichi hachisi kunyanya kukanganisa sezvo chinoratidza chokwadi.
Pane zvakawanda zvinogadzirisa dambudziko iri, zvine zvazvakanakira nezvazvakaipira.
Sekureva kweWikipedia, iyo Yechipiri-Phase Commit (2PC) yakagoverwa transaction protocol inoshandiswa mumakomputa sainzi uye dhatabhesi manejimendi masisitimu kuti ive nechokwadi chekuenderana uye kuvimbika kwekutengeserana kwakagoverwa. Yakagadzirirwa mamiriro ezvinhu apo zviwanikwa zvakawanda (semuenzaniso, dhatabhesi) zvinoda kutora chikamu mukutengeserana kumwe chete, uye inova nechokwadi chekuti vese vanoita dhizaini kana kuti vese vanoibvisa, nekudaro kuchengetedza data kuenderana. Zvinonzwika chaizvo izvo zvatinoda, asi Two-Phase Commit ine akati wandei anokanganisa:
Iyo inonyanya kuoneka mhinduro ye microservices architecture ndeye kushandisa pateni (kana kunyange dzimwe nguva anti-pattern) - yakagovaniswa dhatabhesi. Iyi nzira inonzwisisika kana iwe uchida transactional kuenderana pamatafura akawanda mune akasiyana dhatabhesi, ingo shandisa imwe yakagovaniswa dhatabhesi kune aya mamicroservices.
Zvipingamupinyi zveiyi nzira zvinosanganisira kuunza imwe pfungwa yekutadza, kurambidza yakazvimirira dhatabhesi kuyera, uye kudzikisira kugona kushandisa akasiyana dhatabhesi mhinduro dzakanyatsokodzera kune chaiwo zvinodiwa uye nyaya dzekushandisa. Pamusoro pezvo, kugadziridzwa kune microservices codebases kwaizove kuri madikanwa kutsigira yakadaro fomu yekugoverwa kwekutengeserana.
Iyo ' transactional outbox ' idhizaini yedhizaini inoshandiswa mumasisitimu akagoverwa kuti ive nechokwadi chekufambiswa kwemeseji, kunyangwe pamberi peasingavimbike mameseji masisitimu. Zvinosanganisira kuchengetedza zviitiko mune yakasarudzwa 'OutboxEvents' tafura mukati mekutengesa kwakafanana nekuvhiya pachako. Iyi nzira inoenderana zvakanaka neACID zvivakwa zvehukama dhatabhesi. Kusiyana neizvi, akawanda No-SQL dhatabhesi haatsigire zvizere ACID zvivakwa, achisarudza panzvimbo yemisimboti yeCAP theorem uye BASE uzivi, iyo inotungamira kuwanikwa uye inozopedzisira yagadzikana pamusoro pekusagadzikana kwakasimba.
A transactional outbox inopa kamwechete vimbiso uye inogona kuitwa nemaitiro akati wandei:
Transaction log tailing
Muparidzi wekuvhota
Transaction log tailing approach inoreva kushandisa dhatabhesi-chaiyo mhinduro seCDC (Change Data Capture). Zvinonyanya kukanganisa nzira iyi ndezvi:
Database chaiyo mhinduro
Kuwedzera latency nekuda kwezvakatemwa zveCDC kuita
Imwe nzira ndeye Polling Publisher , iyo inogonesa kuburitsa kunze kwebhokisi nekuvhota tafura yeoutbox. Iyo yekutanga dhizaini yeiyi nzira ndeye mukana wekuwedzera dhatabhesi mutoro, izvo zvinogona kutungamira kumitengo yakakwira. Uyezve, haasi ese eNo-SQL dhatabhesi anotsigira kubvunza kwakanaka kune chaiwo magwaro zvikamu. Kubvisa magwaro ose kunogona, naizvozvo, kukonzera kuderera kwekuita.
Heino dhayagiramu diki yekutevedzana inotsanangura kuti inoshanda sei.
Dambudziko rekutanga neTransactional Outbox pattern riri mukutsamira kwayo pane database ACID zvivakwa. Inogona kuve yakatwasuka mune yakajairwa OLTP dhatabhesi asi inounza matambudziko muNoSQL realm. Kugadzirisa izvi, mhinduro inogona kukwidziridza iyo append log (semuenzaniso, Kafka) kubva pakutanga kugadzirisa chikumbiro.
Panzvimbo pekugadzirisa zvakananga 'kutumira chikumbiro chechikwereti' murairo, isu tinobva tatumira kune yemukati Kafka musoro tozodzosera 'inogamuchirwa' mhedzisiro kumutengi. Nekudaro, sezvo paine mukana wekuti murairo uchiri kuda kugadziriswa, hatigone kuzivisa mutengi nekukurumidza nezvemhedzisiro. Kuti tigadzirise kusarongeka uku, tinogona kushandisa nzira dzakaita sekuvhota kwenguva refu, kuvhota kunotangwa nevatengi, kugadziridza UI, kana kushandisa maWebSockets kana Server-Sent Zviitiko kuti zviziviswe. Zvisinei, iyi inyaya yakasiyana zvachose, saka ngatidzokere kuchidzidzo chedu chekutanga.
Isu takatumira meseji pane yemukati Kafka musoro. Iyo Loan Application Service inopedza iyi meseji - iwo iwo murairo mumwechete wayakagamuchira kubva kumutengi - uye inotanga kugadzirisa. Kutanga, inoita imwe bhizinesi logic; chete mushure mekunge pfungwa iyi yaitwa zvinobudirira uye mhedzisiro yacho ichienderera, inoburitsa mameseji matsva pane yeruzhinji Kafka musoro.
Ngatitarisei zvishoma zvepseudo-code.
public async Task HandleAsync(SubmitLoanApplicationCommand command, ...) { //First, process business logic var loanApplication = await _loanApplicationService.HandleCommandAsync(command, ...); //Then, send new events to public Kafka topic producer.Send(new LoanApplicationSubmittedEvent(loanApplication.Id)); //Then, commit offset consumer.Commit(); }
Zvakadini kana kugadzirisa kweiyo bhizinesi logic kukatadza? Hapana kunetseka, sezvo iyo offset isati yaitwa, meseji ichayedzwa zvakare.
Zvakadini kana kutumira zviitiko zvitsva kuKafka zvikatadza? Hapana kunetseka, sezvo bhizinesi pfungwa isina simba, haizogadzira yakapetwa chikumbiro chechikwereti. Pane kudaro, ichaedza kutumira mameseji kune yeruzhinji Kafka musoro.
Ko kana mameseji akatumirwa kuKafka, asi iyo offset kuzvipira ikatadza? Hapana kunetseka, sezvo bhizinesi pfungwa isina simba, haizogadzira yakapetwa chikumbiro chechikwereti. Pane kudaro, ichatumira zvakare mameseji kune yeruzhinji Kafka musoro uye ndinovimba kuti iyo offset kuzvipira inobudirira panguva ino.
Izvo zvinonyanya kukanganisa nzira iyi zvinosanganisira kuomeswa kwakawedzerwa kwakabatana nemaitiro matsva ehurongwa, kupedzisira kuenderana (sezvo mutengi asingazoziva nekukurumidza mhedzisiro), uye chinodiwa chekuti zvese zvebhizinesi logic ive isina simba.
Chii chinonzi kutsvaga chiitiko, uye chingashandiswa sei pano? Event sourcing isoftware architectural pattern inoshandiswa kuenzanisira mamiriro ehurongwa nekutora shanduko dzese kudata rayo senhevedzano yezviitiko zvisingachinji. Zviitiko izvi zvinomiririra chokwadi kana shanduko yenyika uye inoshanda sechinhu chimwe chete chechokwadi chemamiriro ehurongwa hwazvino. Saka, nehunyanzvi, nekuita chiitiko-kutsvagisa sisitimu, isu tatova nezviitiko zvese muEventStore, uye iyi EventStore inogona kushandiswa nevatengi sesosi imwechete yechokwadi pane zvakaitika. Iko hakuna kudikanwa kweiyo chaiyo dhatabhesi mhinduro yekutevera shanduko dzese kana kunetsekana nezve kuodha, dambudziko chete nderekugara padivi rekuverenga sezvo kukwanisa kuwana chaiyo mamiriro esangano kunodiwa kudzokorora zviitiko zvese.
Muchikamu chino, takaongorora nzira dzinoverengeka dzekuvaka mameseji akavimbika mumasisitimu akaparadzirwa. Pane akati wandei kurudziro atinga tarisa tichivaka masisitimu ane aya maitiro
Nguva inotevera, tichatarisa muenzaniso unoshanda wekushandisa Transactional Outbox. Maona
iwe!