In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to share DTO across micro-services". In daily operation, I believe many people have doubts about how to share DTO across micro-services. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to share DTO across micro-services". Next, please follow the editor to study!
1. Overview
In recent years, microservices have become very popular. One of the basic features of microservices is that they are modular, independent, and scalable. Microservices need to work together and exchange data. To achieve this, we create a shared data transfer object called dto.
two。 Expose domain objects as DTO
The model that represents the application domain is managed using microservices. Domain models are different concerns, and we separate them from the data model in the DAO layer.
The main reason for this is that we do not want to expose the complexity of the domain to the client through services. Instead, we expose the dto through REST api between services serving the application client. When dto is passed between these services, we convert them to domain objects.
The above service-oriented architecture schematically shows the components and processes from DTO to domain objects.
3. DTO sharing between microservices
Take the process of ordering products by customers as an example. This process is based on the customer order model. Let's look at this process from the perspective of service architecture.
Suppose the customer service sends the request data to the order service as follows:
"order": {
"customerId": 1
"itemId": "A152"
}
Customers and order services use contracts to communicate with each other. The contract (another service request) is displayed in JSON format. As a Java model, the OrderDto class represents the contract between customer service and order service:
Public class OrderDTO {
Private int customerId
Private String itemId
/ / constructor, getters, setters
} 3.1. Using client modules (libraries) to share DTO
Microservices require specific information from other services to handle any request. Suppose there is a third microservice that receives the order payment request. Unlike ordering services, this service requires different customer information:
Public class CustomerDTO {
Private String firstName
Private String lastName
Private String cardNumber
/ / constructor, getters, setters
}
If we also add a delivery service, the customer information will be:
Public class CustomerDTO {
Private String firstName
Private String lastName
Private String homeAddress
Private String contactNumber
/ / constructor, getters, setters
}
Therefore, putting the CustomerDTO class in a shared module no longer meets the desired purpose. In order to solve this problem, we adopt a different approach.
In each microservice module, let's create a client module (library) and a server module next to it:
Order-service
| | _ order-client |
| | _ order-server |
The order client module contains a DTO that is shared with customer service. Therefore, the structure of the order client module is as follows:
Order-service
└── order-client
OrderClient.java
OrderClientImpl.java
OrderDTO.java
OrderClient is an interface that defines the order method that processes the order request:
Public interface OrderClient {
OrderResponse order (OrderDTO orderDTO)
}
To implement the order method, we use the RestTemplate object to send a POST request to the order service:
String serviceUrl = "http://localhost:8002/order-service";
OrderResponse orderResponse = restTemplate.postForObject (serviceUrl + "/ create")
Request, OrderResponse.class)
In addition, the order client module is ready to use. Now it becomes a dependent library for the customer service module:
[INFO]-maven-dependency-plugin:3.1.2:list (default-cli) @ customer-service-
[INFO] The following files have been resolved:
[INFO] com.baeldung.orderservice:order-client:jar:1.0-SNAPSHOT:compile
Of course, this doesn't make any sense if there is no order-server module to expose the "/ create" service endpoint to the order client:
@ PostMapping ("/ create")
Public OrderResponse createOrder (@ RequestBody OrderDTO request)
Because of this service endpoint, customer service can send order requests through its order client. By using client modules, microservices communicate with each other in a more isolated way. The properties in DTO are updated in the client module. Therefore, the destruction of the contract is limited to services that use the same client module.
At this point, the study on "how to share DTO across microservices" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 231
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.