Giter Site home page Giter Site logo

ahmedalaahagag / microservices-php-project Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 5.0 35 KB

PHP example project on how to use microservices with api gateway and 2 microservices

microservice laradock php apigateway api-gateway microservices-architecture microservice-framework

microservices-php-project's Introduction

What is this project

This project was done as a part of 3 Microservices project using Lumen and Laradock demoing the usage of microservices.

What is a microservices

file

Microservices are a software development technique —a variant of the service-oriented architecture (SOA) structural style— that arranges an application as a collection of loosely coupled services.[1] In a microservices architecture, services are fine-grained and the protocols are lightweight. https://en.wikipedia.org/wiki/Microservices

This is being to decouple systems and avoid single points of failuers and faster development and deployment and scalability.

Lumen is designed for building lightning fast micro-services and APIs

Laradock is a full PHP development environment for Docker that Includes prepackaged Docker Images, all preconfigured to provide a wonderful PHP development environment. Laradock is well known in the Laravel/lumen community, as the project started with single focus on running Laravel projects on Docker.

Requirements

To be able to run this project one needs the following technologies:

  • Docker -> Laradock uses Docker

  • Composer -> No need if one uses Laradock/Docker

if one want to start Lumen from scratch Composer is need as a package manager.

Instructions

  1. clone the first microservice https://github.com/ahmedalaahagag/books-microserivce follow the readme instructions section.

  2. clone the second microservice https://github.com/ahmedalaahagag/authors-microserivces follow the readme instructions section.

  3. clone the api-gateway microservice https://github.com/ahmedalaahagag/api-gateway-php follow the readme instructions section.

  4. clone the laradock repository from https://github.com/Laradock/laradock.git

  5. now you should have a dir with Authors,Books,ApiGateWay,laradock dirs inside

To run this project

  1. cd laradock

  2. cp env-example .env

  3. Attached is a laradock docker-compose.yml file in this repo copy it to laradock

  4. Attached is 3 nginx config files which holds the configs related to each of the microservices gateway.conf,books.conf,authors.conf copy those files to laradock/nginx/sites

  5. docker-compose up -d nginx mysql phpmyadmin workspace => To start the server

  6. docker-compose exec workspace bash => to get access to virtual machine and here one can execute any artisan command

Remember all the Docker commands have to be run it under Laradock folder as there the Docker files are placed.

If one wants to run this project as it is after composer install run migration as php artisan migrate to update the DB with the right tables. Then seed with php artisan db:seed to populate the DB with some fake data.

Adding a new microservice

After writing your microservice code

  1. Add the new nginx config file like gateway.conf and change the port number inside the new file ex.laradock/nginx/sites/newmicroservice.conf

server {

    listen "81" default_server;
    listen [::]:"81" default_server ipv6only=on;

    # For https
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server ipv6only=on;
    # ssl_certificate /etc/nginx/ssl/default.crt;
    # ssl_certificate_key /etc/nginx/ssl/default.key;

    server_name localhost;
    root /var/www/Books/public;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
}

Change the port to the one that you want the microservice to run on . in .env file add 2 new keys

MICRO_SERVICE_BASE_URI="http://IP_ADDRESS:PORT" MICRO_SERVICE_SECRET=

Which is how your api gateway gets to your microservice

  1. in app/Http/Controllers dir add the oprations that you want to perform with your api gatway

  2. in app/Http/Services dir add the api call using ConsumeExternalService trait or write your own ConsumeExternalService is a guzzle wrapper http://docs.guzzlephp.org/en/stable/

  3. in routes/web.php add your new routes

And like this you’ve added a new microservice.

Note : This is not the best practice though read more on service discovery https://www.nginx.com/blog/service-discovery-in-a-microservices-architecture/

microservices-php-project's People

Contributors

ahmedalaahagag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.