Microservice architecture
The concept of microservices was proposed by Martin Fowler in March 2014:
Microservice architecture is an architectural pattern that advocates A single application is divided into a set of small services, and the services coordinate and cooperate with each other to provide ultimate value to users. Each service runs in its own independent process, and services use lightweight communication mechanisms to communicate with each other. Each service is built around a specific business and can be independently deployed to production environments, production-like environments, etc. In addition, a unified and centralized service management mechanism should be avoided as much as possible. For a specific service, appropriate languages and tools should be selected to build it based on the business context.
The following picture is a microservice architecture diagram of an e-commerce system:
Microservice architecture has the following advantages compared with single applications:
1. Each service is relatively simple and only focuses on one business function;
2. The microservice architecture is loosely coupled, and each service can be independently tested, deployed, upgraded, and released;
3. Each microservice can be developed independently by different teams, and each can choose the best and most appropriate programming languages and tools;
4. Each service can be leveled according to needs Expand and improve system concurrency capabilities.
There is no silver bullet. While the microservice architecture brings many advantages, it also has the following disadvantages:
1. The microservice architecture increases the complexity of the system and increases the operation and maintenance overhead. and cost. For example, a single application may only need to be deployed to a small application service cluster, while a microservice architecture may require building/testing/deploying/running dozens of independent services, and may need to support multiple languages and environments;
2. As a distributed system, the microservice architecture introduces several other issues, such as message serialization, network delay, asynchronous mechanism, fault tolerance processing, service avalanche, etc.;
3. Service management Complexity, such as service registration, discovery, downgrade, circuit breaker and other issues;
4. There are mutual calls between services, which brings huge challenges to troubleshooting system faults.
It can be said that it is precisely the system of traditional application architecture that has become increasingly bloated and faced problems that are difficult to maintain and expand. At the same time, the booming development of containerization technology (Docker) and the increasing maturity of DevOps ideas have given rise to new Architectural Design Style – The emergence of microservices architecture.
RPC Framework
The various services in the microservice architecture are usually not on the same machine, or even in the same network environment, so how do the microservices interact with each other? Calling is an urgent problem that needs to be solved. We usually use the RPC protocol to solve it:
RPC (Remote Procedure Call), that is, remote procedure call, is a computer communication protocol. This protocol allows a program running on one computer to call a subroutine on another computer without the programmer having to additionally program the interaction. ——Wikipedia
implements the framework of the RPC protocol, which allows the server and the caller to shield various underlying details, allowing the caller to call remote functions (services) just like calling local functions. The RPC framework generally provides serialization, deserialization, connection pool management, load balancing, failover, queue management, timeout management, asynchronous management and other functions for the server and client. I found a diagram illustrating the working principle of the RPC framework on the Internet:
#Currently, according to the different technologies used to serialize data, it can be divided into two types: JSON-RPC and gRPC:
1. JSON-RPC is a lightweight RPC protocol standard based on JSON format, which can be transmitted based on HTTP protocol or directly based on TCP protocol. The advantage of JSON-RPC is that it is easy to use and read.
2. gRPC is a high-performance, general-purpose open source RPC framework. It is designed by Google mainly for mobile application development and based on the HTTP/2 protocol standard. It is developed based on the ProtoBuf (Protocol Buffers) serialization protocol, and Supports many development languages. gRPC has the advantages of low latency, high efficiency, high scalability, and support for distribution.
Consul
Now with the RPC framework, we can only consider the business calls between services without considering the underlying transmission details. At this time, if service A wants to call service B, we can configure the IP address and port of service B in service A, and then leave the remaining transmission details to the RPC framework. This is no problem when the scale of microservices is small, but it will face huge challenges when the scale of services is large and more than one instance of each service is deployed. For example, service B has three instances deployed. At this time, service A wants to call service B. Which instance IP should it request? If two of the three instances deployed by service B are down, service A may still request the hung up instances, and the service will be unavailable. It is very inflexible to write IP addresses and ports into configuration files. Microservice architecture often needs to ensure high availability and dynamic scaling.
Therefore, we need a service registration and service discovery tool that can dynamically change service information and find the IP addresses and ports of available services. There are currently many service discovery tools on the market, such as Consul, ZooKeeper, Etcd, Doozerd, etc. This article mainly takes the Consul software as an example.
Consul is a service software that supports multi-data center, distributed high-availability service discovery and configuration sharing. It was developed by HashiCorp in Go language and is open source based on the Mozilla Public License 2.0 agreement. Consul supports health checks and allows HTTP, gRPC, and DNS protocols to call APIs to store key-value pairs.
The following is the architecture diagram after the introduction of service registration and service discovery tools:
In this architecture:
First the instance of S-B After starting, register its own service information (mainly the IP address and port number of the service) into Consul.
Consul will perform health checks on all registered services to determine which service instances are available and which are not.
After S-A starts, it can obtain the IPs and ports of all healthy S-B instances by accessing Consul, and put this information into its own memory. S-A can use this information to call S-B.
S-A can update the service information of S-B stored in the memory by listening to Consul. For example, if S-B-1 hangs up, the health check mechanism will mark it as unavailable. Such information changes will be monitored by S-A, and S-A will update the service information of S-B-1 in its own memory.
It can be seen that in addition to the functions of service registration and service discovery, Consul software also provides health check and status change notification functions.
Hyperf
For Java developers, there are quite mature Dubbo and Spring Cloud microservice frameworks to choose from. As a PHPer, I used Google to check "PHP microservices" and found that there was very little useful related content, no substantial reference value, and I was infinitely disappointed. . . Fortunately, some experts have implemented the high-performance and highly flexible PHP coroutine framework Hyperf based on the Swoole extension, and provided related components of the 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 qualitatively improved compared to the traditional framework based on PHP-FPM, and it provides While maintaining ultra-high performance, it also maintains extremely flexible scalability. Standard components are implemented based on the PSR standard and are based on a powerful dependency injection design, which 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/p…. The project is built using Dokcer. The docker-compose.yml code 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=eth0 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
Here a Consul container consul-server-leader is started as a component of service registration and service discovery. The containers microservice-1 and microservice-2 are respectively Provides addition and division operations. As the service caller, the container app configures the URL of the consul-server-leader container, obtains the IP addresses and ports of the microservice-1 and microservice-2 services by accessing consul-server-leader, and then the app calls addition and division through the RPC protocol. The computing service obtains the results and returns 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 App\Controller\IndexController controller:
public function add(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调用 ]; }
The implementation of add in App\JsonRpc\AdditionService:
class AdditionService extends AbstractServiceClient { /** * 定义对应服务提供者的服务名称 * @var string */ protected $serviceName = 'AdditionService'; /** * 定义对应服务提供者的服务协议 * @var string */ protected $protocol = 'jsonrpc-http'; public function add(int $a, int $b): int { return $this->__request(__FUNCTION__, compact('a', 'b')); } }
Inherits AbstractServiceClient to create a microservice client End 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 PHP microservice cluster construction - Hyperf. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.