Home  >  Article  >  Backend Development  >  How to build microservices in php

How to build microservices in php

(*-*)浩
(*-*)浩Original
2019-09-30 14:49:584356browse

How to build microservices in php

Hyperf (Recommended learning: PHP video tutorial)

For Java developers, the technology is quite mature There are many microservice frameworks to choose from:

[Dubbo](https://dubbo.apache.org/zh-cn/)
[Spring Cloud](https://www.springcloud.cc/)

As a PHPer, I checked "PHP microservices" on Google and found that there was very little useful related content and no substantial reference value. Infinite melancholy.

Fortunately, a master has implemented a high-performance and highly flexible PHP coroutine framework [Hyperf](https://www.hyperf.io/) based on the Swoole extension. And provides related components of microservice architecture.

Hyperf is a high-performance and highly flexible PHP coroutine framework based on `Swoole 4.3`. It has a built-in coroutine server and a large number of commonly used components. Its performance is better than traditional ones based on `PHP-FPM`. The framework has been qualitatively improved, providing ultra-high performance while also maintaining extremely flexible scalability. Standard components are implemented based on [PSR standard](https://www.php-fig.org/psr), based on powerful The dependency injection design ensures that most components or classes are `replaceable` and `reusable`.

So, after learning the basic knowledge related to microservice architecture, I used the Hyperf framework to build a PHP-based microservice cluster. This is the project source code address:

https://github.com/Jochen-z/php-microservice-demo

This project is built using Dokcer. The code of `docker-compose.yml` is as follows:

version:"3"
services:
consul-server- leader:
image:consul:latest
container_name:consul-server-leader
command:"agent -server -bootstrap -ui -node=consul-server-leader -client=0.0.0.0"
environment:
- CONSUL_BIND_INTERFACE=eth 0
ports:
- "8500:8500"
networks:
- microservice
microservice- 1:
build:
context:.
container_name:"microservice-1"
command:"php bin/hyperf.php start"
depends_on:
- "consul-server-leader"
volumes:
- ./www/microservice- 1:/var/www
networks:
- microservice
tty:true
microservice- 2:
build:
context:.
container_name:"microservice-2"
command:"php bin/hyperf.php start"
depends_on:
- "consul-server-leader"
volumes:
- ./www/microservice- 2:/var/www
networks:
- microservice
tty:true
app:
build:
context:.
container_name:"app"
command:"php bin/hyperf.php start"
depends_on:
- "microservice-1"
volumes:
- ./www/ web:/var/www
ports:
- "9501:9501"
networks:
- microservice
tty:true
networks:
microservice:
driver:bridge
volumes:
microservice:
driver:local

A Consul container `consul-server-leader` is started here as a service registration and service The discovered components, containers `microservice-1` and `microservice-2` provide services for addition and division operations respectively.

Container `app`, as the service caller, configures the URL of the `consul-server-leader` container, and obtains the `microservice-1` and `microservice-2` services by accessing `consul-server-leader` IP address and port, and then `app` calls the addition and division services through the RPC protocol to obtain the results and return them to the user.

The `app` container is a web application that deploys a Hyperf project and provides HTTP services to the outside world.

For example, there is an `add` method in the `AppControllerIndexController` controller:

publicfunctionadd(AdditionService $addition)
{
$a = (int) $this->request->input( 'a', 1); # 接受前端用户参数
$b = (int) $this->request->input( 'b', 2);
return[
'a'=> $a,
'b'=> $b,
'add'=> $addition->add($a, $b) # RPC调用
];
}
在 `AppJsonRpcAdditionService` 中 `add` 的实现:
classAdditionServiceextendsAbstractServiceClient
{
/**
* 定义对应服务提供者的服务名称
* @varstring
*/
protected$serviceName = 'AdditionService';
/**
* 定义对应服务提供者的服务协议
* @varstring
*/
protected$protocol = 'jsonrpc-http';
publicfunctionadd(int $a, int $b): int
{
return$this->__request( __FUNCTION__, compact( 'a', 'b'));
}
}

Inherits `AbstractServiceClient` to create a microservice client request class, Hyperf helps us implement the details of interaction with Consul and service providers at the bottom level. We only need the `add` method in the `AdditionService` class to remotely call the services provided by `microservice-1` and `microservice-2`.

At this point, the PHP microservice cluster construction is completed!

The above is the detailed content of How to build microservices in php. 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
Previous article:How to open a file in phpNext article:How to open a file in php