I used C++ to implement the [Registration Center] of the microservice, and implemented high availability for this registration center. My service network communicates through TCP. When the service goes online, it will be registered on the [Registration Center], and the [Registration Center] will push This server address goes to [Service Consumer]. In this way, the [Service Consumer] holds all the [Microservice Maps]. My load balancing is done in the [Service Consumer]. To put it bluntly, the [Service Consumer] needs to be resident in memory, so it is very important for Python. Or Java is easier to do, but for PHP using FPM, it is equivalent to a death sentence. How to transform PHP so that traditional FPM PHP can support this architecture. It seems that Dubbo is done in a similar way, so if the front-end is done in PHP, wouldn’t it be a bit of a pain in the ass?
Reply content:
There are two methods of load balancing:Client load balancing, typically Finagle and Dubbo. The advantage is that it is simple to implement, and the service is directly connected and has good performance. The disadvantage is that when using multiple languages, you need to implement a client for each language.
Server-side load balancing, typical load balancers are Nginx and HAProxy. Usually a reverse proxy is added in front of the service. The client communicates with the server through a reverse proxy, and the specific load balancing logic is implemented by the load balancer. The load balancer itself can also be registered with the registry. The advantage is that the client is very simple and supports multiple languages. The disadvantage is that there is a bit of performance loss.
The specific choice depends on the business and the company’s technology selection. In a multi-language scenario, the server-side solution will save a lot of maintenance costs. Of course, if there is a team to maintain the client, I will not mention it. After all, what does the team need to do?
For pure Java scenarios, you have already used Dubbo or a similar framework. If the framework already provides support, don’t bother.
Back to the main scenario, if you have to implement service discovery in the PHP service, you can cache the service registry, such as putting it in memcache, and pull it again every once in a while.
Above The simplest (and probably the most reliable) solution is agent. Run an agent on the PHP server to monitor your registration center. This agent will modify the PHP configuration file after receiving the new address. When PHP is called by FPM Go read this configuration.
Generally speaking, almost all frameworks have configuration files. You can just modify this file directly. The modification does not increase any cost (because you also have to read the configuration file). This design method now has certain advantages, that is, some capabilities that belong to the service bus have been completely down-layered to the service consumer node. In this way, the bus itself has lighter functions and only manages service registration and distribution of registration information. The downtime of the registration center basically has no impact on service consumption and invocation.
In view of the problem you just mentioned, there are two solutions. One is to still improve the capabilities of the service agent back to the bus, including routing and load balancing. If you don't want to do this, you can also consider setting up a separate server to implement service consumption and routing balancing logic on this server. This server can be regarded as the back-end proxy corresponding to the PHP application. I think the use of client load balancing in microservices is a major trend. There are three reasons: first, it has performance advantages; second, it has higher reliability. Even if the registration center is down, it can still run normally; third, the client itself needs to implement circuit breakers and services. For logic such as downgrading, it would be a good idea to implement a simple load balancing logic. I am not very familiar with PHP. Is it possible to consider writing the service configuration into a certain service_config.php file and include this service_config.php in other files? Contains the timestamp of the previously detected file. If it exceeds a certain time (such as 1 minute), the configuration write file will be obtained from the registration center again. Two solutions
1. Swoole
Swoole: PHP’s asynchronous, parallel, high-performance network communication engine
You can use swoole to write a permanent internal test php server
2. Cache
use APCu (PHP: APCu - Manual) or redis, cache the server address list
It depends on the specific performance requirements and the consistency requirements of the service list. The consistency requirements are relatively high, and the cache can be shorter. If you are worried about the network overhead of redis cache reading and writing, you can use APCu This local cache swoole

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

Dependency injection provides object dependencies through external injection in PHP, improving the maintainability and flexibility of the code. Its implementation methods include: 1. Constructor injection, 2. Set value injection, 3. Interface injection. Using dependency injection can decouple, improve testability and flexibility, but attention should be paid to the possibility of increasing complexity and performance overhead.

Implementing dependency injection (DI) in PHP can be done by manual injection or using DI containers. 1) Manual injection passes dependencies through constructors, such as the UserService class injecting Logger. 2) Use DI containers to automatically manage dependencies, such as the Container class to manage Logger and UserService. Implementing DI can improve code flexibility and testability, but you need to pay attention to traps such as overinjection and service locator anti-mode.

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
