英文:
Best practice when sending a post request to microservice
问题
我使用Spring构建了一对相当简单的微服务。其中包括一个名为main-service的服务,它具有一个ProductRepository;还有一个名为discount-service的服务,它有一个包含按类别分类的产品折扣的DiscountRepository。
当我调用main-service以获取特定id的产品时,它首先提取产品详细信息,然后调用discount-service以获取折扣后的价格。然后将折扣后的价格发送回main-service,添加到产品详细信息的数据传输对象中,并与其余产品详细信息一起呈现给客户。
我的问题是:发送更新DiscountRepository的POST请求的最佳实践是什么?我应该通过main-service发送还是直接调用discount-service?
我知道所有这些都可以在一个微服务中完成,但我正在练习使用@Feignclient连接多个微服务,因此产生了这个问题。
谢谢!
英文:
I have built a fairly simple pair of microservices using Spring. It consists of main-service which has a ProductRepository and a discount-service which has a DiscountRepository containing product discounts by category.
When I call the main-service to get product by id, it pulls product details, then calls the discount-service and gets a discounted price. Which then is sent back to the main-service, added to the product details dto and presented to the customer together with the rest of the product details.
My question is: What is the best practice for sending a post request to update the DiscountRepository? Should I send it through the main-service or should I call the discount-service directly?
I appreciate that all this could have been done in one microservice, but I am practising connecting multiple microservices using @Feignclient and hence came up with this question.
Cheers!
答案1
得分: 1
没有一个适用的单一“最佳实践”;你需要对你的API运作方式做出判断。
例如,你可以决定拥有一个单一的“storefront” API,在这种情况下,/discounts
请求可能会被转发到折扣(Discounts)微服务。
另一种情况是,你可能决定“商店管理员”是一个单独的关注点,在应用程序域内完全运作,没有公共访问。在这种情况下,你不会转发 /discounts
,而是让客户端或管理员微服务直接调用折扣服务。
哪种方法更合适取决于你的业务优先级。
英文:
There's no single "best practice" that applies; you will have to make a judgment about how you intend your API to operate.
For example, you might decide to have a single "storefront" API where everything is managed, and in this case the /discounts
requests might get forwarded on to the Discounts microservice.
Alternately, you might decide that "store admin" is a separate concern and operates entirely inside your application domain, with no public access. In this case, you wouldn't forward /discounts
but rather would have a client or Admin microservice make calls to Discounts directly.
Which approach is more suitable depends on your business priorities.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论