简体   繁体   English

在微服务中配置下游 API 端点

[英]Configure downstream API endpoints in microservices

In a microservice world, what is the recommended way of configuring the endpoint of a downstream API?在微服务世界中,配置下游 API 端点的推荐方法是什么?

For example, if Service A needs to invoke an endpoint in Service B, we have two options:例如,如果服务 A 需要调用服务 B 中的端点,我们有两种选择:

a.一个。 Make the hostname and port number of Service B's API configurable in Service A (service-b:8080) and append the path URI in your code使服务 B 的主机名和端口号 API 在服务 A (service-b:8080) 中可配置,并将 append 设置为代码中的路径 URI

or或者

b.湾。 Make the complete endpoint configurable in Service A (http://service-b:8080/somepath)在服务 A (http://service-b:8080/somepath) 中使完整的端点可配置

While I like the idea of making the endpoint configurable, it leaves a lot of room for error because the entire path needs to be specified.虽然我喜欢使端点可配置的想法,但它留下了很大的错误空间,因为需要指定整个路径。 It also doesn't fit well when multiple endpoints need to be called from Service A to Service B which may potentially have different paths, requiring us to configure multiple endpoints.当需要从服务 A 调用多个端点到服务 B 时,它也不太适合,这可能具有不同的路径,需要我们配置多个端点。

On the other hand, option (a) seems more scalable due to above mentioned reasons.另一方面,由于上述原因,选项 (a) 似乎更具可扩展性。

Most search results online just demonstrated how a service can call another service and uses a hardcoded URL to demo this.大多数在线搜索结果只是演示了一个服务如何调用另一个服务,并使用硬编码的 URL 进行演示。 It would be good to know how is the community doing this in real world projects.很高兴知道社区在现实世界的项目中是如何做到这一点的。

PS: We use Spring Webflux and deploy to k8s. PS:我们使用Spring Webflux并部署到k8s。

I have seen mostly that teams use option a, where the serviceB "baseUrl" (which is basically https://serviceb-hostname:8080) is injected as environment property (kubernetes configmap) into the application during deployment.我看到大多数团队使用选项 a,其中 serviceB“baseUrl”(基本上是 https://serviceb-hostname:8080)在部署期间作为环境属性(kubernetes configmap)注入到应用程序中。

The specific API specific paths are configured in application yaml or in the "proxy config" class itself as constants (eg. ServiceBProxy.java - proxy classes are those which will make rest-calls to the dependent services like service B). The specific API specific paths are configured in application yaml or in the "proxy config" class itself as constants (eg. ServiceBProxy.java - proxy classes are those which will make rest-calls to the dependent services like service B).

Here is a portion of application yaml from one of the microservices (from one of my projects):这是来自其中一个微服务(来自我的一个项目)的应用程序 yaml 的一部分:

authorizationService:
  baseUri: ${authorizationServiceBaseUri}/api

tenantService:
  baseUri: ${tenantServiceBaseUri}/api/v1
  tenantsUri: ${tenantService.baseUri}/tenants

settingsService:
  baseUri: ${settingsServiceBaseUri}

iamService:
  fetchBatchSize: 500
  baseUri: ${iamServiceBaseUri}

Here the values of iamServiceBaseUri,settingsServiceBaseUri,tenantServiceBaseUri,authorizationServiceBaseUri are all injected during deployment.这里iamServiceBaseUri,settingsServiceBaseUri,tenantServiceBaseUri,authorizationServiceBaseUri的值都是在部署时注入的。 And each of them contains the clusterIP with port.它们中的每一个都包含带有端口的clusterIP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM