Download the PHP package sdpmlab/anser without Composer
On this page you can find all versions of the php package sdpmlab/anser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sdpmlab/anser
More information about sdpmlab/anser
Files in sdpmlab/anser
Package anser
Short Description PHP microservice orchestration pattern library.
License MIT
Informations about the package anser
Anser: PHP Microservices Orchestration Library
Anser is a PHP-based microservices orchestration library. You can use this library to manage connections and orchestrate your microservices. Through the Anser library, you can easily achieve the following goals:
- Abstract specific classes and implementations for each HTTP-based microservice, and Anser will not limit your communication mode.
- Quickly compose your microservices
- Write a microservices script with order
- Quickly adopt the SAGA Pattern to design your transaction logic
- Simple backup mechanism, transaction restore when service is interrupted
正體中文文件
Installation
Install Anser library via Composer:
Quick Start
Microservices Connection List
In your project, you must set the microservices connection list during the execution cycle. You can set it through the ServiceList::addLocalService()
method. You can refer to the example we provide to create your microservices connection list, which will be the basis for all microservices connections.
Abstract Microservice
In Anser, you can abstract all endpoints of a microservice through the SimpleService
class. We provide an example, you can refer to it to quickly create a microservice class:
You can directly refer to the Anser-Action
library to understand what mechanism Anser provides to handle microservices connections.
orchestrate Microservices
In Anser, you can orchestrate your microservices through the Orchestrator
class. We provide an example, you can refer to it to quickly create an orchestration class:
In the above example, we can see that in the definition
method, we use the setStep()
method to define the behavior of each step, and use the addAction()
method to define the logic required for each step.
In addAction()
, you can pass in two types to achieve different orchestration needs:
- Pass in the instance of
SDPMlab\Anser\Service\ActionInterface
. When the microservices orchestrator executes this step, it will directly use thisAction
instance to communicate with the microservices. - Pass in
callable
. When the microservices orchestrator executes this step, it will execute this Closure and pass in the Runtime Orchestrator. You can get the data of other steps through the Runtime Orchestrator instance to meet more logical requirements, and return the instance ofSDPMlab\Anser\Service\ActionInterface
at the end.
Use the transStart()
method to start a Saga transaction, and end the Saga transaction in the transEnd()
method. Then, you can use the setCompensationMethod()
method to define the compensation behavior of each step. When the step fails, the compensation behavior will be executed automatically.
Define Compensation
In the above example, we can see that in the definition
method, we use the setCompensationMethod()
method to define the compensation behavior of each step. When the step fails, the compensation behavior will be executed automatically.
You must implement the SDPMlab\Anser\Orchestration\Saga\SimpleSaga
class to define your compensation logic, and get the Runtime Orchestrator instance through the getOrchestrator()
method in the compensation logic. You can get the data of other steps through the Runtime Orchestrator instance to meet more logical requirements.
Execute the microservices orchestration logic
Depending on the framework you are using, you will need to execute your Orchestrator somewhere.
Here is a rough example:
We can see that in the createOrder()
method, we new CreateOrderOrchestrator();
an Orchestrator instance, and use the build()
method to start a service collaboration with Saga transaction, and pass in the product_key
, product_amout
, user_key
three parameters in the build()
method, these parameters will be used in the definition()
method.
Finally, you will get the return value after the build()
is completed. This return value comes from the data processed by defineResult()
.
The above is a full-featured example of the use of Anser Orchestrator Saga. You can use this example to understand how to use Anser Orchestrator.
License
Anser is released under the MIT License. See the bundled LICENSE,
All versions of anser with dependencies
sdpmlab/anser-action Version ^0.3.0
predis/predis Version ^2.0
pingyi/json-serializer Version ^0.1.2