Ví dụ chạy được · Node.js caller → Python handler
Giữ một target xuyên qua hai ngôn ngữ
Checkout viết bằng Node.js gọi billing.quote. Một process Python sở hữu handler đó và trả kết quả qua CoAkka Runtime. Hai package dùng cùng Core generation và wire contract; không application nào import ngôn ngữ của phía kia.
Beginner · 01
Polyglot là một product contract, không phải một language API
Node.js checkout
ask target=billing.quote payload="3"
↓ direct CoAkka Runtime peer
Python billing
handler target=billing.quote → "$36.00"
- Application
- Sở hữu ý nghĩa target, payload format, handler logic và cách dùng kết quả.
- Connector
- Chiếu lifecycle và value vào Node.js hoặc Python theo cách tự nhiên của ngôn ngữ.
- Core
- Sở hữu accepted configuration, transport, routing, correlation, bounds và terminal outcome.
- Hạ tầng
- First run dùng direct localhost peer. Cùng mô hình hoạt động với DNS/IP tới được trên LAN, VM, edge hoặc Kubernetes.
Beginner · 02
Cài mỗi connector từ kênh chính thức
Python service
Terminal 1
mkdir python-service
cd python-service
python -m venv .venv
. .venv/bin/activate
python -m pip install --only-binary=:all: coakka-runtime-python==3.3.2Node.js caller
Terminal 2
mkdir node-caller
cd node-caller
npm init -y
npm install [email protected]Copy hai source file bên dưới vào các folder tương ứng. Không copy native library giữa hai folder; mỗi official connector sở hữu exact package layout và Core pin của nó.
Beginner · 03
Chạy Python billing handler trước
python-service/polyglot_service.py
import signal
import threading
from coakka_v2_connector import (
ConnectorStartSpec,
RuntimeHost,
RuntimeNetworkConfig,
local_route,
)
target = "billing.quote"
stop_requested = threading.Event()
def quote(payload: bytes) -> bytes:
quantity = int(payload.decode("utf-8"))
unit_price_dollars = 12
return f"${quantity * unit_price_dollars}.00".encode("utf-8")
def request_stop(_signum: int, _frame: object) -> None:
stop_requested.set()
start_spec = ConnectorStartSpec(
system_name="polyglot-billing",
node_id="python-billing-1",
routes=[local_route(target)],
network=RuntimeNetworkConfig.network_node(
bind_host="127.0.0.1",
bind_port=19_326,
advertise_host="127.0.0.1",
advertise_port=19_326,
),
)
signal.signal(signal.SIGINT, request_stop)
signal.signal(signal.SIGTERM, request_stop)
with RuntimeHost.start(start_spec=start_spec) as runtime:
runtime.register_payload_handler(target, quote)
print("Python billing ready on 127.0.0.1:19326", flush=True)
stop_requested.wait()
Terminal 1
python polyglot_service.pyReady output
Python billing ready on 127.0.0.1:19326
network_node- Mở listener. Ở đây bind và advertise đều là localhost vì hai process chạy cùng máy.
local_route(target)- Handler sống trong process Python này.
register_payload_handler- Nhận bytes và trả bytes. JSON, Protobuf hoặc schema khác do application sở hữu.
Beginner · 04
Chạy Node.js checkout
node-caller/polyglot-caller.mjs
import { Buffer } from "node:buffer";
import {
RouteStrategy,
RuntimeHost,
outboundOnlyNetwork,
} from "coakka-runtime-node";
const target = "billing.quote";
const runtime = RuntimeHost.start({
systemName: "polyglot-checkout",
nodeId: "node-checkout-1",
network: outboundOnlyNetwork(),
routes: [
{
target,
strategy: RouteStrategy.SINGLE_OWNER,
endpoints: [{ host: "127.0.0.1", port: 19326, weight: 1, flags: 0 }],
},
],
});
try {
const reply = await runtime.askPayload(
"checkout",
target,
Buffer.from("3"),
2000,
);
console.log("Python replied:", Buffer.from(reply).toString("utf8"));
} finally {
await runtime.close();
}
Terminal 2
node polyglot-caller.mjsoutboundOnlyNetwork()- Caller không cần listener.
SINGLE_OWNER- Chính xác một billing endpoint sở hữu route này trong first run.
askPayload- Node.js await correlated reply với caller budget hữu hạn 2000 ms.
finally- App Node.js sở hữu RuntimeHost và close nó kể cả khi call lỗi.
Beginner · 05
Reply đi qua language boundary, không đi qua product boundary
Python tính 3 món với giá mẫu $12.00 mỗi món.
Node.js output
Python replied: $36.00
- Target giữ nguyên là
billing.quoteở hai đầu. - Python sở hữu business calculation; Node.js sở hữu cách checkout dùng reply.
- CoAkka Runtime sở hữu direct transport, correlation và một terminal outcome.
- Dừng Python service bằng Ctrl+C sau khi caller hoàn tất.
- Để dùng nhiều billing replica, failover và safe retry, tiếp tục ở hướng dẫn distributed.