Consul

EasySwoole提供了一个协程安全的consul协程版本客户端,方便用户做分布式的微服务开发。

安装

composer require easyswoole/consul

使用方式

  • 使用下面的接口方式,都需要先给Consul注入Config配置。
  • 接口只展示用法,具体的命名空间需要开发者自己引入
  1. use EasySwoole\Consul\Config;
  2. use EasySwoole\Consul\Consul;
  3. // config默认 127.0.0.1:8500/v1
  4. $config = new Config([
  5. 'IP' => '127.0.0.1',
  6. 'port' => '8500',
  7. 'version' => 'v1',
  8. ]);
  9. $consul = new Consul($config);
  10. // 两种写法,结果相同
  11. $config = new Config();
  12. $config->setIP('127.0.0.1');
  13. $config->setPort('8500');
  14. $config->setVersion('v1');
  15. $consul = new Consul($config);

ACLs

  1. // Bootstrap ACLs
  2. $bootstrap = new Bootstrap();
  3. $this->consul->acl()->bootstrap($bootstrap);
  4. // Check ACL Replication
  5. $replication = new Replication();
  6. $this->consul->acl()->replication($replication);
  7. // Translate Rules
  8. // Translate a Legacy Token's Rules
  9. $translate = new Translate([
  10. 'accessor_id' => $accessor_id
  11. ]);
  12. $this->consul->acl()->translate($translate);
  13. // Login to Auth Method
  14. $login = new Login([
  15. "authMethod" => $authMethod,
  16. "bearerToken" => $bearerToken
  17. ]);
  18. $this->consul->acl()->login($login);
  19. // Logout from Auth Method
  20. $logout = new Logout([
  21. 'token' => $header['token']
  22. ]);
  23. $this->consul->acl()->logout($logout);

Tokens

  1. // Create a Token
  2. $token = new Token([
  3. "description" => "Agent token for 'node1'",
  4. "Policies" => [
  5. ["ID" => "165d4317-e379-f732-ce70-86278c4558f7"],
  6. ["Name" => "node-read"],
  7. ],
  8. "Local" => false,
  9. ]);
  10. $this->consul->acl()->token($token);
  11. // Read a Token
  12. $token = new Token([
  13. "AccessorID" => "6a1253d2-1785-24fd-91c2-f8e78c745511"
  14. ]);
  15. $this->consul->acl()->readToken($token);
  16. // Read Self Token
  17. $self = new Token\GetSelf([
  18. 'token' => "6a1253d2-1785-24fd-91c2-f8e78c745511"
  19. ]);
  20. $this->consul->acl()->self($self);
  21. // Update a Token
  22. $update = new Token([
  23. 'accessorID' => '6a1253d2-1785-24fd-91c2-f8e78c745511',
  24. "Description" => "Agent token for 'node1'",
  25. "Policies" => [],
  26. "local" => false
  27. ]);
  28. $this->consul->acl()->updateToken($update);
  29. // Clone a Token
  30. $clone = new Token\CloneToken([
  31. 'accessorID' => '8f246b77-f3e1-ff88-5b48-8ec93abf3e05',
  32. "description" => "Clone of Agent token for 'node1'",
  33. ]);
  34. $this->consul->acl()->cloneToken($clone);
  35. // Delete a Token
  36. $delete = new Token([
  37. 'AccessorID' => '8f246b77-f3e1-ff88-5b48-8ec93abf3e05'
  38. ]);
  39. $this->consul->acl()->delete($delete);
  40. // List Tokens
  41. $token = new Tokens();
  42. $this->consul->acl()->tokens($token);

Legacy Tokens

  1. // Create ACL Token
  2. $create = new Create([
  3. "Name" => "my-app-token",
  4. "Type" => "client",
  5. "rules" => "a"
  6. ]);
  7. $this->consul->acl()->create($create);
  8. // Update ACL Token
  9. $update = new Update([
  10. "id" => "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
  11. "Name" => "my-app-token-updated",
  12. "Type" => "client",
  13. "Rules" => "# New Rules",
  14. ]);
  15. $this->consul->acl()->update($update);
  16. // Delete ACL Token
  17. $delete = new Destroy([
  18. 'uuid' => '8f246b77-f3e1-ff88-5b48-8ec93abf3e05'
  19. ]);
  20. $this->consul->acl()->destroy($delete);
  21. // Read ACL Token
  22. $info = new Info([
  23. 'uuid' => '8f246b77-f3e1-ff88-5b48-8ec93abf3e05'
  24. ]);
  25. $this->consul->acl()->info($info);
  26. // Clone ACL Token
  27. $cloneAclToken = new CloneACLToken([
  28. 'uuid' => '8f246b77-f3e1-ff88-5b48-8ec93abf3e05'
  29. ]);
  30. $this->consul->acl()->cloneAclToken($cloneAclToken);
  31. // List ACLs
  32. $getList = new Lists();
  33. $this->consul->acl()->getList($getList);
  34. $this->assertEquals('x','x');

