What is Microservices? Understanding PHP Microservices

5.0/5 (1 Reviews)

Tóm Tắt

What is Microservices?

Microservices is an architectural style that structures an application as a collection of services that are

  • Highly maintainable and testable
  • Loosely coupled
  • Independently deployable
  • Organized around business capabilities.

The microservice architecture enables the continuous delivery/deployment of large, complex applications. It also enables an organization to evolve its technology stack.

Microservice architecture, or simply microservices is a distinctive method of developing software systems that tries to focus on building single-function modules with well-defined interfaces and operations. The trend has grown popular in recent years as Enterprises look to become more Agile and move towards a DevOps and continuous testing. 

What is Microservices?

Microservices have many benefits for Agile and DevOps teams - as Martin Fowler points out, Netflix, eBay, Amazon, Twitter, PayPal, and other tech stars have all evolved from monolithic to microservices architecture. Unlike microservices, a monolith application is built as a single, autonomous unit. This make changes to the application slow as it affects the entire system.  A modification made to a small section of code might require building and deploying an entirely new version of software.  Scaling specific functions of an application, also means you have to scale the entire application.

The microservice architecture is SRP principle.It’s a kind of Single Responsibility principle.Each service has own unique responsibility.DB schema should be decomposed also.Exporting services as rest inside a monolithic app not convert a monolithic app to micro service application.

Microservices can be characterized by the following traits:

  • Size: A microservice is very targeted in functionality and scope. As such, it’s code base will be relatively small and manageable.
  • Loosely coupled: By passing messages between services via an appropriate protocol, we decouple resources in the service from the underlying technologies and topologies.
  • Self monitoring: Or at least easily testable.
  • Continuous deployment: However this requires good Devops, automation, and acceptance testing practices and tooling.
  • May even be disposable: The system they’re in may be long-lived but they may be short-lived.

Microservice Architecture

You are developing a server-side enterprise application. It must support a variety of different clients including desktop browsers, mobile browsers and native mobile applications. The application might also expose an API for 3rd parties to consume. It might also integrate with other applications via either web services or a message broker. The application handles requests (HTTP requests and messages) by executing business logic; accessing a database; exchanging messages with other systems; and returning a HTML/JSON/XML response. There are logical components corresponding to different functional areas of the application.

Microservice Architecture

Characteristics of Microservices

The main characteristics of microservice architectural style are defined by the principles of loose coupling and high cohesion of services:

  • Applications are made up of small independent services.
  • Functionalities of the application are distributed among different services.
  • Services are independent of each other, separate pieces of code.
  • Services are small complete units of functionality.
  • Services are independently modifiable and (re)deployable.
  • Services communicate through well-defined technology agnostic protocols such as REST (HTTP protocol) or RPC (using it’s own TCP protocol)
  • Logic related to a particular service should be kept in a single place.
  • Microservices based system architecture and continuous deployment is a match made in heaven.
  • Decentralized data management: each service can have its own database.

php Microservices

PHP Microservices

One of the most prominent ways to create access to a microservice today is through a RESTful API. REST has great advantages over other protocols like sockets that include:

  1. Non-blocking I/O for the client when implemented in languages like Javascript
  2. Easy to manage resources, meaning its easy to organize and group calls together.
  3. The syntax is easy to follow because it follows HTTP standards
  4. The endpoints are stateless, aka idempotent. This reduces the side effects of unexpected outputs occurring.
  5. Easy to implement CRUD operations — Create, Read, Update, Delete.

Restful over HTTP normally breaks down into 4 types of requests of GET, POST, PUT and DELETE. There are other request types that are outside of the scope of this article. The requests can be described as:

  • GET: Normally maps to an operation of retrieving data. In CRUD, this is your READ.
  • POST: Used for creating data, or in CRUD the CREATE.
  • PUT: Called when a user wants to update data. Is the U in CRUD.
  • DELETE: Used to remove data. And lastly, the DELETE in CRUD.

In this tutorial, we are going to create a small application that executes those 4 methods and represents how we can give access to a microservice. This article will NOT cover areas such as security or access control.