PHP microservices has always been less available than Java microservices, but some of the components of Java microservices are reusable in PHP. Take the configuration center component as an example to see how to use 😀 in PHP
As an indispensable component of microservices, the configuration center nacOS is integrated in laravel
Nacos is a micro-service configuration center developed by Alibaba, see: NACOS
The following uses the PHP nacOS client php-nacos written by the author: github.com/neatlife/ph… Welcome star, welcome PR (beg star, beg star, beg star)
Start the NACOS configuration center
It can be started using Docker
git clone https://github.com/nacos-group/nacos-docker.git
cd nacos-docker
docker-compose -f example/standalone-derby.yaml up
Copy the code
Then open the local IP :8848 port to see the effect. The default login user name and password are nacos
And then I’m going to click +, create a new configuration, and I’m going to copy the contents of the.env file from Laravel, dataid is usually the name of the application, you can always put the name of the application, and you’re going to use this parameter when you get the configuration
Delete the. Env file in the project first, then get the configuration from the configuration center, delete and view the effectThe project is reported at 500, and this is the expected effect
Download the PHP-nacos client
One-click download can be made using Composer:
composer require alibaba/nacos
Copy the code
Write the configuration of the NACOS configuration center to the environment variable
Edit the /etc/environment file. The core content is as follows
LARAVEL_NACOS_HOST= your nacOS configuration center address and port, such as http://127.0.0.1:8848
LARAVEL_ENV= App running environment, such as dev, local, etcLARAVEL_NACOS_DATAID= Configured dataidLARAVEL_NACOS_GROUPID= Configured groupidCopy the code
Pass the environment variables to PHP-FPM
Modify the file: / usr/lib/systemd/system/PHP – FPM. Service in [service] stanza add: EnvironmentFile = / etc/environment
/usr/local/ PHP /etc/php-fpm. Conf add: clear_env = no under [WWW
Then reload the php-fpm service with the following command:
systemctl daemon-reload
systemctl stop php-fpm
systemctl start php-fpm
Copy the code
Replace the. Env file of Laravel with the configuration of the Nacos configuration center
In the Laravel project, delete the. Env file and modify the bootstrap/app.php file. At the beginning of the file, add the code to get the nacOS configuration center configuration file
\alibaba\nacos\NacosConfig::setSnapshotPath(dirname(__DIR__)."/nacos/config");
(new \Dotenv\Loader([], new \Dotenv\Environment\DotenvFactory(), true))->loadDirect(
\alibaba\nacos\failover\LocalConfigInfoProcessor::getSnapshot(
getenv("LARAVEL_ENV"),
getenv("LARAVEL_NACOS_DATAID"),
getenv("LARAVEL_NACOS_GROUPID"),
getenv("LARAVEL_NACOS_NAMESPACEID")? :""));Copy the code
Refer to app.php for the complete code
Periodically obtain the latest configuration
Create a command artisan make: Command NacosRefreshConfig in laravel and then edit the newly created file to get the configuration file in the command body. The core code is as follows:
// load nacos config file
(new \Dotenv\Loader([], new \Dotenv\Environment\DotenvFactory(), true))->loadDirect(
\alibaba\nacos\Nacos::init(
getenv("LARAVEL_NACOS_HOST"),
getenv("LARAVEL_ENV"),
getenv("LARAVEL_NACOS_DATAID"),
getenv("LARAVEL_NACOS_GROUPID"),
getenv("LARAVEL_NACOS_NAMESPACEID")? :"",
)->runOnce()
);
Copy the code
See nacosrefreshconfig.php for the complete code
Create a Linux cron to execute the above commands, such as the first second of every minute to get the configuration file 1 */1 * * * PHP path/to/artisan nacos:refresh
To see the effect immediately, run this command manually
php artisan nacos:refresh
Copy the code
After this scheduled task is executed, the configuration file required for the project is pulled to the nacos directory and opened in a browser to see the effect
You can see that the project has successfully obtained the configuration file and is running properly
A couple of points to note
In addition to the configuration center, microservices are also very important fuse downgrading and link tracking
Fuse downgrading can be done using ISTIO, refer to the fuse of ISTIO
Link tracking can use SkyWalking or Pinpoint to do, of course, can also use isTIO link tracking function, see: distributed tracking
The configuration can be saved using the Docker integrated with nacOS and mysql, see: standalone-mysql.yaml
You can run laravel application as a Docker image on Kubernetes, build kubernetes cluster can refer to the author of another blog: install K8S cluster
The full case code has been uploaded to github: github.com/neatlife/la…
If you are interested in the development of micro-services, you can add the author’s wechat to discuss, or mention PR and issue on Github