Policies

  1. // Create a Policy
  2. $policy = new Policy([
  3. "Name" => "node-read",
  4. "Description" => "Grants read access to all node information",
  5. "Rules" => "node_prefix \"\" { policy = \"read\"}",
  6. "datacenters" => ["dc1"]
  7. ]);
  8. $this->consul->acl()->policy($policy);
  9. // Read a Policy
  10. $policy = new Policy([
  11. 'id' => 'c01a1f82-44be-41b0-a686-685fb6e0f485',
  12. ]);
  13. $this->consul->acl()->readPolicy($policy);
  14. // Update a Policy
  15. $policy = new Policy([
  16. "ID" => "c01a1f82-44be-41b0-a686-685fb6e0f485",
  17. "Name" => "register-app-service",
  18. "Description" => "Grants write permissions necessary to register the 'app' service",
  19. "Rules" => "service \"app\" { policy = \"write\"}",
  20. ]);
  21. $this->consul->acl()->updatePolicy($policy);
  22. // Delete a Policy
  23. $policy = new Policy([
  24. 'id' => 'c01a1f82-44be-41b0-a686-685fb6e0f485'
  25. ]);
  26. $this->consul->acl()->deletePolicy($policy);
  27. // List Policies
  28. $policies = new Policies();
  29. $this->consul->acl()->policies($policies);

Roles

  1. // Create a Role
  2. $role = new Role([
  3. "name" => "example-role",
  4. "description" => "Showcases all input parameters",
  5. ]);
  6. $this->consul->acl()->role($role);
  7. // Read a Role
  8. $role = new Role([
  9. 'id' => 'aa770e5b-8b0b-7fcf-e5a1-8535fcc388b4'
  10. ]);
  11. $this->consul->acl()->readRole($role);
  12. // Read a Role by Name
  13. $name = new Role([
  14. 'name' => 'example-role'
  15. ]);
  16. $this->consul->acl()->readRoleByName($name);
  17. // Update a Role
  18. $role = new Role([
  19. 'id' => 'aa770e5b-8b0b-7fcf-e5a1-8535fcc388b4',
  20. "name" => "example-two",
  21. ]);
  22. $this->consul->acl()->updateRole($role);
  23. // Delete a Role
  24. $role = new Role([
  25. 'id' => 'aa770e5b-8b0b-7fcf-e5a1-8535fcc388b4'
  26. ]);
  27. $this->consul->acl()->deleteRole($role);
  28. // List Roles
  29. $roles = new Roles();
  30. $this->consul->acl()->roles($roles);

Auth Method

  1. // Create an Auth Method
  2. $method = new AuthMethod([
  3. "Name" => "minikube",
  4. "Type" => "kubernetes",
  5. "Description" => "dev minikube cluster",
  6. "Config" => [
  7. "Host" => "https://192.0.2.42:8443",
  8. "CACert" => "-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----\n",
  9. "ServiceAccountJWT" => "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9..."
  10. ]
  11. ]);
  12. $this->consul->acl()->authMethod($method);
  13. // Read an Auth Method
  14. $method = new AuthMethod([
  15. 'name' => 'minikube',
  16. ]);
  17. $this->consul->acl()->readAuthMethod($method);
  18. // Update an Auth Method
  19. $method = new AuthMethod([
  20. "Name" => "minikube",
  21. "Type" => "kubernetes",
  22. "Description" => "dev minikube cluster",
  23. "Config" => [
  24. "Host" => "https://192.0.2.42:8443",
  25. "CACert" => "-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----\n",
  26. "ServiceAccountJWT" => "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9..."
  27. ]
  28. ]);
  29. $this->consul->acl()->updateAuthMethod($method);
  30. // Delete an Auth Method
  31. $method = new AuthMethod([
  32. "Name" => "minikube",
  33. ]);
  34. $this->consul->acl()->deleteAuthMethod($method);
  35. // List Auth Methods
  36. $method = new AuthMethods();
  37. $this->consul->acl()->authMethods($method);

