Example traceable events

Scene:  (the User Service)

Update a user operation includes the user to update the basic attributes and assigning roles are performed by two threads, one thread when another fails successfully performed, the entire process flow of a rollback

 

Solution: Use CQRS event traceable to a specific state rollback aggregate root

Traceability event (Event Source):

Event to ensure traceability of all changes to application state is stored as a series of events, we are not only able to query these events, we can use these event records in the past to restore the status of applications and automatically adjusts the state to respond as retroactive basis change.

logical analysis:        

Update the user whether to update the basic properties of the user or assigning roles, we expect that once this aggregate root node instance state changes from the beginning,

For example the first instance to create a UserAggregate {name = zhngsan, phone = 123, assignedRoleList = [r01, r02, r03]}, later updates will assigedRoleList polymerization root instance attribute updates from [r01, r02, r03] to [R01 , R04] UserAggregate this time becomes {name = zhngsan, phone = 123, assignedRoleList = [r01,04]}, and also if this update polymerization root instance, we expect from a assigedRoleList property [r01, r04] the node to begin the update.        

       How to get the aggregate root instances in the past of a state change?        

       We try to get a UserAggregate method by calling AXON load (), the test found that aggregate root instance state whether any overloaded method name using this method are obtained for the initialization state, that is, the state of the aggregate root is first created, which is not consistent with our expectations, we think of the event since traceability 'state can use these application event log reduction past'

May wish to start from the Event Source, a review of its API found AXON provides EventStore component that provides a mechanism to open the event flow from the underlying event memory, now we have a new way to get DomainEventStream by EventStore component is represented by the history of polymerization released event stream and then filtering logic determines from the event stream of an event are most concerned about the current service, in conjunction with the final polymerization root instance initialization state and events in the past to obtain this reduction of the polymerization events root instance.        

       Such as the example given above (not consider all events during program execution in the generated real) polymerization of UserAggregate root change event generated a total of three,

      1.UserCreatedEvent {name=zhngsan,phone=123}

      2.RoleAssignedEvent {assignedRoleList=[r01,r02,r03]}

      3.RoleAssignedEvent {assignedRoleList = [r01, r04]}, we expect this time from the last assigedRoleList property [r01, r04] The start node update, simply,

      a. call AXON load () method to get an initialization state UserAggregate example {name = zhngsan, phone = 123, assignedRoleList = [r01, r02, r03]},

      b. call AXON EventStore readEvent () method to get the event stream, and filtered to obtain the latest time by assigning roles events RoleAssignedEvent version number and event type (RoleAssignedEvent.class) {assignedRoleList = [r01, r04]},

      c. Update ab assignedRoleList common attributes in two steps to obtain an updated distribution on the role UserAggregate polymerization root instance {name = zhngsan, phone = 123} 2.RoleAssignedEvent {assignedRoleList = [r01, r04]}.

      Back to our topic of the beginning, how to roll back the whole process?

    Through the above understanding of how to get past a certain aggregate root instance state changes, rollback difficult to find the whole process is just a physical work, the most critical issues have been resolved, it is not described in detail.

Guess you like

Origin www.cnblogs.com/lshan/p/11425723.html