一个Spring Boot微服务如何知道它可以向另一个服务发送POST请求?

huangapple go评论84阅读模式
英文:

How does the one Spring Boot microservice know how it can send a POST to another service?

问题

在这个教程中(Spring Cloud微服务),代码来自该存储库以用来说明示例:

https://github.com/saturnism/spring-cloud-gcp-guestbook/tree/master/1-bootstrap

在这个存储库中,有两个使用Spring Boot的Maven项目(guestbook-frontend和guestbook-service)。两者都作为服务运行。通过前端服务,可以将消息发布到后端服务。我的问题是,前端是如何配置的,以便知道将POST请求发送到哪里?这两者之间的连接在哪里?

英文:

In this tutorial (Spring Cloud Microservices), the code from this repository is used to illustrate the example:

https://github.com/saturnism/spring-cloud-gcp-guestbook/tree/master/1-bootstrap

In the repo, two Maven projects are found using Spring Boot (guestbook-frontend and guestbook-service). Both are run as a service. Via the frontend service, messages can be posted to the backend service. My question is, where is it configured such that the frontend knows where the POSTs go to? Where is the connection between the two?

答案1

得分: 1

请查看@FeignClient注解,位于以下链接中:

https://github.com/saturnism/spring-cloud-gcp-guestbook/blob/master/1-bootstrap/guestbook-frontend/src/main/java/com/example/frontend/GuestbookMessagesClient.java

英文:

Check @FeignClient annotation in

https://github.com/saturnism/spring-cloud-gcp-guestbook/blob/master/1-bootstrap/guestbook-frontend/src/main/java/com/example/frontend/GuestbookMessagesClient.java

答案2

得分: 1

为了详细解释这个问题,我将强调微服务的核心模式:

  1. 核心开发模式
  2. 路由模式
  3. 客户端弹性模式
  4. 安全模式
  5. 日志和追踪模式
  6. 构建和部署模式

上述提到的模式用于解决我们在转向微服务时遇到的问题。你的问题中强调的问题之一是:“我们无法确定哪台机器将运行我们的微服务”。解决这个问题的一种天真的方法是我们维护某种包含所有机器及其服务详细信息的映射。为了在服务之间建立通信,我们可以使用那个映射。但这种方法是天真的,它有自己的问题。

因此,为了解决这个问题,我们将不得不使用路由模式。在路由模式中,有一个服务发现的概念。不同的平台提供了不同的服务发现方式。例如:Netflix提供了Eureka和Feign Client,后来被添加到了Spring Cloud中,HashiCorp使用Consul提供了服务发现,AWS ECS也有其自己的服务发现机制。

话虽如此,我可以总结出有多种方式可以从一个spring-boot的微服务调用另一个服务。但由于你更关心spring cloud,你需要Eureka Server和Feign Client。将它们添加到你的项目中非常简单。

你可以按照这个教程来使用Eureka和Feign Client:https://dzone.com/articles/microservices-communication-feign-as-rest-client

英文:

In order to explain this question in detail, I will highlight the core patterns of microservices

  1. Core Development Patterns
  2. Routing Patterns
  3. Client Resiliency Pattern
  4. Security Pattern
  5. Logging and Tracing Pattern
  6. Build and Deployment Pattern

Above mentioned patterns are used to deal with problems that come when we move onto microservices. One of the problems which your question is highlighting is that "We can never be sure which machine will be running our microservice" One naive approach to resolve this problem is that we maintain some kind of map with all the details of machines and its services. And in order to establish communication between services we can use that map. But this approach is naive and it has its own problems.

SO to deal with this we will have to make use of Routing Patterns. In Routing Patterns there is a concept of Service Discovery There are different ways to do Service Discovery that is provided by different platforms. For instance: Netflix provides Eureka and Feign Client which later on was added in Spring Cloud, HashiCorp provides Service Discovery with Consul, and AWS ECS also has its service discovery mechanism.

Having said that I can conclude that there are different ways by which you can call another service from one microservice of spring-boot. But since you are more concerned about spring cloud, In spring Cloud you would need Eureka Server and Feign Client. Adding them to your project is fairly simple.

you can follow this tutorial to use Eureka and Feign Client https://dzone.com/articles/microservices-communication-feign-as-rest-client

huangapple
  • 本文由 发表于 2020年9月10日 18:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63828114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定