Python, Nameko, nameko-sqlalchemy : Error : db_uris = self.container.config[DB_URIS_KEY] KeyError: 'DB_URIS'
Python, Nameko, nameko-sqlalchemy : Error : db_uris = self.container.config[DB_URIS_KEY] KeyError: 'DB_URIS'
大家好,
首先,我想让你知道,我才刚刚开始学习 python 微服务库 Nameko。
我正在尝试创建一个简单的微服务,使用 nameko-sqlalchemy 扩展在 Sqlite 数据库中添加一个用户。当我尝试使用 Nameko 命令 运行 微服务时:nameko 运行 user_service,出现错误:
Connected to amqp://guest:**@127.0.0.1:5672//
starting services: userService
Traceback (most recent call last):
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 461, in fire_timers
timer()
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/hubs/timer.py", line 59, in __call__
cb(*args, **kw)
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/greenthread.py", line 221, in main
result = function(*args, **kwargs)
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/nameko/utils/concurrency/__init__.py", line 76, in call
return getattr(item, name)(*args, **kwargs)
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/nameko_sqlalchemy/database_session.py", line 24, in setup
db_uris = self.container.config[DB_URIS_KEY]
KeyError: 'DB_URIS'
我使用下面的代码:
user_service.py
from nameko_sqlalchemy import DatabaseSession
from nameko.rpc import rpc
from nameko.containers import ServiceContainer
from model import DeclarativeBase
from model import User
class UserService(object):
name = 'userService'
db = DatabaseSession(DeclarativeBase)
@rpc
def write_to_db(self, user_id, first_name, last_name):
user = User(user_id, first_name, last_name)
with self.db.get_session() as session:
session.add(user)
session.commit()
CONFIG = {
"AMQP_URI":"amqp://guest:guest@localhost",
"DB_URIS":
{"userService:Base":"sqlite:///data.db"}
}
container = ServiceContainer(UserService,config=CONFIG)
service_extention = list(container.extensions)
container.start()
container.stop()
与 :
model.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime
DeclarativeBase = declarative_base()
class User(DeclarativeBase):
__tablename__ = "users"
user_id = Column(Integer,primary_key=True)
first_name = Column(String)
last_name = Column(String)
def __init__(self, user_id, first_name, last_name):
self.user_id = user_id
self.first_name = first_name
self.last_name = last_name
if __name__ == "__main__":
from sqlalchemy import create_engine
engine = create_engine('sqlite:///data.db', echo = True)
DeclarativeBase.metadata.create_all(engine)
环境:
滑子 2.12.0 /
nameko-sqlalchemy 1.5.0 /
Ubuntu 18.04.4 LTS /
Python3.6.9/
虚拟环境
提前感谢您的帮助。
此致,
尤尼斯
在学习了 official documentation 之后,我发现了 运行ning 代码的方法出了什么问题。
解决方案是:
- 像下面那样直接使用 Nameko CLI,但要添加配置文件。新命令如下所示:nameko 运行 user_service --config ./config.yaml
./config.yaml
AMQP_URI: 'amqp://guest:guest@localhost'
DB_URIS:
'userService:Base': 'sqlite:///data.db'
- 添加服务容器或运行器后直接运行 user_service.py 文件,配置作为字典传递。该解决方案每次使用都需要 运行 python 文件。我想我用第二种方式错过了一些东西。
希望对您有所帮助。
大家好,
首先,我想让你知道,我才刚刚开始学习 python 微服务库 Nameko。
我正在尝试创建一个简单的微服务,使用 nameko-sqlalchemy 扩展在 Sqlite 数据库中添加一个用户。当我尝试使用 Nameko 命令 运行 微服务时:nameko 运行 user_service,出现错误:
Connected to amqp://guest:**@127.0.0.1:5672//
starting services: userService
Traceback (most recent call last):
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 461, in fire_timers
timer()
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/hubs/timer.py", line 59, in __call__
cb(*args, **kw)
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/greenthread.py", line 221, in main
result = function(*args, **kwargs)
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/nameko/utils/concurrency/__init__.py", line 76, in call
return getattr(item, name)(*args, **kwargs)
File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/nameko_sqlalchemy/database_session.py", line 24, in setup
db_uris = self.container.config[DB_URIS_KEY]
KeyError: 'DB_URIS'
我使用下面的代码:
user_service.py
from nameko_sqlalchemy import DatabaseSession
from nameko.rpc import rpc
from nameko.containers import ServiceContainer
from model import DeclarativeBase
from model import User
class UserService(object):
name = 'userService'
db = DatabaseSession(DeclarativeBase)
@rpc
def write_to_db(self, user_id, first_name, last_name):
user = User(user_id, first_name, last_name)
with self.db.get_session() as session:
session.add(user)
session.commit()
CONFIG = {
"AMQP_URI":"amqp://guest:guest@localhost",
"DB_URIS":
{"userService:Base":"sqlite:///data.db"}
}
container = ServiceContainer(UserService,config=CONFIG)
service_extention = list(container.extensions)
container.start()
container.stop()
与 :
model.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime
DeclarativeBase = declarative_base()
class User(DeclarativeBase):
__tablename__ = "users"
user_id = Column(Integer,primary_key=True)
first_name = Column(String)
last_name = Column(String)
def __init__(self, user_id, first_name, last_name):
self.user_id = user_id
self.first_name = first_name
self.last_name = last_name
if __name__ == "__main__":
from sqlalchemy import create_engine
engine = create_engine('sqlite:///data.db', echo = True)
DeclarativeBase.metadata.create_all(engine)
环境: 滑子 2.12.0 / nameko-sqlalchemy 1.5.0 / Ubuntu 18.04.4 LTS / Python3.6.9/ 虚拟环境
提前感谢您的帮助。
此致, 尤尼斯
在学习了 official documentation 之后,我发现了 运行ning 代码的方法出了什么问题。
解决方案是:
- 像下面那样直接使用 Nameko CLI,但要添加配置文件。新命令如下所示:nameko 运行 user_service --config ./config.yaml ./config.yaml
AMQP_URI: 'amqp://guest:guest@localhost'
DB_URIS:
'userService:Base': 'sqlite:///data.db'
- 添加服务容器或运行器后直接运行 user_service.py 文件,配置作为字典传递。该解决方案每次使用都需要 运行 python 文件。我想我用第二种方式错过了一些东西。
希望对您有所帮助。