HOW TO CREATE MICRO-SERVICES USING LUMEN FRAMEWORK: PART I
What Will I Learn?
In this part, you will learn the following
- What is a Micro-service
- Why you need to make use of micro-services
- What is Lumen
- How to install Lumen
- Setup Lumen development environment
- Setup database environment
- Install Postman on Google Chrome
Requirements
For this tutorial, you will need the following
- A PC/laptop with any Operating system such as Linux, Mac OSX, Windows OS
- Internet Connection
- PHP version >= 7.1
- Composer version >= 1.4.1
- Google Chrome browser
Difficulty
- Intermediate
Tutorial Contents
This is the first of the four series tutorial on how to create micro-services using lumen framework. In this series, you will set up the lumen development environment to enable you get started.
Before then, I will explain some terminologies for you to understand what you are diving into.
What is a micro-service?
A micro-service is a variant of the service-oriented architecture (SOA). It is an approach to developing a single application as a suite of small parts - services communicated through APIs.
Why you need to make use of micro-services
- Scalability
- Protect from failure
What is Lumen?
Lumen is a stunningly fast micro-framework by laravel that is used for the development of lighting fast micro-services and APIs.
Why we use Lumen in building micro-services.
- Micro-services need to possess the ability to read and write to a database, must be secured, built with the so-called message driven approach, and have the ability to provide reports.
- Lumen utilizes all benefits of laravel framework including security, easy database configuration, queue service configuration and so on.
Installing Lumen
To have lumen up and running on your system, you need to make sure you server meets the following requirements:
- PHP >= 7.1
- OpenSSL PHP Extension
- PDO PHP Extension
- Mbstring PHP Extension
Make sure the above listed extensions are set on your server.
Lumen utilizes composer to manage its dependencies so before installing lumen, make sure you have composer already installed. You can download composer here
Next, you are going to install lumen via lumen installer
First, download the Lumen installer using Composer: Open your command prompt window and type in the following code
composer global require "laravel/lumen-installer"
You should see this in your CMD window
Once lumen is installed, you can create a new lumen project folder in any directory of your choice. Let's call your project "eblog".
Type the following code:
lumen new eblog
And wait while your new eblog project is been created.
Once your project folder is created completely, you can serve your project locally by using the following command
php -S localhost:8000 -t public
Open your google chrome browser and type http://localhost:8000/ to confirm your installation is properly done and if it was, you should see this page:
If you see this page, the your install was done perfectly well. So let's move to the next step.
Setup lumen development environment.
The next thing you should do after installing Lumen is set your application key to a random string. The application key is a set of random key that is unique to your app. The key can be set in the .env environment file. Duplicate the .env.example file and rename to .env.
Generate a random key by typing random characters into the APP_KEY variable. This string should be 32 characters long
APP_ENV=local
APP_DEBUG=true
APP_KEY= jdgjdskdsjjkjdjdhghgjdhgjdsjgsmmledhqeevmfporiuevbdk
APP_TIMEZONE=UTC
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
QUEUE_DRIVER=sync
Note: If the application key is not set, your user encrypted data will not be secure!
Setup database environment
Open your database management system, i'm currently making use of HeidiSQL version 9.4.0.5125
There are other options like phpmyadmin which is more preferable.
Create a new database called "blog" and save.
So next, you need to configure your lumen app to make use of the blog database.
Go to .env file, and set the following variable:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=
So your .env file should look like this:
Install Postman on google chrome
Postman is a REST Client that runs as an application inside the Chrome browser. It is very useful for interfacing with REST APIs.
Go to : https://chrome.google.com/webstore/search/postman?_category=extensions
Click on Add to chrome on postman interceptor
So you are now ready to start building your first micro-service.
Posted on Utopian.io - Rewarding Open Source Contributors