REST API 使用哪种设计模式?

huangapple go评论59阅读模式
英文:

REST API uses which design pattern

问题

  1. 在创建Spring Boot应用程序的REST API时,我们遵循哪种设计模式?
  2. 微服务使用哪种设计模式?
英文:

I have the below question asked in the interview but could not able to find any straight answers to the below questions.

  1. What design pattern do we follow while creating the REST API in Spring boot application?
  2. Microservices uses which design pattern?

答案1

得分: 1

对于你的第一个问题,我们使用(MVC)模式来创建一个REST API,但在REST API中我们不使用View,而是使用JSON响应,同时还可以使用在构建REST API时可能会用到的其他模式,包括Command Query Responsibility Segregation (CQRS)模式。值得一提的是,微服务也使用了我们刚刚提到的这种模式,因为微服务采用了一种将大型复杂应用构建为一组小型独立服务的架构风格。在构建微服务时常用的设计模式是Command Query Responsibility Segregation (CQRS)模式,因为该模式涉及将数据的读取写入责任分离到单独的组件中,从而使组件具有单一责任

英文:

for your question one, we're using the (MVC) pattern to create a REST API but in the REST API we're not using View instead we're using a JSON responses, and also other patterns that may be used while building REST API including Command Query Responsibility Segregation (CQRS) pattern, its also worth to be mentioned that Microservices also use this pattern which we mentioned now since the Microservices uses an architectural style that involves building a large, complex application as a collection of small, independent services, The design pattern that is commonly used when building microservices is the Command Query Responsibility Segregation (CQRS) pattern because This pattern involves separating the responsibilities of reading and writing data into separate components which gives single responsibility to the component as they already are.

答案2

得分: 1

面试官提出的问题相当广泛。无论如何,我认为展示基于Spring MVC模式的基本知识以及Spring Boot中嵌入的Tomcat Servlet容器的运作方式非常重要(基本上是Spring Boot控制器的主要角色)。

在Spring MVC中,通过添加spring-boot-starter-web依赖项,您可以使用各种控制器来处理HTTP请求并创建REST API。这个依赖项包括一些关键库,包括Spring MVC框架和Tomcat Servlet容器。然后,您有两个选项,要么使用@Controller,要么使用@RestController,来为您的Web应用程序创建自己的Servlet。由于面试官询问了REST API设计的问题,我更倾向于使用@RestController,因为此注释能够生成RESTful响应实体。

至于第二个问题,我谨慎回答,因为微服务架构是一种“架构模式”,这是一种更复杂和精密的业务服务后端结构。总体而言,我认为“事件驱动设计模式”是实现成功的微服务架构的基本选项之一。

在微服务架构中,“事件驱动设计模式”对于实现不同微服务项目之间的通信非常有用。由于微服务被设计为小型、独立且松耦合的,它们通常使用“异步消息(也称为事件)”相互通信。通过使用“事件驱动设计模式”,您可以创建微服务之间的“发布-订阅模型”,这可以更容易地扩展系统并随时间添加新的微服务。

但不要忘记,微服务架构包含各种有用的设计模式!

英文:

The questions that the interviewer has covered are fairly broad questions. Anyway, I believe that it is important to show your basic knowledge based on the Spring MVC pattern, and how the embedded Tomcat Servlet container in the Spring Boot operates. (So basically it is the main role of the Spring Boot Controller)

In the Spring MVC, you use various controllers to handle HTTP requests and create REST APIs, by adding spring-boot-starter-web dependency. This dependency includes some key libraries including the Spring MVC framework and the Tomcat servlet container. Then, you got two options either @Controller or @RestController, to create your own servlet for your web application.
Since the interviewer is asking about REST API design, I would prefer to use
@RestController, because this annotation is capable to produce RESTful response entities.

As for the second question, I am prudent to answer it, because Microservice Architecture is a type of "Architecture Pattern", which is a more complicated and sophisticated backend structure for the business service. Overall, I believe that the Event-driven design pattern is a good option as a fundamental one for implementing a successful MSA.

In the MSA, the event-driven design pattern is useful for enabling communication between different microservices projects. Because microservices are designed to be small, independent, and loosely coupled, they often communicate with each other using asynchronous messages(a.k.a. events). By using an event-driven design pattern, you can create a publish-subscribe model of communication between microservices, which can make it easier to scale the system and add new microservices over time.

BUT don't forget that MSA contains various design patterns that are useful as well!

huangapple
  • 本文由 发表于 2023年1月9日 10:21:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定