大家好,
首先,我想让你知道,我刚刚开始学习 python微服务库 Nameko。
我正在尝试创建一个简单的微服务,使用nameko-sqlalchemy扩展在Sqlite数据库中添加用户。当我尝试使用 Nameko 命令运行微服务时:nameko run 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'
我使用下面的代码:
用户服务.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()
和 :
模型.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)
环境:nameko 2.12.0 / nameko-sqlalchemy 1.5.0 / Ubuntu 18.04.4 LTS / Python 3.6.9 / Virtualenv
预先感谢您的帮助。
问候,尤尼斯