问题
The short version of my question is that I'm trying to wrap my head around how models are "shared" between an API Gateway and internal microservices. I assume that the gateway can be responsible for transforming calls to multiple services and returning a new aggregated representation of the data. How does the gateway know about the available models from the microservices?
In my simple example. I have:
- API Gateway
- User Service
- Restaurant Service
User Service
This rest service would expose an /api/users
endpoint
Model
{
"id": 12345,
"name": "Joe Bloggs",
"e-mail": "joe@bloggs.com",
"lastloggedin": "05/10/2017"
}
Restaurant Service
This rest service would expose an /api/dishes
endpoint
Model
{
"dishes": [
{
"userId": 12345,
"isActive": true,
"type": "italian",
"sub-types": [
"pizza",
"pasta"
]
},
{
"userId": 12345,
"isActive": false,
"type": "american",
"sub-types": [
"burgers",
"steaks"
]
}
]
}
API Gateway
The API Gateway could allow for someone to call /api/user/12345/dishes
or /api/dishes
and it would return dishes for cooks. Along with some basic user details.
Model
{
"cook": "Joe Bloggs",
"lastloggedin": "05/10/2017",
"dishes": [
{
"type": "italian",
"sub-types": [
"pizza",
"pasta"
]
}
]
}
At this point. I've identified 3 models (i.e. C# classes). 2 from the internal services and 1 for the new representation of the data being returned from the API Gateway. I am unsure as to how the API Gateway knows about the User and Restaurant service models without coupling those models directly to the API Gateway as well. Essentially sharing code between the Gateway and the Microservices, which I believe is not desirable.
回答1:
I am unsure as to how the API Gateway knows about the User and Restaurant service models without coupling those models directly to the API Gateway as well
An API gateway is just that: A gateway.
According to P of EAA, a gateway is defined as:
An object that encapsulates access to an external system or resource.
So the gateway is about access. There is nothing about a gateway which says it needs to care about the types being exposed over it. In fact, there is no coupling between the types exposed over your service and the gateway itself, at least at design-time.
An API gateway, such as Ocelot, will happily handle any types in the request and pass them through to the downstream service. If you change the service types, great! You don't need to change the gateway. It will continue working as if nothing happened.
来源:https://stackoverflow.com/questions/46581022/api-gateway-and-sharing-service-models