Binding Rules

  1. // Create a Binding Rule
  2. $bindingRule = new BindingRule([
  3. "description" => "example rule",
  4. "authMethod" => "minikube",
  5. "Selector" => "serviceaccount.namespace==default",
  6. "BindType" => "service",
  7. "BindName" => "{{ serviceaccount.name }}"
  8. ]);
  9. $this->consul->acl()->bindingRule($bindingRule);
  10. // Read a Binding Rule
  11. $bindingRule = new BindingRule([
  12. 'id' => '000ed53c-e2d3-e7e6-31a5-c19bc3518a3d',
  13. ]);
  14. $this->consul->acl()->readBindingRule($bindingRule);
  15. // Update a Binding Rule
  16. $bindingRule = new BindingRule([
  17. 'id' => '000ed53c-e2d3-e7e6-31a5-c19bc3518a3d',
  18. "Description" => "updated rule",
  19. "authMethod" => "minikube",
  20. "Selector" => "serviceaccount.namespace=dev",
  21. "BindType" => "role",
  22. "BindName" => "{{ serviceaccount.name }}",
  23. ]);
  24. $this->consul->acl()->updateBindingRule($bindingRule);
  25. // Delete a Binding Rule
  26. $bindingRule = new BindingRule([
  27. 'id' => '000ed53c-e2d3-e7e6-31a5-c19bc3518a3d',
  28. ]);
  29. $this->consul->acl()->deleteBindingRule($bindingRule);
  30. // List Binding Rules
  31. $bindingRules = new BindingRules();
  32. $this->consul->acl()->bindingRules($bindingRules);

Agent

  1. // List Members
  2. $this->consul->agent()->members(new Members([
  3. 'wan' => 'a',
  4. 'segment' => 'b',
  5. ]));
  6. // Read Configuration
  7. $self = new SelfParams();
  8. $this->consul->agent()->self($self);
  9. // Reload Agent
  10. $reload = new Reload();
  11. $this->consul->agent()->reload($reload);
  12. // Enable Maintenance Mode
  13. $maintenance = new Maintenance([
  14. 'enable' => true,
  15. 'reason' => 'whatever',
  16. ]);
  17. $this->consul->agent()->maintenance($maintenance);
  18. // View Metrics
  19. $metrics = new Metrics([
  20. 'format' => 'prometheus',
  21. ]);
  22. $this->consul->agent()->metrics($metrics);
  23. // Stream Logs
  24. $monitor = new Monitor([
  25. 'loglevel' => 'info',
  26. ]);
  27. $this->consul->agent()->monitor($monitor);
  28. // Join Agent
  29. $join = new Join([
  30. 'address' => '1.2.3.4',
  31. 'wan' => false
  32. ]);
  33. $this->consul->agent()->join($join);
  34. // Graceful Leave and Shutdown
  35. $leave = new Leave();
  36. $this->consul->agent()->leave($leave);
  37. // Force Leave and Shutdown
  38. $forceLeave = new ForceLeave([
  39. 'node' => 'consul'
  40. ]);
  41. $this->consul->agent()->forceLeave($forceLeave);
  42. // Update ACL Tokens
  43. $token = new Token([
  44. 'action' => 'acl_agent_token',
  45. 'token' => 'token'
  46. ]);
  47. $this->consul->agent()->token($token);

Checks

  1. // List Checks
  2. $checks = new Checks([
  3. 'filter' => '',
  4. ]);
  5. $this->consul->agent()->checks($checks);
  6. // Register Check
  7. $register = new Register([
  8. 'name' => 'Memory_utilization', // 不可以出现空格或者其他url中不允许出现的特殊字符,否则取消注册的check_id会报400 error
  9. "notes" => "Ensure we don't oversubscribe memory",
  10. "DeregisterCriticalServiceAfter" => "90m",
  11. "Args" => ["/usr/local/bin/check_mem.py"],
  12. "DockerContainerID" => "f972c95ebf0e",
  13. "Shell" => "/bin/bash",
  14. "HTTP" => "https://example.com",
  15. "Method" => "POST",
  16. "Header" => ["x-foo" => ["bar", "baz"]],
  17. "TCP" => "example.com:22",
  18. "Interval" => "10s",
  19. "TTL" => "15s",
  20. "TLSSkipVerify" => true,
  21. ]);
  22. $this->consul->agent()->register($register);
  23. // Deregister Check
  24. $deRegister = new DeRegister([
  25. 'check_id' => 'Memory_utilization'
  26. ]);
  27. $this->consul->agent()->deRegister($deRegister);
  28. // TTL Check Pass
  29. $pass = new Pass([
  30. 'check_id' => 'Memory_utilization',
  31. 'note' => 'consul',
  32. ]);
  33. $this->consul->agent()->pass($pass);
  34. // TTL Check Warn
  35. $warn = new Warn([
  36. 'check_id' => 'Memory_utilization',
  37. 'note' => 'consul',
  38. ]);
  39. $this->consul->agent()->warn($warn);
  40. // TTL Check Fail
  41. $fail = new Fail([
  42. 'check_id' => 'Memory_utilization',
  43. 'note' => 'consul',
  44. ]);
  45. $this->consul->agent()->fail($fail);
  46. // TTL Check Update
  47. $update = new Update([
  48. 'check_id' => 'Memory_utilization',
  49. 'Status' => 'passing',
  50. 'Output' => 'update success'
  51. ]);
  52. $this->consul->agent()->update($update);

