Java 使用Spring云流的Spring REST请求响应

Java 使用Spring云流的Spring REST请求响应,java,spring,spring-integration,spring-cloud,spring-cloud-stream,Java,Spring,Spring Integration,Spring Cloud,Spring Cloud Stream,我正在尝试使用SpringIntegrationGateway将rest端点请求/响应与SpringCloud流连接起来。下面的代码适用于第一个rest调用,但后续调用不起作用。我知道SpringCloudStream用于消息传递/异步操作。但这是一个实际场景,其中需要请求/响应同步 SpringBootApplication package com.example.restgateway; import org.springframework.beans.factory.annotation

我正在尝试使用SpringIntegrationGateway将rest端点请求/响应与SpringCloud流连接起来。下面的代码适用于第一个rest调用,但后续调用不起作用。我知道SpringCloudStream用于消息传递/异步操作。但这是一个实际场景,其中需要请求/响应同步

SpringBootApplication

package com.example.restgateway;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.dsl.HeaderEnricherSpec;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@EnableBinding({RestGatewayApplication.GatewayChannels.class})
@SpringBootApplication
public class RestGatewayApplication {

  interface GatewayChannels {

    String TO_UPPERCASE_REPLY = "to-uppercase-reply";
    String TO_UPPERCASE_REQUEST = "to-uppercase-request";

    @Input(TO_UPPERCASE_REPLY)
    SubscribableChannel toUppercaseReply();

    @Output(TO_UPPERCASE_REQUEST)
    MessageChannel toUppercaseRequest();
  }

  @MessagingGateway
  public interface StreamGateway {
    @Gateway(requestChannel = ENRICH, replyChannel = GatewayChannels.TO_UPPERCASE_REPLY)
    String process(String payload);
  }

  private static final String ENRICH = "enrich";

  public static void main(String[] args) {
    SpringApplication.run(RestGatewayApplication.class, args);
  }

  @Bean
  public IntegrationFlow headerEnricherFlow() {
    return IntegrationFlows.from(ENRICH).enrichHeaders(HeaderEnricherSpec::headerChannelsToString)
        .channel(GatewayChannels.TO_UPPERCASE_REQUEST).get();
  }

  @RestController
  public class UppercaseController {
    @Autowired
    StreamGateway gateway;

    @GetMapping(value = "/uppercase/{string}",
        produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    public ResponseEntity<String> getUser(@PathVariable("string") String string) {
      return new ResponseEntity<String>(gateway.process(string), HttpStatus.OK);
    }
  }


  @StreamListener(GatewayChannels.TO_UPPERCASE_REQUEST)
  @SendTo(GatewayChannels.TO_UPPERCASE_REPLY)
  public Message<?> process(Message<String> request) {
    return MessageBuilder.withPayload(request.getPayload().toUpperCase())
        .copyHeaders(request.getHeaders()).build();
  }

}

不清楚你想做什么;IntegrationFlow绕过目标主题,直接向输入通道发送消息;回复(或者)通过主题和
replyChannel
标题(网关所需的内容将丢失,因为它不可序列化)


它将交替工作,因为应答通道上有两个订阅者-网关和绑定。默认情况下,当通道有2个订户时,消息以循环方式分发。一条消息将发送到网关,绑定的下一条消息,等等。

我正在尝试执行与此答案相同的操作-->是;但是在同一个应用程序中有
@StreamListener
;这是错误的,因为每个通道上有2个订阅者-绑定和本地引用。
spring:
  cloud:
    stream:
      bindings:
        to-uppercase-request:
          destination: to-uppercase-request
          group: stream-to-uppercase-request
          producer:
            required-groups: stream-to-uppercase-request
        to-uppercase-reply:
          destination: to-uppercase-reply
          group: gateway-to-uppercase-reply
          producer:
           required-groups: gateway-to-uppercase-reply
      kafka:
        binder:
          brokers:
          - 192.168.34.210:9092
      default-binder: kafka


server:
  port: 8080