Introduction to using Consul to manage Laravel project configuration

Source: Internet
Author: User
Run for more than a year in the pre-production environment and record and share it here.

Questions & Other Solutions

When we need to deploy multiple different projects on multiple servers, we need a centralized tool for configuring information management and synchronization. The scenarios considered are:

    1. Synchronize files with rsync .env : Multiple items are configured slightly cumbersome, to SSH to the server to modify and then synchronize

    2. Docker: Cluster deployment is convenient, you can modify the environment variables with the graphical interface, but update the environment variables to restart the container, most of the items are not migrated to Docker

Here's a look at the Consul + Consul template scheme.

Installation

Both the consul and Consul template provide pre-compiled executables that can be downloaded and decompressed.

Start Consul:

Consul Agent-server-bootstrap-ui-bind <ip>

Other service nodes can be consul agent -join <ip> joined to the cluster.

After startup, Consul is managed through the Consul command, the RESTful API, and the Web UI.

Consul KV

Consul provides a distributed key-value pair store that can be used to store the application's dynamic configuration information, basic operations:

# put set one kv to consul kv put App_debug false# get get consul kv get app_debug# Exprot export Jsonconsul kv exports > data.json# i Mport Import jsonconsul kv import @data. json# Delete Remove Consul kv Delete App_debug

The Consul kv Support directory can be / implemented by separating the key, for example, we use configs/<app_name>/ such a directory to hold all configurations for a project.

Consul Template

Configuration centralized management, how to pass to Laravel? One approach is to use the Consul API in Laravel to get configuration information, the second issue is to listen to the KV changes externally and then write to the .env file, Consul template is such an external tool.

We use the configuration template ( .env.tpl ):

{{range ls ' Configs/crm '}} {{ . Key}}={{. Value}}{{End}}

This means traversing config/crm the KV directory and generating the key=value format.

Run:

# # Format: Template file: Target file: Command executed after update consul-template--template \    /path/to/.env.tpl:/path/to/.env: ' Php/path/to/artisan Config:cache '

You can generate an. env file, and continuously listen for changes, a consul-template instance can have multiple--template multiple parameters, which can generate a configuration file for multiple projects on a single server.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.