Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
hhstore committed Jul 6, 2024
1 parent 142ba0b commit 437f851
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
23 changes: 21 additions & 2 deletions packages/try-nats/py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ pip install nats-py

## Deployment(Docker-compose):

- 基于 `docker-compose` 部署


### build

- build base image:

```ruby

# build python3.12 base images:
task df:py:bt312

# build nats base image:
task pkg:npy:ops:base -- build


Expand All @@ -41,13 +48,25 @@ task pkg:npy:ops:base -- build

### deploy `pub` service:

-
- run:


```ruby

task pkg:npy:ops:pub -- up -d

```

### deploy `sub` service:

- run:

```ruby
# 方式 1: 启动 1 个容器实例
task pkg:npy:ops:sub -- up -d
task pkg:npy:ops:sub -- logs -f

task ops:sub -- up -d
# 方式 2: 启动 3 个容器实例
docker-compose -f compose-sub.yml up -d --scale py-nats-sub=3

```
5 changes: 4 additions & 1 deletion packages/try-nats/py/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ tasks:
- docker-compose -f compose-pub.yml {{.CLI_ARGS}}

sub:up:
aliases: [ "compose:sub:up", "up:sub" ]
cmds:
- docker-compose -f compose-sub.yml up -d --scale py-nats-sub=3
- docker-compose -f compose-sub.yml up -d --scale py-nats-sub=3 # TODO X: --scale 可以启动多个容器实例
- docker-compose -f compose-sub.yml ps
- docker-compose -f compose-sub.yml logs -f
ignore_error: true


16 changes: 16 additions & 0 deletions packages/try-nats/py/compose-pub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
##########################################################################################
# python nats sub service:
#
##########################################################################################

version: '3.5'

services:
py-nats-pub:
extends:
file: compose-base.yml
service: py-nats-base
ports: [ ]
restart: no
command: "python3 src/try_nats/run_pub.py"

11 changes: 8 additions & 3 deletions packages/try-nats/py/src/try_nats/run_pub.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import asyncio

from nats.aio.client import Client as NATS

from loguru import logger

async def main():
#
# TODO X: docker 容器内访问, 需要使用本地真实 IP, 不可使用 localhost
#
host = "nats://10.211.1.58:4222"
nc = NATS()

await nc.connect("nats://localhost:4222")
await nc.connect(host)

#
# todo x: 并发测试, 测试 run_multi_sub.py 基于 queue 方式订阅, 处理唯一性
#
for i in range(10):
logger.debug(f"Nats Publish: msg {i}")
await nc.publish("updates", f"Message {i}".encode())

await nc.publish("updates", b'Hello NATS!')

print("Message published!")
logger.debug("Message published!")

await nc.close()

Expand Down

0 comments on commit 437f851

Please sign in to comment.