1. Go to this page and download the library: Download easyswoole/consul library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
easyswoole / consul example snippets
use EasySwoole\Consul\Config;
use EasySwoole\Consul\Consul;
// config默认 127.0.0.1:8500/v1
$config = new Config([
'IP' => '127.0.0.1',
'port' => '8500',
'version' => 'v1',
]);
$consul = new Consul($config);
// 两种写法,结果相同
$config = new Config();
$config->setIP('127.0.0.1');
$config->setPort('8500');
$config->setVersion('v1');
$consul = new Consul($config);
// Bootstrap ACLs
$bootstrap = new Bootstrap();
$this->consul->acl()->bootstrap($bootstrap);
// Check ACL Replication
$replication = new Replication();
$this->consul->acl()->replication($replication);
// Translate Rules
// Translate a Legacy Token's Rules
$translate = new Translate([
'accessor_id' => $accessor_id
]);
$this->consul->acl()->translate($translate);
// Login to Auth Method
$login = new Login([
"authMethod" => $authMethod,
"bearerToken" => $bearerToken
]);
$this->consul->acl()->login($login);
// Logout from Auth Method
$logout = new Logout([
'token' => $header['token']
]);
$this->consul->acl()->logout($logout);
// Create a Token
$token = new Token([
"description" => "Agent token for 'node1'",
"Policies" => [
["ID" => "165d4317-e379-f732-ce70-86278c4558f7"],
["Name" => "node-read"],
],
"Local" => false,
]);
$this->consul->acl()->token($token);
// Read a Token
$token = new Token([
"AccessorID" => "6a1253d2-1785-24fd-91c2-f8e78c745511"
]);
$this->consul->acl()->readToken($token);
// Read Self Token
$self = new Token\GetSelf([
'token' => "6a1253d2-1785-24fd-91c2-f8e78c745511"
]);
$this->consul->acl()->self($self);
// Update a Token
$update = new Token([
'accessorID' => '6a1253d2-1785-24fd-91c2-f8e78c745511',
"Description" => "Agent token for 'node1'",
"Policies" => [],
"local" => false
]);
$this->consul->acl()->updateToken($update);
// Clone a Token
$clone = new Token\CloneToken([
'accessorID' => '8f246b77-f3e1-ff88-5b48-8ec93abf3e05',
"description" => "Clone of Agent token for 'node1'",
]);
$this->consul->acl()->cloneToken($clone);
// Delete a Token
$delete = new Token([
'AccessorID' => '8f246b77-f3e1-ff88-5b48-8ec93abf3e05'
]);
$this->consul->acl()->delete($delete);
// List Tokens
$token = new Tokens();
$this->consul->acl()->tokens($token);
// Create a Policy
$policy = new Policy([
"Name" => "node-read",
"Description" => "Grants read access to all node information",
"Rules" => "node_prefix \"\" { policy = \"read\"}",
"datacenters" => ["dc1"]
]);
$this->consul->acl()->policy($policy);
// Read a Policy
$policy = new Policy([
'id' => 'c01a1f82-44be-41b0-a686-685fb6e0f485',
]);
$this->consul->acl()->readPolicy($policy);
// Update a Policy
$policy = new Policy([
"ID" => "c01a1f82-44be-41b0-a686-685fb6e0f485",
"Name" => "register-app-service",
"Description" => "Grants write permissions necessary to register the 'app' service",
"Rules" => "service \"app\" { policy = \"write\"}",
]);
$this->consul->acl()->updatePolicy($policy);
// Delete a Policy
$policy = new Policy([
'id' => 'c01a1f82-44be-41b0-a686-685fb6e0f485'
]);
$this->consul->acl()->deletePolicy($policy);
// List Policies
$policies = new Policies();
$this->consul->acl()->policies($policies);
// Create a Role
$role = new Role([
"name" => "example-role",
"description" => "Showcases all input parameters",
]);
$this->consul->acl()->role($role);
// Read a Role
$role = new Role([
'id' => 'aa770e5b-8b0b-7fcf-e5a1-8535fcc388b4'
]);
$this->consul->acl()->readRole($role);
// Read a Role by Name
$name = new Role([
'name' => 'example-role'
]);
$this->consul->acl()->readRoleByName($name);
// Update a Role
$role = new Role([
'id' => 'aa770e5b-8b0b-7fcf-e5a1-8535fcc388b4',
"name" => "example-two",
]);
$this->consul->acl()->updateRole($role);
// Delete a Role
$role = new Role([
'id' => 'aa770e5b-8b0b-7fcf-e5a1-8535fcc388b4'
]);
$this->consul->acl()->deleteRole($role);
// List Roles
$roles = new Roles();
$this->consul->acl()->roles($roles);
// Apply Configuration
$config = new \EasySwoole\Consul\Request\Config([
'Kind' => 'service-defaults',
'Name' => 'web',
'Protocol' => 'Http'
]);
$this->consul->config()->config($config);
// Get Configuration
$config = new \EasySwoole\Consul\Request\Config([
'Kind' => 'service-defaults',
'name' => 'web',
]);
$this->consul->config()->getConfig($config);
// List Configurations
$config = new \EasySwoole\Consul\Request\Config([
'Kind' => 'service-defaults'
]);
$this->consul->config()->listConfig($config);
// Delete Configuration
$config = new \EasySwoole\Consul\Request\Config([
'Kind' => 'service-defaults',
'name' => 'web',
]);
$this->consul->config()->deleteConfig($config);
// List CA Root Certificates
$roots = new Roots();
$this->consul->connect()->roots($roots);
// Get CA Configuration
$configuration = new Configuration();
$this->consul->connect()->configuration($configuration);
// Update CA Configuration
$configuration = new Configuration([
'Provider' => 'consul',
'Config' => [
'LeafCertTTL' => '72h'
]
]);
$this->consul->connect()->updateConfiguration($configuration);
// Create Intention
$intentions = new Intentions([
'SourceName' => 'web',
'DestinationName' => 'db',
'SourceType' => 'consul',
'action' => 'allow'
]);
$this->consul->connect()->intentions($intentions);
// Read Specific Intention
$intentions = new Intentions([
'uuid' => 'e9ebc19f-d481-42b1-4871-4d298d3acd5c',
]);
$this->consul->connect()->readIntention($intentions);
// List Intentions
$intentions = new Intentions();
$this->consul->connect()->listIntention($intentions);
// Update Intention
$intentions = new Intentions([
'uuid' => 'b40faaf3-34aa-349f-3cf2-f5d720240662',
'description' => 'just a test description',
'SourceName' => '',
'DestinationName' => '',
'Action' => 'allow'
]);
$this->consul->connect()->updateIntention($intentions);
// Delete Intention
$intentions = new Intentions([
'uuid' => 'b40faaf3-34aa-349f-3cf2-f5d720240662',
]);
$this->consul->connect()->deleteIntention($intentions);
// Check Intention Result
$intentions = new Intentions\Check([
'source' => 'web',
'destination' => 'db',
]);
$this->consul->connect()->check($intentions);
// List Matching Intentions
$intentions = new Intentions\Match([
'by' => 'source',
'name' => 'web',
]);
$this->consul->connect()->match($intentions);
// Read WAN Coordinates
$datacenters = new Datacenters();
$this->consul->coordinates()->datacenters($datacenters);
// Read LAN Coordinates for all nodes
$nodes = new Nodes([]);
$this->consul->coordinates()->nodes($nodes);
// Read LAN Coordinates for a node
$node = new Node([
'node' => '2456c2850382',
]);
$this->consul->coordinates()->node($node);
// Update LAN Coordinates for a node
$update = new Update([
'dc' => 'dc1',
'node' => '2456c2850382',
'Segment' => 'update',
"Coord" => [
"Adjustment" => 0,
"Error" => 1.5,
"Height" => 0,
"Vec" => [0, 0, 0, 0, 0, 0, 0, 0]
]
]);
$this->consul->coordinates()->update($update);
// Fire Event
$fire = new Fire([
'name' => 'consul',
'dc' => 'dc1',
]);
$this->consul->event()->fire($fire);
// List Events
$listEvent = new ListEvent([
'name' => 'consul',
]);
$this->consul->event()->listEvent($listEvent);
// List Checks for Node
$node = new Node([
'node' => '2456c2850382',
'dc' => 'dc1',
]);
$this->consul->health()->node($node);
// List Checks for Service
$checks = new Checks([
'service' => 'consul',
'node_meta' => 'node-meta',
]);
$this->consul->health()->checks($checks);
// List Nodes for Service
$service = new Service([
'service' => 'consul',
'dc' => 'dc1',
]);
$this->consul->health()->service($service);
// List Nodes for Connect-capable Service
$connect = new Connect([
'service' => 'consul'
]);
$this->consul->health()->connect($connect);
// List Checks in State
$state = new State([
'state' => 'passing'
]);
$this->consul->health()->state($state);
// Getting the Consul License
$license = new License([
'dc' => 'dc1',
]);
$this->consul->operator()->getLicense($license);
// Updating the Consul License
$license = new License([
'dc' => 'dc1'
]);
$this->consul->operator()->updateLicense($license);
// Resetting the Consul License
$license = new License([
'dc' => 'dc1'
]);
$this->consul->operator()->resetLicense($license);