DDD Domain-Driven Design (DDD: Domain-Driven Design)
Architecture Background:
The existing architecture design is really unbearable, and the repeated changes of the business lead to the terrifying complexity of the code circle. My previous experience with microservices architecture made me go a bit more thorough and use DDD Domain Driven Design to make the whole change.
After several months of hard work, I have gradually realized the advantages of ddd's architecture design. The aggregate root design can assist us in the transformation of the entire service, and the development is getting faster and faster.
CQRS access method:
In the architecture system of CQRS , two components will be separated first. One is the command -related microservice, which is responsible for add , update , and other state-changing actions. The query microservice , as the name suggests, is the read action .
- Microservices use spring-boot framework
- Build and run docker containers
- CQRS's axon, eventbus, storage using elasticsearch
- gateway adopts the method of independent research and development, and supports registration, discovery, and routing methods
Case:
gateway has been written in the previous blog, not described here
First simple rest controller
@RestController public class NotifyController { @Autowired private CommandGateway commandGateway; @RequestMapping(value = "/notify", method = RequestMethod.GET) public void notify(HttpServletRequest request) { commandGateway.send(asCommandMessage(new CreateTaskCommand("1","chenyang","China")), new CommandCallback() { @Override public void onSuccess(Object result) { // Continue things successfully } @Override public void onFailure(Throwable cause) { //fail } }); } }
Command:
public class SuccessNotification extends AbstractAnnotatedAggregateRoot<String> { /** * The constant serialVersionUID */ private static final long serialVersionUID = -159779842362041665L; @AggregateIdentifier private String id; @CommandHandler public SuccessNotification(SuccessNotifyCommand command) { apply(new SuccessNotifyEvent(command.getId())); } SuccessNotification() { } @CommandHandler void on(SuccessNotifyCommand command) { apply(new SuccessNotifyEvent(command.getId())); } @EventSourcingHandler public void on(SuccessNotifyEvent event) { this.id = event.getId(); } }
Event:
@EventHandler String on(SuccessNotifyEvent event) { return "world"; }
final conclusion:
Compared with the chimney-style approach, ddd has higher requirements for developers and architects. As one of the role domain experts, it is necessary to have a thorough understanding of the aggregate root in order to better understand the business.