Services

  1. // List Services
  2. $services = new Services([
  3. 'filter' => '',
  4. ]);
  5. $this->consul->agent()->services($services);
  6. // Get Service Configuration
  7. $service = new Service([
  8. 'service_id' => "consul"
  9. ]);
  10. $this->consul->agent()->service($service);
  11. // Get local service health
  12. $name = new Name([
  13. 'service_name' => 'consul',
  14. 'format' => 'text',
  15. ]);
  16. $this->consul->agent()->name($name);
  17. // Get local service health by its ID
  18. $id = new ID([
  19. 'service_id' => 'consul',
  20. 'format' => 'text',
  21. ]);
  22. $this->consul->agent()->id($id);
  23. // Register Service
  24. $register = new Service\Register([
  25. "ID" => "redis1",
  26. "name" => "redis",
  27. "Tags" => [
  28. "primary",
  29. "v1"
  30. ],
  31. "Address" => "127.0.0.1",
  32. "Port" => 8000,
  33. "meta" => [
  34. "redis_version" => "4.0",
  35. ],
  36. "EnableTagOverride" => false,
  37. "Check" => [
  38. "DeregisterCriticalServiceAfter" => "90m",
  39. "Args" => ["/usr/local/bin/check_redis.py"],
  40. "HTTP" => "http://localhost:5000/health",
  41. "Interval" => "10s",
  42. "TTL" => "15s"
  43. ],
  44. "weights" => [
  45. "Passing" => 10,
  46. "Warning" => 1
  47. ]
  48. ]);
  49. $this->consul->agent()->serviceRegister($register);
  50. // Deregister Service
  51. $deregister = new Service\DeRegister([
  52. 'service_id' => 'consul',
  53. ]);
  54. $this->consul->agent()->serviceDeregister($deregister);
  55. // Enable Maintenance Mode
  56. $maintenance= new Service\Maintenance([
  57. 'service_id' => 'consul',
  58. 'enable' => true,
  59. 'reason' => ''
  60. ]);
  61. $this->consul->agent()->serviceMaintenance($maintenance);

Connect

  1. // Authorize
  2. $authorize = new Authorize([
  3. "target" => "db",
  4. "clientCertURI" => "spiffe://dc1-7e567ac2-551d-463f-8497-f78972856fc1.consul/ns/default/dc/dc1/svc/web",
  5. "clientCertSerial" => "04:00:00:00:00:01:15:4b:5a:c3:94"
  6. ]);
  7. $this->consul->agent()->authorize($authorize);
  8. // Certificate Authority (CA) Roots
  9. $roots = new Roots();
  10. $this->consul->agent()->roots($roots);
  11. // Service Leaf Certificate
  12. $leaf = new Leaf([
  13. 'service' => 'consul'
  14. ]);
  15. $this->consul->agent()->leaf($leaf);

