简体   繁体   English

重型REST应用程序

[英]Heavy REST Application

I have an Enterprise Service Bus (ESB) that posts Data to Microservices (MCS) via Rest. 我有一个企业服务总线(ESB),它通过Rest将数据发布到微服务(MCS)。 I use Spring to do this. 我用Spring来做这件事。 The main Problem is that i have 6 Microservices, that run one after one. 主要问题是我有6个微服务,一个接一个地运行。 So it looks like this: MCS1 -> ESB -> MCS2 -> ESB -> ... -> MCS6 所以它看起来像这样:MCS1 - > ESB - > MCS2 - > ESB - > ... - > MCS6

So my Problem looks like this: (ESB) 所以我的问题看起来像这样:(ESB)

@RequestMapping(value = "/rawdataservice/container", method =  RequestMethod.POST)
@Produces(MediaType.APPLICATION_JSON)
public void rawContainer(@RequestBody Container c)
{
    // Here i want to do something to directly send a response and afterwards execute the 
    // heavy code
    // In the heavy code is a postForObject to the next Microservice
}

And the Service does something like this: 服务做这样的事情:

@RequestMapping(value = "/container", method = RequestMethod.POST)
public void addDomain(@RequestBody Container container)
{
    heavyCode();
    RestTemplate rt = new RestTemplate();
    rt.postForObject("http://134.61.64.201:8080/rest/rawdataservice/container",container, Container.class);
}

But i dont know how to do this. 但我不知道该怎么做。 I looked up the post for Location method, but i dont think it would solve the Problem. 我查找了Location方法的帖子,但我认为它不会解决问题。

EDIT: I have a chain of Microservices. 编辑:我有一系列的微服务。 The first Microservice waits for a Response of the ESB. 第一个微服务器等待ESB的响应。 In the response the ESB posts to another Microservice and waits for a response and the next one does the same as the first one. 在响应中,ESB发布到另一个微服务并等待响应,下一个响应与第一个相同。 So the Problem is that the first Microservice is blocked as long as the complete Microservice Route is completed. 所以问题是只要完整的微服务路由完成,第一个微服务就会被阻塞。

ESB Route Maybe a picture could help. ESB路线也许图片可能有所帮助。 1.rawdataService 2.metadataservice 3.syntaxservice 4.semantik 1.rawdataService 2.metadataservice 3.syntaxservice 4.semantik

// Here i want to do something to directly send a response and afterwards execute the 
// heavy code

The usual spelling of that is to use the data from the http request to create a Runnable that knows how to do the work, and dispatch that runnable to an executor service for later processing. 通常的拼写是使用来自http请求的数据来创建一个知道如何完成工作的Runnable,并将该runnable分配给执行程序服务以供以后处理。 Much the same, you copy the data you need into a queue, which is polled by other threads ready to complete the work. 大致相同,您将所需的数据复制到队列中,该队列由准备完成工作的其他线程轮询。

The http request handler then returns as soon as the executor service/queue has accepted the pending work. 一旦执行程序服务/队列接受了挂起的工作,http请求处理程序就会返回。 The most common implementation is to return a "202 Accepted" response, including in the Location header the url for a resource that will allow the client to monitor the work in progress, if desired. 最常见的实现是返回“202 Accepted”响应,包括在Location头中的资源URL,如果需要,将允许客户端监视正在进行的工作。

In Spring, it might be ResponseEntity that manages the codes for you. 在Spring中,可能是ResponseEntity为您管理代码。 For instance 例如

ResponseEntity.accepted()....

See also: 也可以看看:

From the caller's point of view, it would invoke RestTemplate.postForLocation , receive a URI, and throw away that URI because the microservice only needs to know that the work as been accepted 从调用者的角度来看,它将调用RestTemplate.postForLocation ,接收URI并丢弃该URI,因为微服务只需知道工作已被接受

Side note: in the long term, you are probably going to want to be able to correlate the activities of the different micro services, especially when you are troubleshooting. 旁注:从长远来看,您可能希望能够关联不同微服务的活动,尤其是在进行故障排除时。 So make sure you understand what Gregor Hohpe has to say about correlation identifiers . 因此,请确保您了解Gregor Hohpe对相关标识符的评价

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

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