{"id":45056659,"url":"https://github.com/xyctruth/final","last_synced_at":"2026-02-19T09:44:22.199Z","repository":{"id":57651409,"uuid":"446334955","full_name":"xyctruth/final","owner":"xyctruth","description":"使用本地消息表与消息队列实现最终一致性","archived":false,"fork":false,"pushed_at":"2022-01-22T10:31:34.000Z","size":275,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-06-20T16:51:36.678Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xyctruth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-10T08:17:06.000Z","updated_at":"2022-07-06T10:19:13.000Z","dependencies_parsed_at":"2022-09-26T20:30:56.624Z","dependency_job_id":null,"html_url":"https://github.com/xyctruth/final","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xyctruth/final","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyctruth%2Ffinal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyctruth%2Ffinal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyctruth%2Ffinal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyctruth%2Ffinal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xyctruth","download_url":"https://codeload.github.com/xyctruth/final/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyctruth%2Ffinal/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29609525,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T06:47:36.664Z","status":"ssl_error","status_checked_at":"2026-02-19T06:45:47.551Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-02-19T09:44:21.513Z","updated_at":"2026-02-19T09:44:22.192Z","avatar_url":"https://github.com/xyctruth.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Final\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/xyctruth/final)](https://goreportcard.com/report/github.com/xyctruth/final)\n[![codecov](https://codecov.io/gh/xyctruth/final/branch/main/graph/badge.svg?token=YWNYJK9KQW)](https://codecov.io/gh/xyctruth/final)\n[![Build status](https://img.shields.io/github/workflow/status/xyctruth/final/Build/main)](https://github.com/xyctruth/final/actions/workflows/build.yml)\n[![LICENSE status](https://img.shields.io/github/license/xyctruth/final)](https://github.com/xyctruth/final/blob/main/LICENSE)\n\n# 简介\n\n**Final 使用本地消息表实现最终一致性**\n\n# 使用\n\n## 初始化\n\n```go\ndb, err := sql.Open(\"mysql\", mysqlConnStr)\nif err != nil {\n  panic(err)\n}\n\nmqProvider := amqp.NewProvider(amqpConnStr)\n\nbus := final.New(\"send_svc\", db, mqProvider, final.DefaultOptions())\nbus.Start()\n\n```\n\n更多的选项配置在 [options.go](./options.go)\n\n\n## 订阅\n\n```go\nbus.Subscribe(\"topic1\").Middleware(common.Middleware1, common.Middleware2).Handler(common.EchoHandler)\n```\n`common.Middleware1`,`common.Middleware2`,`common.EchoHandler` 的代码在 [common.go](_example/common/common.go)\n\n\n## 发布\n\n```go\nmsg := common.GeneralMessage{Type: \"simple message\", Count: 100}\nmsgBytes, _ := msgpack.Marshal(msg)\nerr := bus.Publish(\"topic1\", msgBytes, message.WithConfirm(true))\nif err != nil {\n  panic(err)\n}\n```\n\n更多消息发布策略在 [message_policy.go](./message/message_policy.go)\n\n## 关联本地事务发布\n\n### database/sql\n\n```go\ntx, _ := _example.NewDB().Begin()\n\n/* return err rollback，return nil commit */\nerr := bus.Transaction(tx, func(txBus *final.TxBus) error {\n  // 本地业务\n  _, err := tx.Exec(\"INSERT INTO local_business (remark) VALUE (?)\", \"sql local business\")\n  if err != nil {\n    return err\n  }\n  \n  // 发布消息\n  msg := common.GeneralMessage{Type: \"sql transaction message\", Count: 100}\n\tmsgBytes, _ := msgpack.Marshal(msg)\n  \n  err = txBus.Publish(\"topic1\", msgBytes)\n  if err != nil {\n    return err\n  }\n  return nil\n})\n\nif err != nil {\n  panic(err)\n}\n```\n\n### gorm\n\n```go\ntx := _example.NewGormDB().Begin()\n\n/* return err rollback，return nil commit */\nerr = bus.Transaction(tx.Statement.ConnPool.(*sql.Tx), func(txBus *final.TxBus) error {\n   // 本地业务\n  result := tx.Create(\u0026_example.LocalBusiness{\n    Remark: \"gorm local business\",\n  })\n  if result.Error != nil {\n    return result.Error\n  }\n\n   // 发送消息\n  msg := common.GeneralMessage{Type: \"gorm transaction message\", Count: 100}\n  msgBytes, _ := msgpack.Marshal(msg)\n  err = txBus.Publish(\"topic1\", msgBytes)\n  if err != nil {\n    return err\n  }\n  return nil\n})\n\nif err != nil {\n  panic(err)\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxyctruth%2Ffinal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxyctruth%2Ffinal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxyctruth%2Ffinal/lists"}