Catalog

  1. // Register Entity
  2. $register = new Register([
  3. "datacenter" => "dc1",
  4. "id" => "40e4a748-2192-161a-0510-9bf59fe950b5",
  5. "node" => "foobar",
  6. "Address" => "192.168.10.10",
  7. "TaggedAddresses" => [
  8. "lan" => "192.168.10.10",
  9. "wan" => "10.0.10.10"
  10. ],
  11. "NodeMeta" => [
  12. "somekey" => "somevalue"
  13. ],
  14. "Service" => [
  15. "ID" => "redis1",
  16. "Service" => "redis",
  17. "Tags" => [
  18. "primary",
  19. "v1"
  20. ],
  21. "Address" => "127.0.0.1",
  22. "TaggedAddresses" => [
  23. "lan" => [
  24. "address" => "127.0.0.1",
  25. "port" => 8000,
  26. ],
  27. "wan" => [
  28. "address" => "198.18.0.1",
  29. "port" => 80
  30. ]
  31. ],
  32. "Meta" => [
  33. "redis_version" => "4.0"
  34. ],
  35. "Port" => 8000
  36. ],
  37. "Check" => [
  38. "Node" => "foobar",
  39. "CheckID" => "service:redis1",
  40. "Name" => "Redis health check",
  41. "Notes" => "Script based health check",
  42. "Status" => "passing",
  43. "ServiceID" => "redis1",
  44. "Definition" => [
  45. "TCP" => "localhost:8888",
  46. "Interval" => "5s",
  47. "Timeout" => "1s",
  48. "DeregisterCriticalServiceAfter" => "30s"
  49. ]
  50. ],
  51. "SkipNodeUpdate" => false
  52. ]);
  53. $this->consul->catalog()->register($register);
  54. // Deregister Entity
  55. $deregister = new Deregister([
  56. "datacenter" => "dc1",
  57. "node" => "foobar",
  58. "CheckID" => "service:redis1",
  59. ]);
  60. $this->consul->catalog()->deRegister($deregister);
  61. // List Datacenters
  62. $datacenters = new Datacenters();
  63. $this->consul->catalog()->dataCenters($datacenters);
  64. // List Nodes
  65. $nodes = new Nodes([
  66. 'dc' => 'dc1',
  67. 'node-meta' => '',
  68. 'near' => '',
  69. 'filter' => '',
  70. ]);
  71. $this->consul->catalog()->nodes($nodes);
  72. // List Services
  73. $nodes = new Nodes([
  74. $services = new Services([
  75. 'dc' => 'dc1',
  76. 'node-meta' => '',
  77. ]);
  78. $this->consul->catalog()->services($services);
  79. ]);
  80. $this->consul->catalog()->nodes($nodes);
  81. // List Nodes for Service
  82. $services = new Services([
  83. 'dc' => 'a',
  84. 'node-meta' => 'b',
  85. ]);
  86. $this->consul->catalog()->services($services);
  87. // List Nodes for Connect-capable Service
  88. $service = new Service([
  89. 'service' => 'consul',
  90. 'dc' => 'dc1',
  91. 'tag' => '',
  92. 'near' => '',
  93. 'node-meta' => '',
  94. 'filter' => '',
  95. ]);
  96. $this->consul->catalog()->service($service);
  97. // List Services for Node
  98. $connect = new Connect([
  99. 'service' => 'consul',
  100. 'dc' => 'dc1',
  101. 'tag' => '',
  102. 'near' => '',
  103. 'node-meta' => '',
  104. 'filter' => '',
  105. ]);
  106. $this->consul->catalog()->connect($connect);
  107. // List Services for Node
  108. $node = new Node([
  109. 'node' => '2eb87046a6fe',
  110. 'dc' => 'dc1',
  111. 'filter' => '',
  112. ]);
  113. $this->consul->catalog()->node($node);

Config

  1. // Apply Configuration
  2. $config = new \EasySwoole\Consul\Request\Config([
  3. 'Kind' => 'service-defaults',
  4. 'Name' => 'web',
  5. 'Protocol' => 'Http'
  6. ]);
  7. $this->consul->config()->config($config);
  8. // Get Configuration
  9. $config = new \EasySwoole\Consul\Request\Config([
  10. 'Kind' => 'service-defaults',
  11. 'name' => 'web',
  12. ]);
  13. $this->consul->config()->getConfig($config);
  14. // List Configurations
  15. $config = new \EasySwoole\Consul\Request\Config([
  16. 'Kind' => 'service-defaults'
  17. ]);
  18. $this->consul->config()->listConfig($config);
  19. // Delete Configuration
  20. $config = new \EasySwoole\Consul\Request\Config([
  21. 'Kind' => 'service-defaults',
  22. 'name' => 'web',
  23. ]);
  24. $this->consul->config()->deleteConfig($config);

Connect

Certificate Authority (CA)

  1. // List CA Root Certificates
  2. $roots = new Roots();
  3. $this->consul->connect()->roots($roots);
  4. // Get CA Configuration
  5. $configuration = new Configuration();
  6. $this->consul->connect()->configuration($configuration);
  7. // Update CA Configuration
  8. $configuration = new Configuration([
  9. 'Provider' => 'consul',
  10. 'Config' => [
  11. 'LeafCertTTL' => '72h'
  12. ]
  13. ]);
  14. $this->consul->connect()->updateConfiguration($configuration);

