Microservices are small services that make up a single application. They run on their own processes and communicate with lightweight mechanisms.

In this article, we’ll explore how to setup a project with many different microservices, and also how to distribute those microservices so that developers can work on them with minimal set up.

For a working example, see our Github.

Setting Up Your Project

We’ll set up our project by splitting up our microservices into different folders. Each folder will have a different installation of Lumen in it. Our reasoning here is that each microservice should be self-sufficient. If one service wants to use PostgreSQL and another wants to use MongoDB, they should be independently able to. The other bonus here is that each service can be deployed on it’s own, which allows us to scale services independently of each other.

For example, if we creating a chat application called awesome-chat, we may want to split up the application into:

  • authentication – for registering and logging users in;
  • chat – for handling rooms, messages, and reading users that belong to those rooms;
  • rtc – for handling our real time connections, which will read and create messages, rooms, and users in real time;
  • mail – for sending confirmation emails, invitations, and password reset emails;
  • .homstead – this is where we will put our configuration for our Homestead instance.
awesome-chat
│ README.md
│
├ .homestead
└ services
  ├ auth
  ├ chat
  ├ rtc
  └ mail

Each folder has it’s own Lumen installation.

In the next section, we’ll use Homestead and Vagrant deploy all of these services locally and be able to hit each one from our host machine.

Setting Up Homestead

Homestead gives two different ways we can install it: globally so that we can work on many different projects in the same environment, or per project, so that we can make a specific configuration for our project. In our case, what we will do since we are developing locally is create one Homestead Vagrant configuration that any developer can launch. The instance will have all our microservices running on it.

In our .homestead directory, we will initialize Homestead and make some changes to our Homestead.yaml file.

vagrant box add laravel/homestead
cd .homestead
composer require laravel/homestead --dev

Then, open up the Homestead.yaml file. I suggest you change the hostname, name, and databases to reflect your project’s name. Then, change your folders and sites to the following:

folders:
    - map: "../services/auth"
      to: "/home/vagrant/awesome-chat/auth"
    - map: "../services/chat"
      to: "/home/vagrant/awesome-chat/chat"
    - map: "../services/rtc"
      to: "/home/vagrant/awesome-chat/rtc"
    - map: "../services/mail"
      to: "/home/vagrant/awesome-chat/mail"

sites:
    - map: auth.awesome-chat.app
      to: "/home/vagrant/awesome-chat/auth/public"
    - map: chat.awesome-chat.app
      to: "/home/vagrant/awesome-chat/chat/public"
    - map: rtc.awesome-chat.app
      to: "/home/vagrant/awesome-chat/rtc/public"
    - map: mail.awesome-chat.app
      to: "/home/vagrant/awesome-chat/mail/public"

 

Running the Environment

In order to actually run the Homestead Vagrant machine, you’ll need to update your hosts file. We’ll simply add entries for all of our subdomains to point to our Vagrant box.

If you are on Mac or Linux machine, updating your hosts file is easy:

sudo vi /etc/hosts

Then add the following entries:

192.168.10.10   auth.project.app
192.168.10.10   chat.awesome-chat.app
192.168.10.10   rtc.awesome-chat.app
192.168.10.10   mail.awesome-chat.app

Once the configuration is saved, you can simply run vagrant up and navigate to auth.awesome-chat.app in your web browser to see the services respond.