43 lines
2.2 KiB
Markdown
43 lines
2.2 KiB
Markdown
# 在现有 HTTP 服务中使用 Orbit
|
||
|
||
可运行的 Go 服务:HTTP 业务事务 → 公共 MySQL Outbox → Orbit → 内嵌 Worker → WebSocket 结果 → 业务 Inbox。
|
||
|
||
- [完整准备、运行、API、故障与退出说明](../../docs/examples/http-service.md)
|
||
- [业务路由与组件装配](cmd/server/main.go)
|
||
- [生命周期顺序](cmd/server/lifecycle.go)
|
||
- [业务事务](internal/jobs/store.go)
|
||
- [业务 Inbox 示例实现](internal/inbox/store.go),沿用独立 result-inbox 示例的事件去重与版本约束
|
||
|
||
`init` 创建业务库表;`provision` 用稳定身份创建结果订阅;`serve` 使用配置中的订阅 ID,启动时无需 Orbit 在线。默认只监听回环地址 127.0.0.1:8090。接入已有服务时挂载业务路由,并接入原有鉴权、监控及生命周期。
|
||
|
||
## 快速运行
|
||
|
||
先按上方完整手册创建专用 Orbit 队列/凭据(含 Worker 与事件权限)和一个独立空业务库。从仓库根目录执行,DSN 替换为自己的配置:
|
||
|
||
```sh
|
||
export ORBIT_URL=http://127.0.0.1:8080
|
||
export ORBIT_CREDENTIALS_FILE="$PWD/.local/http-demo.credentials.json"
|
||
export APP_MYSQL_DSN='user:password@tcp(127.0.0.1:3306)/orbit_http_demo'
|
||
export APP_HTTP_ADDR=127.0.0.1:8090
|
||
cd examples/http-service
|
||
export GOWORK=off
|
||
go run ./cmd/server init
|
||
export ORBIT_SUBSCRIPTION_ID="$(go run ./cmd/server provision)"
|
||
go build -o server ./cmd/server
|
||
./server serve
|
||
```
|
||
|
||
`init` 只在首次空库执行;持久保存 provision 的订阅 ID,重启只运行 `serve`。另开终端:
|
||
|
||
```sh
|
||
export OPERATION_ID="$(openssl rand -hex 16)"
|
||
curl --fail http://127.0.0.1:8090/jobs \
|
||
-H 'Content-Type: application/json' \
|
||
-d "{\"operation_id\":\"$OPERATION_ID\",\"source_ref\":\"objects/demo.txt\"}"
|
||
curl --fail "http://127.0.0.1:8090/jobs/$OPERATION_ID"
|
||
```
|
||
|
||
预期首次返回 `local_committed=true`;稍后 GET 显示 `submission=ACCEPTED`、`execution=SUCCEEDED`,结果包含 `normalized_ref=OBJECTS/DEMO.TXT`。同 ID、同内容重报返回 `duplicate=true`;相同 ID 改内容返回 409。超时后保留原 ID,不重新生成。
|
||
|
||
通过 Ctrl-C/SIGTERM 关闭时先停止接收 HTTP,再收尾发送器/Worker,之后停止结果消费,最后关闭数据库。完整错误码、故障恢复和部署关闭期限见运行手册。
|