Home >Backend Development >PHP Tutorial >PHP microservice architecture practice

PHP microservice architecture practice

王林
王林Original
2024-06-05 14:58:09697browse

PHP Microservice Architecture Practice: Installing the LEMP stack: Install Linux, Nginx, MySQL and PHP. Create a MySQL database: Create a database to store data. Install Composer: Use Composer to manage PHP dependencies. Build the microservice: Use Symfony to create a new Composer project and configure the service. Create entities: Define entities for mapping to database tables. Create a database schema: Use Doctrine to create a database schema. Create an API controller: A controller that handles user requests. Running microservices: Start microservices using PHP built-in server.

PHP microservice architecture practice

PHP microservice architecture practice

Introduction
Microservice is a software architecture style , decompose applications into independent and scalable services. PHP is a popular backend language that is ideal for building microservices. This article will guide you through a practical case to complete the construction of PHP microservice architecture.

Install the LEMP stack
First, you need to install the LEMP (Linux, Nginx, MySQL, PHP) stack:

# Ubuntu/Debian
sudo apt update && sudo apt install nginx mysql-server php8.1

# CentOS/Fedora
sudo yum update && sudo yum install epel-release
sudo yum install nginx mariadb php81

Create the MySQL database
Next, create a MySQL database for storing data:

CREATE DATABASE micro_services;
GRANT ALL PRIVILEGES ON micro_services.* TO 'user'@'localhost' IDENTIFIED BY 'password';

Install Composer
Composer is a PHP dependency management tool:

sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Building Microservices
Next, create a new Composer project:

composer create-project symfony/skeleton micro_services
cd micro_services

Add the following content to the config/services.yaml file:

services:
    database.connection: # 数据库连接
        class: Doctrine\DBAL\Connection
        arguments:
            dsn: '%env(DATABASE_URL)%'
    monolog.logger: # 日志记录器
        class: Monolog\Logger
        arguments: [micro_services]
        calls:
            - [pushHandler, [new Monolog\Handler\StreamHandler('logs/dev.log')]]

Create src/Entity/User.php entity that maps to the user table in the database:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $email;

    // ...
}

Run the following command to create the database schema:

composer dump-autoload &&
php bin/console doctrine:database:create

Create API Controller
Create an API controller to handle user requests:

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;

class UserController extends AbstractController
{
    /**
     * @Route("/api/users", methods={"GET"})
     */
    public function index(EntityManagerInterface $em): Response
    {
        $users = $em->getRepository(User::class)->findAll();

        return $this->json($users);
    }
}

Run the microservice
Finally, start the PHP built-in server to Run the microservice:

php -S localhost:8000 public/index.php

Visit http://localhost:8000/api/users to get the user list.

The above is the detailed content of PHP microservice architecture practice. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn