我正在使用MicroServices构建RESTAPI,这意味着我有不同的服务来提供不同的资源。假设我有以下服务:
ServiceA正在提供资源resourcesA和resourcesA1,URL如下
https://my-internal-endpoint-for-serviceA/resourcesA
https://my-internal-endpoint-for-serviceA/resourcesA1
ServiceB正在通过以下URL提供资源resourcesBesourcesB1
https://my-internal-endpoint-for-serviceB/resourcesB
https://my-internal-endpoint-for-serviceB/resourcesB1
现在,我想在Azure API管理中管理它们。为了发布它们(通过从服务导入Swagger文档),API管理门户需要API路径进行发布。因此,serviceA和serviceB可以发布如下:
https://my-api-azure-api.net/serviceA/resourcesA
https://my-api-azure-api.net/serviceA/resourcesA1
https://my-api-azure-api.net/serviceB/resourcesB
https://my-api-azure-api.net/serviceB/resourcesB1
但为了实现更多基于资源的API管理,我希望发布的API更像以下内容:
https://my-api-azure-api.net/resourcesA
https://my-api-azure-api.net/resourcesA1
https://my-api-azure-api.net/resourcesB
https://my-api-azure-api.net/resourcesB1
不幸的是,API管理不允许我将2个API(serviceA和serviceB)发布到同一路径(本例中为根路径)。我不想把服务名称(或类似的名称)放在URL路径中,因为服务名称与它提供的资源名称重复。我该如何解决这个问题?
Azure API管理策略可以在这里帮助您,特别是具有转发请求能力的控制流。文件如下:https://learn.microsoft.com/en-us/azure/api-management/api-management-policy-reference
我将通过将资源设置为一个单独的API来实现这一点,通过swagger添加一个资源,然后将其他服务添加到该资源中以使其完整(正如您希望它显示为一个完整的服务)。一旦这一点到位,您就可以自由地应用这些政策。
注意:您可能需要以以下方式扩展资源路径
https://my-api-azure-api.net/mynewservice/resourcesA
https://my-api-azure-api.net/mynewservice/resourcesA1
https://my-api-azure-api.net/mynewservice/resourcesB
https://my-api-azure-api.net/mynewservice/resourcesB1
也许这个问题的答案会有所帮助:如何使用Azure API管理链接API
您可以使用相同的策略将API管理中同一API的多个操作映射到不同的后端API。但一般来说,所有API都以<myGateway>.azure-api.net/<myApi>/<myOperation>
的形式公开。