Intentions

  1. // Create Intention
  2. $intentions = new Intentions([
  3. 'SourceName' => 'web',
  4. 'DestinationName' => 'db',
  5. 'SourceType' => 'consul',
  6. 'action' => 'allow'
  7. ]);
  8. $this->consul->connect()->intentions($intentions);
  9. // Read Specific Intention
  10. $intentions = new Intentions([
  11. 'uuid' => 'e9ebc19f-d481-42b1-4871-4d298d3acd5c',
  12. ]);
  13. $this->consul->connect()->readIntention($intentions);
  14. // List Intentions
  15. $intentions = new Intentions();
  16. $this->consul->connect()->listIntention($intentions);
  17. // Update Intention
  18. $intentions = new Intentions([
  19. 'uuid' => 'b40faaf3-34aa-349f-3cf2-f5d720240662',
  20. 'description' => 'just a test description',
  21. 'SourceName' => '',
  22. 'DestinationName' => '',
  23. 'Action' => 'allow'
  24. ]);
  25. $this->consul->connect()->updateIntention($intentions);
  26. // Delete Intention
  27. $intentions = new Intentions([
  28. 'uuid' => 'b40faaf3-34aa-349f-3cf2-f5d720240662',
  29. ]);
  30. $this->consul->connect()->deleteIntention($intentions);
  31. // Check Intention Result
  32. $intentions = new Intentions\Check([
  33. 'source' => 'web',
  34. 'destination' => 'db',
  35. ]);
  36. $this->consul->connect()->check($intentions);
  37. // List Matching Intentions
  38. $intentions = new Intentions\Match([
  39. 'by' => 'source',
  40. 'name' => 'web',
  41. ]);
  42. $this->consul->connect()->match($intentions);

Coordinate

  1. // Read WAN Coordinates
  2. $datacenters = new Datacenters();
  3. $this->consul->coordinates()->datacenters($datacenters);
  4. // Read LAN Coordinates for all nodes
  5. $nodes = new Nodes([]);
  6. $this->consul->coordinates()->nodes($nodes);
  7. // Read LAN Coordinates for a node
  8. $node = new Node([
  9. 'node' => '2456c2850382',
  10. ]);
  11. $this->consul->coordinates()->node($node);
  12. // Update LAN Coordinates for a node
  13. $update = new Update([
  14. 'dc' => 'dc1',
  15. 'node' => '2456c2850382',
  16. 'Segment' => 'update',
  17. "Coord" => [
  18. "Adjustment" => 0,
  19. "Error" => 1.5,
  20. "Height" => 0,
  21. "Vec" => [0, 0, 0, 0, 0, 0, 0, 0]
  22. ]
  23. ]);
  24. $this->consul->coordinates()->update($update);

Events

  1. // Fire Event
  2. $fire = new Fire([
  3. 'name' => 'consul',
  4. 'dc' => 'dc1',
  5. ]);
  6. $this->consul->event()->fire($fire);
  7. // List Events
  8. $listEvent = new ListEvent([
  9. 'name' => 'consul',
  10. ]);
  11. $this->consul->event()->listEvent($listEvent);

Health

  1. // List Checks for Node
  2. $node = new Node([
  3. 'node' => '2456c2850382',
  4. 'dc' => 'dc1',
  5. ]);
  6. $this->consul->health()->node($node);
  7. // List Checks for Service
  8. $checks = new Checks([
  9. 'service' => 'consul',
  10. 'node_meta' => 'node-meta',
  11. ]);
  12. $this->consul->health()->checks($checks);
  13. // List Nodes for Service
  14. $service = new Service([
  15. 'service' => 'consul',
  16. 'dc' => 'dc1',
  17. ]);
  18. $this->consul->health()->service($service);
  19. // List Nodes for Connect-capable Service
  20. $connect = new Connect([
  21. 'service' => 'consul'
  22. ]);
  23. $this->consul->health()->connect($connect);
  24. // List Checks in State
  25. $state = new State([
  26. 'state' => 'passing'
  27. ]);
  28. $this->consul->health()->state($state);

KV Store

  1. // Read Key
  2. $kv = new Kv([
  3. 'key' => 'my-key',
  4. 'dc' => 'dc1',
  5. ]);
  6. $this->consul->kvStore()->kv($kv);
  7. // Create Key
  8. $create = new kv([
  9. 'key' => 'my-key',
  10. 'dc' => 'dc1',
  11. ]);
  12. $this->consul->kvStore()->create($create);
  13. // Update Key
  14. $update = new kv([
  15. 'key' => 'my-key',
  16. 'dc' => 'dc1',
  17. ]);
  18. $this->consul->kvStore()->update($update);
  19. // Delete Key
  20. $delete = new Kv([
  21. 'key' => 'my-key',
  22. 'recurse' => false,
  23. ]);
  24. $this->consul->kvStore()->delete($delete);

Operator

