File tree 4 files changed +49
-6
lines changed
4 files changed +49
-6
lines changed Original file line number Diff line number Diff line change @@ -27,12 +27,19 @@ pip install nats-py
27
27
28
28
## Deployment(Docker-compose):
29
29
30
+ - 基于 ` docker-compose ` 部署
31
+
32
+
30
33
### build
31
34
32
35
- build base image:
33
36
34
37
``` ruby
35
38
39
+ # build python3.12 base images:
40
+ task df:py: bt312
41
+
42
+ # build nats base image:
36
43
task pkg:npy:ops: base -- build
37
44
38
45
@@ -41,13 +48,25 @@ task pkg:npy:ops:base -- build
41
48
42
49
### deploy ` pub ` service:
43
50
44
- -
51
+ - run:
45
52
46
53
54
+ ``` ruby
55
+
56
+ task pkg:npy:ops: pub -- up - d
57
+
58
+ ```
59
+
47
60
### deploy ` sub ` service:
48
61
62
+ - run:
63
+
49
64
``` ruby
65
+ # 方式 1: 启动 1 个容器实例
66
+ task pkg:npy:ops: sub -- up - d
67
+ task pkg:npy:ops: sub -- logs - f
50
68
51
- task ops: sub -- up - d
69
+ # 方式 2: 启动 3 个容器实例
70
+ docker- compose - f compose- sub .yml up - d -- scale py- nats- sub = 3
52
71
53
72
```
Original file line number Diff line number Diff line change @@ -60,8 +60,11 @@ tasks:
60
60
- docker-compose -f compose-pub.yml {{.CLI_ARGS}}
61
61
62
62
sub:up :
63
+ aliases : [ "compose:sub:up", "up:sub" ]
63
64
cmds :
64
- - docker-compose -f compose-sub.yml up -d --scale py-nats-sub=3
65
+ - docker-compose -f compose-sub.yml up -d --scale py-nats-sub=3 # TODO X: --scale 可以启动多个容器实例
65
66
- docker-compose -f compose-sub.yml ps
66
67
- docker-compose -f compose-sub.yml logs -f
67
68
ignore_error : true
69
+
70
+
Original file line number Diff line number Diff line change
1
+ # #########################################################################################
2
+ # python nats sub service:
3
+ #
4
+ # #########################################################################################
5
+
6
+ version : ' 3.5'
7
+
8
+ services :
9
+ py-nats-pub :
10
+ extends :
11
+ file : compose-base.yml
12
+ service : py-nats-base
13
+ ports : [ ]
14
+ restart : no
15
+ command : " python3 src/try_nats/run_pub.py"
16
+
Original file line number Diff line number Diff line change 1
1
import asyncio
2
2
3
3
from nats .aio .client import Client as NATS
4
-
4
+ from loguru import logger
5
5
6
6
async def main ():
7
+ #
8
+ # TODO X: docker 容器内访问, 需要使用本地真实 IP, 不可使用 localhost
9
+ #
10
+ host = "nats://10.211.1.58:4222"
7
11
nc = NATS ()
8
12
9
- await nc .connect ("nats://localhost:4222" )
13
+ await nc .connect (host )
10
14
11
15
#
12
16
# todo x: 并发测试, 测试 run_multi_sub.py 基于 queue 方式订阅, 处理唯一性
13
17
#
14
18
for i in range (10 ):
19
+ logger .debug (f"Nats Publish: msg { i } " )
15
20
await nc .publish ("updates" , f"Message { i } " .encode ())
16
21
17
22
await nc .publish ("updates" , b'Hello NATS!' )
18
23
19
- print ("Message published!" )
24
+ logger . debug ("Message published!" )
20
25
21
26
await nc .close ()
22
27
You can’t perform that action at this time.
0 commit comments