Привет всем ,
Прежде всего, я хочу сообщить вам, что я только начинаю узнавать о python микро сервисной библиотеке Nameko .
Я пытаюсь создать простой микро-сервис, который добавляет пользователя в базу данных Sqlite , используя расширение nameko-sqlalchemy . Когда я пытаюсь запустить микросервис с помощью команды 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'
Я использую код ниже:
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)
Среда : nameko 2.12.0 / nameko-sqlalchemy 1.5.0 / Ubuntu 18.04.4 LTS / Python 3.6.9 / Virtualenv
Заранее благодарю за помощь.
С уважением, Юнес