Download the PHP package erikwang2013/consul-php without Composer

On this page you can find all versions of the php package erikwang2013/consul-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package consul-php

erikwang2013/consul-php

PHP Consul 客户端,完整覆盖 Consul HTTP API v1,重点支持服务注册发现与配置中心。核心包零框架依赖,内置 Laravel / Hyperf / webman / ThinkPHP 适配,一个 composer require 即可在任何框架下使用。

PHP 8.0+ · PSR-18/PSR-3/PSR-14/PSR-16 · 零框架依赖


文档导航

文档 链接
文档总目录 docs/README.md
Laravel 集成 见下方 Laravel
Hyperf 集成 见下方 Hyperf
webman 集成 见下方 webman
ThinkPHP 集成 见下方 ThinkPHP
设计文档 docs/superpowers/specs/2026-05-14-consul-php-design.md

框架集成一览

Laravel Hyperf webman ThinkPHP
扩展包 内置 内置 内置 内置
注入方式 自动发现 + ServiceProvider 自动发现 + ConfigProvider 手动 new / 插件 手动 bind 到容器
便捷访问 Consul Facade #[Inject] 注解 app('consul') 助手
配置位置 config/consul.php config/autoload/consul.php config/plugin/erikwang2013/consul-php/app.php config/consul.php
HTTP 客户端 Guzzle (PSR-18) Swoole 协程客户端 Guzzle (PSR-18) Guzzle (PSR-18)
缓存 Laravel Cache (PSR-16) Hyperf Cache (PSR-16) 自行注入 自行注入
热更新运行 Artisan 命令 AbstractProcess 协程 Worker 进程 Timer / Swoole 进程
事件监听 EventServiceProvider Hyperf Event ThinkPHP Listener
文档 源码 源码 源码 源码

同一操作,不同写法

获取客户端:

框架 写法
通用 $client = new ConsulClient(['base_uri' => '...']);
Laravel $client = app(ConsulClient::class);Consul::kv->get(...)
Hyperf #[Inject] private ConsulClient $consul;
webman $client = new ConsulClient(['base_uri' => '...']);
ThinkPHP $client = app('consul');

服务注册:

配置读取:

热更新运行方式:

框架 启动命令 / 方式 运行环境
Laravel php artisan consul:watch 独立 Artisan 进程
Hyperf ConsulWatchProcess (自动启动) Swoole 协程
webman onWorkerStart 中 fork Worker 进程
ThinkPHP Timer::setInterval / Swoole Process 独立进程

安装

框架集成

框架适配已内置在核心包中,无需额外安装。安装核心包后,对应框架会自动发现并注册 Consul 服务:


快速开始(通用)

Token 会自动通过 X-Consul-Token 请求头附加到所有请求中。

服务注册

支持 TTL、HTTP、TCP、gRPC 四种健康检查模式。

服务发现

内置 RoundRobin(默认)和 Random 两种负载均衡策略。

配置中心

热更新原理: 优先 Consul blocking query(index 长轮询),网络异常时自动降级为定时轮询,连接恢复后自动切回长轮询。回调 + PSR-14 EventDispatcher 双通道通知。

缓存策略: 注入 PSR-16 缓存后,get()namespace() 自动读写缓存。Watcher 始终读 Consul 实时数据,不走缓存。

KV 存储

健康检查 API

Session / 分布式锁

ACL

异步客户端

注意: 异步客户端基于 Promise 模式,适用于需要并发请求的场景。Hyperf 协程环境中默认的 HTTP 客户端即可实现协程级并发。


各框架集成指南

Laravel

Laravel 自动发现 ConsulServiceProvider,无需手动注册。

.env 中设置 CONSUL_BASE_URI,之后即可通过依赖注入或 Facade 使用。Laravel 扩展自动注入 PSR-18 客户端、PSR-16 缓存、PSR-3 日志和 PSR-14 事件分发器。

Hyperf

Hyperf 自动发现 ConfigProvider,无需手动注册。

Hyperf 扩展自动注册 ConsulClient 到 DI 容器,HTTP 请求默认使用 Swoole 协程客户端。服务注册建议放在 MainServerStart 事件监听中,热更新使用 AbstractProcess 在协程中运行。

webman

webman 自动发现插件,composer install 时自动复制配置文件到 config/plugin/erikwang2013/consul-php/。由于 webman 是常驻内存架构,服务注册放在 onWorkerStart 回调中,全局只需注册一次。

ThinkPHP

ThinkPHP 无自动发现机制,需手动注册 Service。将配置文件复制到 config/consul.php,然后在 app/service 目录下注册 ConsulService

或在 app/AppService.php 中直接绑定:


自定义 HTTP 客户端


API 模块速查

属性 主要方法
$client->kv Api\Kv get put delete all keys
$client->agent Api\Agent members self registerService deregisterService checks services
$client->catalog Api\Catalog register deregister nodes services service node
$client->health Api\Health service node checks state
$client->session Api\Session create destroy renew info all node
$client->acl Api\Acl token* policy* role* authMethod* login logout bootstrap
$client->event Api\Event fire list
$client->status Api\Status leader peers
$client->coordinate Api\Coordinate datacenters nodes node
$client->operator Api\Operator raftConfig autopilotConfig keyring(常量:KEYRING_LIST KEYRING_INSTALL KEYRING_USE KEYRING_REMOVE
$client->snapshot Api\Snapshot save(返回原始快照字节,通过 getRaw()restore(发送原始字节,通过 putRaw()

高层封装:

方法 返回 说明
$client->serviceRegistry() Service\Registry 服务注册/心跳/下线
$client->serviceDiscovery() Service\Discovery 实例列表/负载均衡/变更监听
$client->configCenter() Config\ConfigCenter 配置读写/缓存/热更新

异常体系

所有异常继承 ConsulException(继承 RuntimeException):


架构


最低要求

开源不易,欢迎支持

微信 支付宝

License

MIT


All versions of consul-php with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
psr/http-client Version ^1.0
psr/http-message Version ^1.0|^2.0
psr/http-factory Version ^1.0
psr/log Version ^1.0|^2.0|^3.0
psr/event-dispatcher Version ^1.0
psr/simple-cache Version ^1.0|^2.0|^3.0
psr/cache Version ^1.0|^2.0|^3.0
php-http/discovery Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package erikwang2013/consul-php contains the following files

Loading the files please wait ...