Building Laravel-Python Microservices for Data Processing

In this article, I will guide you through creating a microservice architecture using Laravel and Python to build a high-performance, scalable system for data processing. Laravel will act as the central hub for managing API requests, while Python will handle computationally intensive tasks like data transformation or machine learning.

This approach allows you to combine the strengths of both frameworks, enabling better performance and flexibility.

Building Laravel-Python Microservices for Data Processing

Building Laravel-Python Microservices for Data Processing

 

Setting Up Laravel as the Main Application

First, install a fresh Laravel project:

composer create-project --prefer-dist laravel/laravel laravel-microservice
cd laravel-microservice

Set up the env file with your database credentials. Run the migrations:

php artisan migrate

Install Laravel HTTP Client for seamless communication:

composer require guzzlehttp/guzzle

Create a controller to send data to the Python microservice:

php artisan make:controller DataProcessingController

Update the DataProcessingController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class DataProcessingController extends Controller
{
    public function processData(Request $request)
    {
        $data = $request->input('data'); // Accepting data from the user

        // Send the data to the Python microservice
        $response = Http::post('http://127.0.0.1:5000/process', [
            'data' => $data,
        ]);

        return response()->json([
            'status' => $response->status(),
            'result' => $response->json(),
        ]);
    }
}

Add a route in routes/web.php:

use App\Http\Controllers\DataProcessingController;

Route::post('/process-data', [DataProcessingController::class, 'processData']);

 

Building the Python Microservice

We’ll use Flask to create the Python microservice.

Install Flask:

pip install flask

Create a new Python file microservice.py:

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/process', methods=['POST'])
def process_data():
    data = request.json.get('data')  # Receive data from Laravel
    # Example: Perform some data processing
    processed_data = data.upper()  # Transform data to uppercase
    return jsonify({"processed_data": processed_data})

if __name__ == '__main__':
    app.run(debug=True, port=5000)

Run the microservice:

python microservice.py

 

Testing the Laravel-Python Communication

Start the Laravel application:

php artisan serve

Using a tool like Postman, send a POST request to the Laravel endpoint:

  • URL: http://127.0.0.1:8000/process-data
  • Method: POST
  • Body
{
    "data": "hello from laravel"
}

You should get a response from the Python microservice:

{
    "status": 200,
    "result": {
        "processed_data": "HELLO FROM LARAVEL"
    }
}

 

Enhancing the Architecture

Here’s how you can improve the system:

Use Docker for Microservice Deployment:

To containerize Laravel and Python, use Docker Compose

docker-compose.yml:

version: '3.8'

services:
  laravel:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    working_dir: /app
    command: php artisan serve --host=0.0.0.0 --port=8000

  python:
    build:
      context: ./python-service
    ports:
      - "5000:5000"

 

Add Queues for Async Processing

Use Laravel Queues to send requests to the Python service asynchronously, improving the system's scalability.

 

Secure the Communication

  • Implement authentication between Laravel and Python using API keys or OAuth2.
  • Use HTTPS for secure communication

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS