fastapi-nacos 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_nacos-0.0.1/.gitignore +55 -0
- fastapi_nacos-0.0.1/PKG-INFO +25 -0
- fastapi_nacos-0.0.1/README.md +0 -0
- fastapi_nacos-0.0.1/pyproject.toml +63 -0
- fastapi_nacos-0.0.1/src/fastapi_nacos/__init__.py +10 -0
- fastapi_nacos-0.0.1/src/fastapi_nacos/exception/__init__.py +8 -0
- fastapi_nacos-0.0.1/src/fastapi_nacos/manager/__init__.py +0 -0
- fastapi_nacos-0.0.1/src/fastapi_nacos/manager/fastapi_nacos_manager.py +82 -0
- fastapi_nacos-0.0.1/src/fastapi_nacos/nacos/__init__.py +0 -0
- fastapi_nacos-0.0.1/src/fastapi_nacos/nacos/nacos_config.py +35 -0
- fastapi_nacos-0.0.1/src/fastapi_nacos/nacos/nacos_discovery.py +30 -0
- fastapi_nacos-0.0.1/src/fastapi_nacos/util/__init__.py +0 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Compiled class file
|
|
2
|
+
*.class
|
|
3
|
+
|
|
4
|
+
# Log file
|
|
5
|
+
*.log
|
|
6
|
+
|
|
7
|
+
# BlueJ files
|
|
8
|
+
*.ctxt
|
|
9
|
+
|
|
10
|
+
*.ini
|
|
11
|
+
|
|
12
|
+
# Mobile Tools for Java (J2ME)
|
|
13
|
+
.mtj.tmp/
|
|
14
|
+
|
|
15
|
+
# Package Files #
|
|
16
|
+
*.jar
|
|
17
|
+
*.war
|
|
18
|
+
|
|
19
|
+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
|
20
|
+
hs_err_pid*
|
|
21
|
+
|
|
22
|
+
# IDEA or eclipse files #
|
|
23
|
+
.settings/
|
|
24
|
+
.project
|
|
25
|
+
.classpath
|
|
26
|
+
.factorypath
|
|
27
|
+
.idea/
|
|
28
|
+
*.iml
|
|
29
|
+
|
|
30
|
+
# mvn target
|
|
31
|
+
target/
|
|
32
|
+
.mvn/
|
|
33
|
+
|
|
34
|
+
# log files
|
|
35
|
+
logs/
|
|
36
|
+
|
|
37
|
+
# npm files
|
|
38
|
+
node_modules/
|
|
39
|
+
rebel.xml
|
|
40
|
+
|
|
41
|
+
# mac os
|
|
42
|
+
.DS_Store
|
|
43
|
+
|
|
44
|
+
__pycache__/
|
|
45
|
+
build/
|
|
46
|
+
dist/
|
|
47
|
+
.pytest_cache/
|
|
48
|
+
/royal_tsx/
|
|
49
|
+
inis-*/
|
|
50
|
+
*.c
|
|
51
|
+
*.key
|
|
52
|
+
*.pem
|
|
53
|
+
config-pro
|
|
54
|
+
ssh-key/
|
|
55
|
+
cert_files/
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-nacos
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A FastAPI-friendly integration with Nacos that provides configuration management, service registration and discovery, and client-side load-balanced service calls.
|
|
5
|
+
Author-email: ferdinand <gongjpa@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: aio,config,load balancing,nacos,service discovery,service registry
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Framework :: AsyncIO
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Requires-Python: >=3.12
|
|
16
|
+
Requires-Dist: aio-service-caller>=0.1.7
|
|
17
|
+
Requires-Dist: yamlpyconfig[nacos]>=0.0.9
|
|
18
|
+
Provides-Extra: dev
|
|
19
|
+
Requires-Dist: aresponses>=3.0.0; extra == 'dev'
|
|
20
|
+
Requires-Dist: black>=25.11.0; extra == 'dev'
|
|
21
|
+
Requires-Dist: mypy>=1.18.2; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest-asyncio>=1.3.0; extra == 'dev'
|
|
23
|
+
Requires-Dist: pytest-cov>=7.0.0; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=9.0.1; extra == 'dev'
|
|
25
|
+
Requires-Dist: ruff>=0.14.5; extra == 'dev'
|
|
File without changes
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "fastapi-nacos"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
description = "A FastAPI-friendly integration with Nacos that provides configuration management, service registration and discovery, and client-side load-balanced service calls."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"aio-service-caller>=0.1.7",
|
|
9
|
+
"yamlpyconfig[nacos]>=0.0.9",
|
|
10
|
+
]
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "ferdinand", email = "gongjpa@gmail.com" },
|
|
13
|
+
]
|
|
14
|
+
# 关键字方便快速检索
|
|
15
|
+
keywords = [
|
|
16
|
+
"nacos", "aio", "load balancing", "service registry", "service discovery", "config"
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
license = {text = "MIT"}
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Development Status :: 4 - Beta",
|
|
22
|
+
"Intended Audience :: Developers",
|
|
23
|
+
"License :: OSI Approved :: MIT License",
|
|
24
|
+
"Operating System :: OS Independent",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Framework :: AsyncIO",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"aresponses>=3.0.0",
|
|
33
|
+
"black>=25.11.0",
|
|
34
|
+
"mypy>=1.18.2",
|
|
35
|
+
"pytest>=9.0.1",
|
|
36
|
+
"pytest-asyncio>=1.3.0",
|
|
37
|
+
"pytest-cov>=7.0.0",
|
|
38
|
+
"ruff>=0.14.5",
|
|
39
|
+
]
|
|
40
|
+
[build-system]
|
|
41
|
+
requires = ["hatchling"]
|
|
42
|
+
build-backend = "hatchling.build"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = ["src/fastapi_nacos"]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.sdist]
|
|
49
|
+
include = [
|
|
50
|
+
"src",
|
|
51
|
+
"README.md",
|
|
52
|
+
"pyproject.toml"
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[tool.hatch.version]
|
|
56
|
+
path = "src/fastapi_nacos/__init__.py"
|
|
57
|
+
|
|
58
|
+
[[tool.uv.index]]
|
|
59
|
+
name = "testpypi"
|
|
60
|
+
url = "https://test.pypi.org/simple/"
|
|
61
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
62
|
+
explicit = true
|
|
63
|
+
|
|
File without changes
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import logging
|
|
3
|
+
from functools import wraps
|
|
4
|
+
from typing import Optional, Any
|
|
5
|
+
|
|
6
|
+
from aio_service_caller import ServiceManager
|
|
7
|
+
from yamlpyconfig.models import AlgorithmEnum
|
|
8
|
+
|
|
9
|
+
from ..exception import ServiceRegistryConfigError
|
|
10
|
+
from ..nacos.nacos_config import NacosConfig
|
|
11
|
+
from ..nacos.nacos_discovery import NacosDiscovery
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
def singleton(cls):
|
|
16
|
+
instance = {}
|
|
17
|
+
@wraps(cls)
|
|
18
|
+
def get_instance(*args, **kwargs):
|
|
19
|
+
if cls not in instance:
|
|
20
|
+
instance[cls] = cls(*args, **kwargs)
|
|
21
|
+
return instance[cls]
|
|
22
|
+
return get_instance
|
|
23
|
+
|
|
24
|
+
@singleton
|
|
25
|
+
class FastApiNacos:
|
|
26
|
+
|
|
27
|
+
def __init__(self, config_dir: Optional[str] = None,
|
|
28
|
+
crypto_algorithm: AlgorithmEnum | None = None, key: str = None):
|
|
29
|
+
self._nacos_config = NacosConfig(config_dir, crypto_algorithm, key)
|
|
30
|
+
self._discovery = None
|
|
31
|
+
|
|
32
|
+
async def start(self):
|
|
33
|
+
config = self._nacos_config.config
|
|
34
|
+
tasks = []
|
|
35
|
+
if "app-registry" in config and "nacos" in config["app-registry"]:
|
|
36
|
+
self._discovery = NacosDiscovery(config_manager=self._nacos_config.config_manager,
|
|
37
|
+
interceptors=[])
|
|
38
|
+
task_service_registry = asyncio.create_task(self._discovery.start())
|
|
39
|
+
task_service_registry.add_done_callback(lambda t: logger.error(f"service registry error: {t.exception()}") if t.exception() else None)
|
|
40
|
+
tasks.append(task_service_registry)
|
|
41
|
+
else:
|
|
42
|
+
logger.warning("No app-registry configuration found, so the service manager will be None.")
|
|
43
|
+
|
|
44
|
+
task_config = asyncio.create_task(self._nacos_config.start())
|
|
45
|
+
task_config.add_done_callback(lambda t: logger.error(f"get nacos config error: {t.exception()}") if t.exception() else None)
|
|
46
|
+
tasks.append(task_config)
|
|
47
|
+
await asyncio.gather(*tasks, return_exceptions=True)
|
|
48
|
+
|
|
49
|
+
async def __aenter__(self):
|
|
50
|
+
await self.start()
|
|
51
|
+
return self
|
|
52
|
+
|
|
53
|
+
async def stop(self):
|
|
54
|
+
tasks = []
|
|
55
|
+
task_config = asyncio.create_task(self._nacos_config.stop())
|
|
56
|
+
task_config.add_done_callback(lambda t: logger.error(f"nacos config stop error: {t.exception()}") if t.exception() else None)
|
|
57
|
+
tasks.append(task_config)
|
|
58
|
+
if self._discovery:
|
|
59
|
+
task_service_registry = asyncio.create_task(self._discovery.stop())
|
|
60
|
+
task_service_registry.add_done_callback(lambda t: logger.error(f"service discovery stop error: {t.exception()}") if t.exception() else None)
|
|
61
|
+
tasks.append(task_service_registry)
|
|
62
|
+
await asyncio.gather(*tasks, return_exceptions=True)
|
|
63
|
+
|
|
64
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
65
|
+
await self.stop()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def service_manager(self) -> Optional[ServiceManager]:
|
|
70
|
+
"""如果配置中存在app-registry.nacos的相关配置则返回ServiceManager对象,否则将抛出ServiceRegistryConfigError"""
|
|
71
|
+
if self._discovery:
|
|
72
|
+
return self._discovery.service_manager
|
|
73
|
+
else:
|
|
74
|
+
message = (
|
|
75
|
+
"No app-registry configuration found; "
|
|
76
|
+
"service manager cannot be initialized."
|
|
77
|
+
)
|
|
78
|
+
raise ServiceRegistryConfigError(message)
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def config(self) -> dict[str, Any]:
|
|
82
|
+
return self._nacos_config.config
|
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from yamlpyconfig.models import AlgorithmEnum
|
|
3
|
+
from yamlpyconfig import ConfigManager
|
|
4
|
+
|
|
5
|
+
class NacosConfig:
|
|
6
|
+
|
|
7
|
+
def __init__(self, config_dir: Optional[str] = None,
|
|
8
|
+
crypto_algorithm: AlgorithmEnum | None = None, key: str = None):
|
|
9
|
+
self._config_manager = ConfigManager(
|
|
10
|
+
config_dir,
|
|
11
|
+
crypto_algorithm=crypto_algorithm,
|
|
12
|
+
key=key,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
async def start(self):
|
|
16
|
+
await self._config_manager.start()
|
|
17
|
+
|
|
18
|
+
async def __aenter__(self):
|
|
19
|
+
await self.start()
|
|
20
|
+
return self
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async def stop(self):
|
|
24
|
+
await self._config_manager.stop()
|
|
25
|
+
|
|
26
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
27
|
+
await self.stop()
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def config(self):
|
|
31
|
+
return self._config_manager.get_config()
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def config_manager(self):
|
|
35
|
+
return self._config_manager
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
from aio_service_caller import ServiceManager, IServiceInterceptor
|
|
4
|
+
from yamlpyconfig import ConfigManager
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Project01/configs.py
|
|
8
|
+
|
|
9
|
+
class NacosDiscovery:
|
|
10
|
+
def __init__(self, config_manager: ConfigManager,
|
|
11
|
+
interceptors: Optional[list[IServiceInterceptor]] = None):
|
|
12
|
+
self._service_manager = ServiceManager(config_manager=config_manager, interceptors=interceptors)
|
|
13
|
+
|
|
14
|
+
@property
|
|
15
|
+
def service_manager(self):
|
|
16
|
+
return self._service_manager
|
|
17
|
+
|
|
18
|
+
async def start(self):
|
|
19
|
+
await self._service_manager.start()
|
|
20
|
+
|
|
21
|
+
async def __aenter__(self):
|
|
22
|
+
await self.start()
|
|
23
|
+
return self
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def stop(self):
|
|
27
|
+
await self._service_manager.stop()
|
|
28
|
+
|
|
29
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
30
|
+
await self.stop()
|
|
File without changes
|