英文:
Implementing microservices architecture, managing its interdependencies
问题
想象一个使用微服务的OAuth提供者。我们将为授权端点、令牌端点等每个端点创建微服务。
例如,每个端点都是不同的微服务,大多数端点都需要通过ID检索客户端以进行身份验证请求。那么我应该如何共享从数据库获取客户端的方法,客户端模型以及与之相关的所有实体?
我应该如何在微服务之间共享公共方法和类?
如果微服务A依赖于微服务B,那么我需要通过HTTP进行它们之间的通信,还是只需添加项目引用?或者也许某些微服务依赖于另一个的情况是不好的,表明我们的服务太微小了?
在我们的微服务中使用领域驱动设计(DDD)的情况是否表明我们的微服务太大了(或者它增加了太多复杂性)?如果它们确实是微小的,那是否意味着一些简单的文件夹,如“模型”、“服务”和“端点”,就足够了?
英文:
Imagine a OAuth provider using microservices. We will have microservice for authorization endpoint, token endpoint etc.
For example, every endpoint is different microservice and most endpoints will need to retrieve client by id to authenticate request. So how should I share method to get client from database, model for client and all related to it entities?
How shall I share common methods and classes between microservices?
If microservice A depends on microservice B, then I need to do communication between them over HTTP or I just can add project reference? Or maybe situation where some microservice depends on another is bad and says that our services are too micro?
Does scenario where we use DDD in our microservices says that our microservices are too big (or it adds too many complexity)? If they are really micro, doesn't it mean that some simple folders like Models
, Services
and Endpoints
are enough?
答案1
得分: 6
So how should I share method to get client from database, model for client and all related to it entities?
你不应该这样做,因为在微服务架构中通常共享数据库是一个非常不好的做法。
How shall I share common methods and classes between microservices?
一般来说,你不应该这样做。你可以维护一些用于契约(但首先应考虑某种模式注册表)和横切关注点的库,但仅此而已。
If microservice A depends on microservice B,
问题是你应该尽量避免传统意义上的服务相互依赖。服务应该尽可能松散耦合。
then I need to do communication between them over HTTP
有些情况下,你可能需要某种形式的RPC,但一般的方法是尽量避免服务之间的同步通信,而是采用异步通信的方式(例如通过某种消息总线)。
I just can add project reference?
如果你可以从一个微服务中引用另一个微服务,那么你就不是在使用微服务了。
If they are really micro, doesn't it mean
我认为“微服务”这个术语在一般情况下没有那么明确定义,在不同地方可能差异很大。
请注意,微服务架构的目标不是将每个端点都拆分为单独的服务,实际上这样做可能会非常低效。我认为主要目标是将系统拆分为松散耦合甚至是独立的组件(理想情况下尽可能小),实际上,如果你发现几个组件紧密耦合,有时可以将它们合并(或重新设计架构以使它们可以解耦)。
我强烈建议学习以下资源:
-
Building Microservices: Designing Fine-Grained Systems 1st Edition by Sam Newman
-
microservices.io(以及Chris Richardson编写的Microservices Patterns: With Examples in Java)
英文:
> So how should I share method to get client from database, model for client and all related to it entities?
You don't, because sharing database between microservices usually is a very bad practice in microservice architecure.
> How shall I share common methods and classes between microservices?
In general - you should not. You can maintain several libraries for contracts (but you should consider some kind of Schema Registry first) and cross-cutting concerns, but that's it.
> If microservice A depends on microservice B,
The thing is that you should avoid services being depended on each other in traditional sense. Services should be as loosely coupled as possible
> then I need to do communication between them over HTTP
There are cases when you will need some kind of RPC but the general approach is to try avoid synchronous communication between services in favor of asynchronous one (i.e. via some message bus for example)
> I just can add project reference?
If you can add a reference from one microservice on another then you don't have microservices.
> If they are really micro, doesn't it mean
I would argue that term "microservice" is not that well defined in general and from place to place can differ vastly.
Note that the goal of microservice architecture is not to split every endpoint into separate service, actually it can be very counterproductive to do so. I would argue that the main goal is to split your system into loosely coupled or even independent components (ideally as small as possible), actually if you are finding that several components are tightly coupled there are cases when you can just unite them back (or rework architecture so they can be decoupled).
I highly recommend to study the following resources:
-
Building Microservices: Designing Fine-Grained Systems 1st Edition
by Sam Newman -
microservices.io (and Microservices Patterns: With Examples in Java by Chris Richardson)
答案2
得分: 1
微服务的关键在于它们应该是独立的。这意味着它们可以独立开发和部署。这可能需要每个服务都有一个单独的、向后兼容的版本化API。还需要单独的数据库等。重要的是,通常还意味着使用单独的团队,使用单独的存储库、单独的解决方案,可能使用不同的编程语言。
有时会使用API网关来将请求路由到服务,这可能会执行一些操作,如身份验证。
调用其他服务不是被禁止的,可以通过服务的公共API来完成,无论使用什么协议。如果您的服务使用HTTP,应该使用HTTP进行调用。但需要小心避免导致性能完全崩溃的级联调用。
英文:
The point of microservices is that they should be independent. Meaning they can be developed and deployed independently. This may require that each service has a separate versioned, backwards compatible API. Separate databases, etc. Importantly it usually also means separate teams, using separate repositories, separate solutions, and possibly separate languages.
Sometimes an API gateway is used to route requests to services, and this may do some things, like authentication.
Calling other services is not prohibited, and are done thru the public API of the service, whatever protocol that may use. If your services use HTTP you should call using HTTP. But you need to be careful to avoid cascades of calls that can absolutely ruin performance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论