Area

  1. // Create Network Area
  2. $area = new Area([
  3. 'PeerDatacenter' => 'dc1',
  4. "RetryJoin" => [ "10.1.2.3", "10.1.2.4", "10.1.2.5" ],
  5. "UseTLS" => false
  6. ]);
  7. $this->consul->operator()->area($area);
  8. // List Network Areas
  9. $area = new Area([
  10. 'dc' => 'dc1',
  11. 'uuid' => '10275a2e-aa8f-2cf3-0adf-ff03d8950902',
  12. ]);
  13. $this->consul->operator()->areaList($area);
  14. // List Specific Network Area
  15. $area = new Area([
  16. 'dc' => 'dc1',
  17. 'uuid' => '10275a2e-aa8f-2cf3-0adf-ff03d8950902',
  18. ]);
  19. $this->consul->operator()->areaList($area);
  20. // Update Network Area
  21. $area = new Area([
  22. 'uuid' => '10275a2e-aa8f-2cf3-0adf-ff03d8950902',
  23. 'UseTLS' => true,
  24. 'dc' => 'dc1',
  25. ]);
  26. $this->consul->operator()->updateArea($area);
  27. // Delete Network Area
  28. $area = new Area([
  29. 'uuid' => '10275a2e-aa8f-2cf3-0adf-ff03d8950902',
  30. ]);
  31. $this->consul->operator()->deleteArea($area);
  32. // Join Network Area
  33. $area = new Area([
  34. 'uuid' => '10275a2e-aa8f-2cf3-0adf-ff03d8950902',
  35. ]);
  36. $this->consul->operator()->joinArea($area);
  37. // List Network Area Members
  38. $area = new Area([
  39. 'uuid' => '10275a2e-aa8f-2cf3-0adf-ff03d8950902'
  40. ]);
  41. $this->consul->operator()->membersArea($area);

Autopilot

  1. // Read Configuration
  2. $configuration = new Configuration([
  3. 'dc' => 'dc1',
  4. 'stale' => true,
  5. ]);
  6. $this->consul->operator()->getConfiguration($configuration);
  7. // Update Configuration
  8. $configuration = new Configuration([
  9. 'dc' => 'dc1',
  10. 'stale' => true,
  11. "cleanupDeadServers" => true,
  12. "lastContactThreshold" => "200ms",
  13. "MaxTrailingLogs" => 250,
  14. "ServerStabilizationTime" => "10s",
  15. "RedundancyZoneTag" => "",
  16. "DisableUpgradeMigration" => false,
  17. "UpgradeVersionTag" => "",
  18. ]);
  19. $this->consul->operator()->updateConfiguration($configuration);
  20. // Read Health
  21. $health = new Health([
  22. 'dc' => 'dc1',
  23. ]);
  24. $this->consul->operator()->health($health);

Keyring

  1. // List Gossip Encryption Keys
  2. $keyring = new Keyring();
  3. $keyring->setRelayFactor(0);
  4. $keyring->setLocalOnly(false);
  5. $this->consul->operator()->getKeyring($keyring);
  6. // Add New Gossip Encryption Key
  7. $keyring = new Keyring([
  8. "Key" => "3lg9DxVfKNzI8O+IQ5Ek+Q==",
  9. 'relayFactor' => 1,
  10. ]);
  11. $this->consul->operator()->addKeyring($keyring);
  12. // Change Primary Gossip Encryption Key
  13. $keyring = new Keyring([
  14. "Key" => "3lg9DxVfKNzI8O+IQ5Ek+Q==",
  15. ]);
  16. $this->consul->operator()->changeKeyring($keyring);
  17. // Delete Gossip Encryption Key
  18. $keyring = new Keyring([
  19. "Key" => "3lg9DxVfKNzI8O+IQ5Ek+Q==",
  20. "relayFactor" => 1
  21. ]);
  22. $this->consul->operator()->deleteKeyring($keyring);

License

  1. // Getting the Consul License
  2. $license = new License([
  3. 'dc' => 'dc1',
  4. ]);
  5. $this->consul->operator()->getLicense($license);
  6. // Updating the Consul License
  7. $license = new License([
  8. 'dc' => 'dc1'
  9. ]);
  10. $this->consul->operator()->updateLicense($license);
  11. // Resetting the Consul License
  12. $license = new License([
  13. 'dc' => 'dc1'
  14. ]);
  15. $this->consul->operator()->resetLicense($license);

Raft

  1. // Read Configuration
  2. $raft = new \EasySwoole\Consul\Request\Operator\Raft\Configuration();
  3. $this->consul->operator()->getRaftConfiguration($raft);
  4. // Delete Raft Peer
  5. $peer = new Peer([
  6. 'address' => '172.17.0.18:8301',
  7. 'dc' => 'dc1',
  8. ]);
  9. $this->consul->operator()->peer($peer);

Segment

  1. // List Network Segments
  2. $segment = new Segment();
  3. $this->consul->operator()->segment($segment);

