Skip to content

Commit 5a349df

Browse files
committed
[fit] python 版的默认使用内存版注册中心 并添加 readme 文件
1 parent e386bed commit 5a349df

File tree

11 files changed

+262
-0
lines changed

11 files changed

+262
-0
lines changed
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# FIT Python Nacos Registry 插件
2+
3+
## 简介
4+
5+
FIT Python Nacos Registry 插件是 FIT for Python 框架的服务注册中心插件,提供基于 Alibaba Nacos 的服务注册与发现功能。该插件实现了与 Java 版本兼容的 Nacos 服务协调机制,支持 Python 服务与 Java 服务的跨语言服务发现和调用。
6+
7+
## 重要说明
8+
9+
由于 `fit_py_nacos_registry` 插件的优先级默认高于内存版注册中心,只要该插件存在于 `plugin` 目录中,就会影响内存版注册中心的使用。因此:
10+
11+
### 默认状态
12+
- 插件默认**不启用**,需要手动移动到 `plugin` 目录下才会生效
13+
- 当不使用 Nacos 时,应将插件移动到非 `plugin` 目录,避免影响内存版注册中心
14+
15+
### 插件启用/禁用
16+
17+
#### 启用 Nacos 插件
18+
```bash
19+
# 进入 FIT Python 根目录
20+
cd framework/fit/python
21+
22+
# 将插件从根目录移动到 plugin 目录
23+
mv fit_py_nacos_registry plugin/
24+
echo "Nacos 插件已启用"
25+
```
26+
27+
#### 禁用 Nacos 插件
28+
```bash
29+
# 将插件移动到其他位置(建议移动到 plugin 目录的上级目录)
30+
mv framework/fit/python/plugin/fit_py_nacos_registry framework/fit/python/
31+
```
32+
33+
## 快速开始
34+
35+
### 1. Nacos 部署
36+
37+
#### 本地安装部署
38+
39+
1. 下载 Nacos Server
40+
41+
```bash
42+
# 下载 Nacos 2.3.0
43+
wget https://github.com/alibaba/nacos/releases/download/2.3.0/nacos-server-2.3.0.tar.gz
44+
45+
# 解压
46+
tar -xvf nacos-server-2.3.0.tar.gz
47+
cd nacos/bin
48+
```
49+
50+
2. 启动 Nacos
51+
52+
```bash
53+
# Linux/Mac
54+
sh startup.sh -m standalone
55+
56+
# Windows
57+
startup.cmd -m standalone
58+
```
59+
60+
访问控制台:http://localhost:8848/nacos
61+
62+
**默认登录凭据**
63+
- 用户名:`nacos`
64+
- 密码:`nacos`
65+
66+
> ⚠️ **安全提醒**:默认凭据仅适用于开发环境。在生产环境中,请务必修改默认的用户名和密码以确保系统安全。
67+
68+
### 2. 插件配置
69+
70+
#### 依赖安装
71+
72+
确保已安装必要的 Python 依赖:
73+
74+
```bash
75+
pip install nacos-sdk-python
76+
pip install pyyaml
77+
pip install requests
78+
```
79+
80+
#### 配置文件
81+
82+
`application.yml` 中配置 Nacos 连接信息:
83+
84+
```yaml
85+
# 基础配置示例
86+
registry-center:
87+
server:
88+
mode: 'PROXY' # 使用代理模式连接 Nacos
89+
addresses:
90+
- "localhost:8848"
91+
protocol: 2
92+
formats:
93+
- 1
94+
- 2
95+
96+
# Nacos 特定配置(可选)
97+
nacos:
98+
username: 'nacos' # Nacos 用户名
99+
password: 'nacos' # Nacos 密码
100+
namespace: 'local' # 命名空间
101+
weight: 1.0 # 服务权重
102+
isEphemeral: true # 是否为临时实例
103+
heartBeatInterval: 5000 # 心跳间隔(毫秒)
104+
heartBeatTimeout: 15000 # 心跳超时(毫秒)
105+
async:
106+
timeout: 30 # 异步操作超时时间(秒)
107+
```
108+
109+
## 配置参数说明
110+
111+
### 基础配置
112+
113+
| 参数 | 类型 | 必需 | 默认值 | 说明 |
114+
| --------------------------------- | ------- | ---- | ------ | --------------------------------------- |
115+
| `registry-center.server.mode` | String | 是 | - | 注册中心连接模式,使用 'PROXY' 连接 Nacos |
116+
| `registry-center.server.addresses`| List | 是 | - | Nacos 服务器地址列表 |
117+
| `registry-center.server.protocol` | Integer | 是 | - | 通信协议类型 |
118+
| `registry-center.server.formats` | List | 是 | - | 支持的数据格式 |
119+
120+
### Nacos 特定配置
121+
122+
| 参数 | 类型 | 必需 | 默认值 | 说明 |
123+
| --------------------------- | ------- | ---- | ------ | -------------------- |
124+
| `nacos.username` | String | 否 | - | Nacos 用户名 |
125+
| `nacos.password` | String | 否 | - | Nacos 密码 |
126+
| `nacos.accessKey` | String | 否 | - | 访问密钥 |
127+
| `nacos.secretKey` | String | 否 | - | 秘密密钥 |
128+
| `nacos.namespace` | String | 否 | "" | 命名空间 |
129+
| `nacos.weight` | Float | 否 | 1.0 | 服务权重 |
130+
| `nacos.isEphemeral` | Boolean | 否 | true | 是否为临时实例 |
131+
| `nacos.heartBeatInterval` | Long | 否 | 5000 | 心跳间隔(毫秒) |
132+
| `nacos.heartBeatTimeout` | Long | 否 | 15000 | 心跳超时(毫秒) |
133+
| `nacos.async.timeout` | Integer | 否 | 30 | 异步操作超时(秒) |
134+
135+
## 插件管理
136+
137+
### 目录结构
138+
139+
```
140+
framework/fit/python/
141+
├── plugin/ # 活跃插件目录
142+
│ ├── fit_py_nacos_registry/ # Nacos 插件(启用状态)
143+
│ └── other_plugins/ # 其他插件
144+
├── fit_py_nacos_registry/ # Nacos 插件(禁用状态)
145+
└── other_directories/
146+
```
147+
148+
### 管理命令
149+
150+
#### 启用 Nacos 插件
151+
152+
```bash
153+
# 进入 FIT Python 根目录
154+
cd framework/fit/python
155+
156+
# 如果插件在根目录,移动到 plugin 目录
157+
if [ -d "fit_py_nacos_registry" ]; then
158+
mv fit_py_nacos_registry plugin/
159+
echo "Nacos 插件已启用"
160+
fi
161+
```
162+
163+
#### 禁用 Nacos 插件
164+
165+
```bash
166+
# 进入 FIT Python 根目录
167+
cd framework/fit/python
168+
169+
# 如果插件在 plugin 目录,移动到根目录
170+
if [ -d "plugin/fit_py_nacos_registry" ]; then
171+
mv plugin/fit_py_nacos_registry ./
172+
echo "Nacos 插件已禁用,使用内存版注册中心"
173+
fi
174+
```
175+
176+
### 验证插件状态
177+
178+
#### 检查插件是否启用
179+
180+
```bash
181+
# 检查 Nacos 插件是否在 plugin 目录
182+
if [ -d "framework/fit/python/plugin/fit_py_nacos_registry" ]; then
183+
echo "✅ Nacos 插件已启用"
184+
else
185+
echo "❌ Nacos 插件已禁用,使用内存版注册中心"
186+
fi
187+
```
188+
189+
## 使用示例
190+
191+
### 基本服务注册示例
192+
193+
```python
194+
from fitframework import fitable
195+
from fitframework.api.decorators import fit_service
196+
197+
@fit_service(service_id="weather_service")
198+
class WeatherService:
199+
@fitable(fitable_id="get_weather")
200+
def get_weather(self, city: str) -> str:
201+
return f"Weather in {city}: Sunny, 25°C"
202+
```
203+
204+
### 服务发现和调用示例
205+
206+
```python
207+
from fitframework import inject_fit
208+
from fitframework.api.decorators import fit_service
209+
210+
@fit_service(service_id="weather_client")
211+
class WeatherClient:
212+
def __init__(self):
213+
# 通过 FIT 框架自动发现并注入远程服务
214+
self.weather_service = inject_fit("get_weather")
215+
216+
def get_city_weather(self, city: str) -> str:
217+
# 调用远程服务(可能运行在其他 Python 或 Java 进程中)
218+
return self.weather_service(city)
219+
```
220+
221+
### 配置文件示例
222+
223+
```yaml
224+
# 完整的应用配置示例
225+
app:
226+
name: 'Weather Service'
227+
228+
worker:
229+
id: "weather-service-001"
230+
protocol-priorities:
231+
- 'HTTP:JSON'
232+
- 'HTTP:CBOR'
233+
234+
worker-environment:
235+
env: 'local'
236+
env-seq: 'local,dev,test,prod'
237+
238+
http:
239+
server:
240+
enabled: true
241+
address:
242+
port: 9666
243+
protocol: 2
244+
formats: [1, 2]
245+
246+
registry-center:
247+
server:
248+
mode: 'PROXY' # 使用 Nacos 代理模式
249+
addresses:
250+
- "localhost:8848"
251+
protocol: 2
252+
formats: [1, 2]
253+
254+
nacos:
255+
username: 'nacos'
256+
password: 'nacos'
257+
namespace: 'local'
258+
weight: 1.0
259+
isEphemeral: true
260+
heartBeatInterval: 5000
261+
heartBeatTimeout: 15000
262+
```

