0

I am building APIs for a Microservice application using FastAPI.

I built 2 microservices: ServiceA at localhost:8000 and ServiceB at localhost:8001. They are communicating with each other via RedisStreams. Here is how the directory structure looks like:

Project_Directory/
  | - ServiceA
        | - run.py : I import `app` from `src` here which imports the initialized variable in `__init__.py`
        | - src
              | - __init__.py: FastAPI application is created and initialized here. `app = FastAPI()` would be in this file
              | - routes.py: All routes in this file
              | - models.py: All models in this file
              | - consumer.py: This file reads and writes to RedisStream
  | - ServiceB
   ...(similar directory structure)

When I start the application, I start using the following:

uvicorn run:app --port 8000 --reload
uvicorn run:app --port 8001 --reload

I need to start the consumer separately (run consumer.py) to read/write values in each of the services.

Is there a way to integrate this in the FastAPI? I imported consumer into __init__ and tried the following:

  • invoked a direct method call to the Redis consumer method
    consumer.run_consumer_stream()
    
  • used the on_event("startup") to call the same consumer method
    @app.on_event("startup")
    async def startup_event():
        consumer.run_consumer_stream()
    
    
    

In both cases my service would take a long time to respond to API request and would fail with connect ETIMEDOUT 127.0.0.1:8000.

I want to integrate the starting of the Redis-Streams consumer to the main code, so that every time I start the service with uvicorn I don't need to run the consumer separately. One command should take care of starting all parts of a service. I am not sure I am looking in the right direction and hence, I'm not getting anything. Can someone help me with this?

  • 1
    Are you sure that this is an efficient structure? Being a microservice architecture, I assume that several tasks can be attributed to other workers (that would also qualify as microservices). The AIPs should generally be simple and act as an interface to various operations. Also, if you're interested about methods of replicating the same API (creating multiple instances), you should maybe take a look at `nginx`. – Ionut-Alexandru Baltariu Sep 04 '22 at 14:18

0 Answers0