Spring Boot-微服务之间如何通信?

Spring Boot-微服务之间如何通信?

问题描述:

我目前正在从事Spring Boot微服务项目.我已经创建了服务,并且每个服务都单独运行.这样,我需要一些服务来与其他服务进行通信.我该如何实现?

I'm currently working on a Spring Boot microservices project. I have created services and each service is running separately. With this, I need some services to communicate with other services. How can i achieve that?

我看到一些有关此的博客,这些博客使用Netflix,Eureka云服务器来实现此目的.在不使用云服务器的情况下,有什么方法可以在本地环境中实现?

I saw some blogs about this which use Netflix, Eureka cloud servers to achieve this. Is there any way I can achieve this in my local environment without using cloud servers?

当然可以. 微服务只是REST服务. 您需要了解REST服务的工作方式. 之后,只需使用Spring-boot编写2个微服务(2个REST服务:生产者服务和消费者服务),让它们在不同的服务器端口下运行,从另一个服务器端口调用消费者服务即可,就是这样:您拥有了微服务. 现在,这是编写微服务的原始方法.

Of course you can. Microservices are just REST-Services. You need to understand how REST-Services work. After that just write 2 Microservices (2 Rest-Services: producer-service and consumer-service) with Spring-boot, let them run under different server-ports, call the consumer-service from the other, and that's it: you have your Microservices. Now this is the primitive way to write Microservices.

要使其进化,您需要添加一些魔术"(无火箭科学),例如使用Ribbon在两个生产者服务"实例之间分配负载.

To make them evolve, you need to add some "magic" (no rocket science), for example using Ribbon to distribute load between two instances of your "producer-service".

您可以使用发现服务,该服务只是带有注解@EnableEurekaServer的春季启动应用程序(您需要在pom中添加适当的依赖项) 现在向您的第一个(原始)微服务添加注解@EnableDiscoveryClient到主要类,并在这两者的application.properties(或application.yml)中指向您的eureka-service的defaultZone,启动您的eureka-service(发现服务),并2个微服务:那些将在发现服务上注册.当然,现在您无需在消费者服务中对生产者服务的http地址进行硬编码.
看看教程

You may use a discovery service which is just a spring-boot application with the annotation @EnableEurekaServer (You need to add the appropriate dependency in your pom) Now add to your first (primitive) Microservices the annotation @EnableDiscoveryClient to the main classes and the defaultZone pointing to your eureka-service in the application.properties (or application.yml) of both, start your eureka-service (discovery service) and the 2 Microservices: those will register on the discovery-service. Of course now you don't need to hard-code the http address of the producer-service in the consumer-service.
Take a look at this tutorial

于2018年11月21日格林尼治标准时间12:41编辑

Edited on 21th of November 2018 at 12:41 GMT

假设您的第一个(琐碎)微服务(纯休息服务)在PC上的端口8091下运行.

Suppose that your first (trivial) microservice (a pure rest-service) is running on your PC under port 8091.

在第二个(琐碎的)微服务的控制器中,您可以使用RestTemplate.getForEntity(url,responseType,uriVariables)调用第一个服务,就像链接教程中的示例一样:

In the controller of your second (trivial) microservice you call your first service using the RestTemplate.getForEntity(url,responseType,uriVariables) like so for the example in the linked tutorial:

ResponseEntity<CurrencyConversionBean> responseEntity = 
   new RestTemplate().getForEntity(
        "http://localhost:8091/currency-exchange/from/{from}/to/{to}", CurrencyConversionBean.class,  uriVariables);

哪里 url:您的第一个(微型)(其余)服务的URL. responseType:等待作为响应的对象的类/类型. uriVariables:是包含URI模板变量的映射.

Where url: the url of your first (micro)(rest)service. responseType: the class/type of the object awaited as response. uriVariables: is a map containing variables for the URI template.