framework/fit/python/plugin/fit_py_nacos_registry/conf/application.yml renamed to framework/fit/python/fit_py_nacos_registry/conf/application.yml

File renamed without changes.

framework/fit/python/plugin/fit_py_nacos_registry/conf/info.yml renamed to framework/fit/python/fit_py_nacos_registry/conf/info.yml

File renamed without changes.

framework/fit/python/plugin/fit_py_nacos_registry/heartbeat/heartbeat_service.py renamed to framework/fit/python/fit_py_nacos_registry/heartbeat/heartbeat_service.py

File renamed without changes.

framework/fit/python/plugin/fit_py_nacos_registry/service/__init__.py renamed to framework/fit/python/fit_py_nacos_registry/service/__init__.py

File renamed without changes.

framework/fit/python/plugin/fit_py_nacos_registry/service/async_executor.py renamed to framework/fit/python/fit_py_nacos_registry/service/async_executor.py

File renamed without changes.

framework/fit/python/plugin/fit_py_nacos_registry/service/config.py renamed to framework/fit/python/fit_py_nacos_registry/service/config.py

File renamed without changes.

framework/fit/python/plugin/fit_py_nacos_registry/service/constants.py renamed to framework/fit/python/fit_py_nacos_registry/service/constants.py

File renamed without changes.

framework/fit/python/plugin/fit_py_nacos_registry/service/nacos_registry_server.py renamed to framework/fit/python/fit_py_nacos_registry/service/nacos_registry_server.py

File renamed without changes.

framework/fit/python/plugin/fit_py_nacos_registry/service/parsers.py renamed to framework/fit/python/fit_py_nacos_registry/service/parsers.py

File renamed without changes.

0 commit comments

Comments
 (0)