Prepared Query

  1. // Create Prepared Query
  2. $query = new Query([
  3. "name" => "my-query",
  4. "Session" => "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
  5. "Token" => "11",
  6. "Service" => [
  7. "Service" => "redis",
  8. "Failover" => [
  9. "NearestN" => 3,
  10. "Datacenters" => ["dc1", "dc2"]
  11. ],
  12. "Near" => "node1",
  13. "OnlyPassing" => false,
  14. "Tags" => ["primary", "!experimental"],
  15. "NodeMeta" => ["instance_type" => "m3.large"],
  16. "ServiceMeta" => ["environment" => "production"]
  17. ],
  18. "DNS" => [
  19. "TTL" => "10s"
  20. ],
  21. ]);
  22. $this->consul->query()->query($query);
  23. // Read Prepared Query
  24. $query = new Query([
  25. 'dc' => 'dc1'
  26. ]);
  27. $this->consul->query()->readQuery($query);
  28. // Update Prepared Query
  29. $query = new Query([
  30. 'uuid' => '90dce5ca-5697-ae2f-09ae-51e9542ea58c',
  31. 'dc' => 'dc1',
  32. ]);
  33. $this->consul->query()->updateQuery($query);;
  34. // Read Prepared Query
  35. $query = new Query([
  36. 'dc' => 'dc1'
  37. ]);
  38. $this->consul->query()->readQuery($query);
  39. // Delete Prepared Query
  40. $query = new Query([
  41. 'uuid' => '90dce5ca-5697-ae2f-09ae-51e9542ea58c'
  42. ]);
  43. $this->consul->query()->deleteQuery($query);;
  44. // Execute Prepared Query
  45. $execute = new Query\Execute([
  46. 'uuid' => '90dce5ca-5697-ae2f-09ae-51e9542ea58c',
  47. 'dc' => 'dc1',
  48. ]);
  49. $this->consul->query()->execute($execute);
  50. // Explain Prepared Query
  51. $execute = new Query\Explain([
  52. 'uuid' => '90dce5ca-5697-ae2f-09ae-51e9542ea58c',
  53. 'dc' => 'dc1',
  54. ]);
  55. $this->consul->query()->explain($execute);

Sessions

  1. // Create Session
  2. $create = new Create([
  3. 'dc' => 'dc1',
  4. "LockDelay" => "15s",
  5. "Name" => "my-service-lock",
  6. "Node" => "foobar",
  7. "Checks" => ["a", "b", "c"],
  8. "Behavior" => "release",
  9. "TTL" => "30s",
  10. ]);
  11. $this->consul->session()->create($create);
  12. // Delete Session
  13. $destroy = new Destroy([
  14. 'uuid' => 'f32a15b3-1baa-c047-bde9-bec3015ea013',
  15. 'dc' => 'dc1',
  16. ]);
  17. $this->consul->session()->destroy($destroy);
  18. // Read Session
  19. $info = new Info([
  20. 'uuid' => 'f32a15b3-1baa-c047-bde9-bec3015ea013',
  21. 'dc' => 'dc1',
  22. ]);
  23. $this->consul->session()->info($info);
  24. // List Sessions for Node
  25. $node = new Node([
  26. 'node' => '2456c2850382',
  27. 'dc' => 'dc1',
  28. ]);
  29. $this->consul->session()->node($node);
  30. // List Sessions
  31. $sessionList = new SessionList([
  32. 'dc' => 'dc1'
  33. ]);
  34. $this->consul->session()->sessionList($sessionList);
  35. // Renew Session
  36. $renew = new Renew([
  37. 'uuid' => '4f6d1cf6-b60a-c929-eeb8-12f4d7eaff62',
  38. 'dc' => 'dc1'
  39. ]);
  40. $this->consul->session()->renew($renew);

Snapshots

  1. // Generate Snapshot
  2. $generate = new Snapshot([
  3. 'dc' => 'dc1',
  4. 'stale' => 'true',
  5. ]);
  6. $this->consul->snapshot()->generate($generate);
  7. // Restore Snapshot
  8. $restore = new Snapshot();
  9. $this->consul->snapshot()->restore($restore);

Status

  1. // Get Raft Leader
  2. $leader = new Leader();
  3. $this->consul->status()->leader($leader);
  4. // List Raft Peers
  5. $peers = new Peers([
  6. 'dc' => 'dc1',
  7. ]);
  8. $this->consul->status()->peers($peers);

Transactions

  1. // Create Transaction
  2. $transaction = new Txn([]);
  3. $this->consul->transaction()->create($transaction);
  1. $node = new Node([
  2. 'node' => '44e4656a94cd',
  3. 'dc' => 'dc1',
  4. 'filter' => '',
  5. ]);
  6. $consul->catalog()->node($node);