1

我创建了一个新示例并将代码滑入客户端和服务器端。

完整的代码可以在这里找到。

服务器端有3个版本。

  • 服务器无 Spring Boot 应用,使用 Spring Integration RSocket InboundGateway。
  • server-boot重用 Spring RSocket autconfiguration,并ServerRSocketConnecter通过ServerRSocketMessageHanlder.
  • server-boot-messsagemapping不使用 Spring Integration,只使用 Spring Boot RSocket autconfiguration,and @Controllerand @MessageMapping

客户端有2个版本。

  • 客户端,使用 Spring Integration Rocket OutboundGateway 发送消息。
  • client-requester使用 发送消息RSocketRequester,根本不使用 Spring Integration。

客户端和服务器交互方式为REQUEST_CHANNEL,通过TCP/localhost:7000连接服务器。

服务器

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-rsocket</artifactId>
</dependency>

应用类:

@Configuration
@ComponentScan
@IntegrationComponentScan
@EnableIntegration
public class DemoApplication {

    public static void main(String[] args) throws IOException {
        try (ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(DemoApplication.class)) {
            System.out.println("Press any key to exit.");
            System.in.read();
        } finally {
            System.out.println("Exited.");
        }

    }

    @Bean
    public ServerRSocketConnector serverRSocketConnector() {
        return new ServerRSocketConnector("localhost", 7000);
    }

    @Bean
    public IntegrationFlow rsocketUpperCaseFlow(ServerRSocketConnector serverRSocketConnector) {
        return IntegrationFlows
                .from(RSockets.inboundGateway("/uppercase")
                        .interactionModels(RSocketInteractionModel.requestChannel)
                        .rsocketConnector(serverRSocketConnector)
                )
                .<Flux<String>, Flux<String>>transform((flux) -> flux.map(String::toUpperCase))
                .get();
    }

}

服务器启动

pom.xml 中的依赖项。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-rsocket</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-rsocket</artifactId>
        </dependency>

应用程序属性

spring.rsocket.server.port=7000
spring.rsocket.server.transport=tcp

应用类。

@SpringBootApplication
@EnableIntegration
public class DemoApplication {

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

    // see PR: https://github.com/spring-projects/spring-boot/pull/18834
    @Bean
    ServerRSocketMessageHandler serverRSocketMessageHandler(RSocketStrategies rSocketStrategies) {
        var handler = new ServerRSocketMessageHandler(true);
        handler.setRSocketStrategies(rSocketStrategies);
        return handler;
    }

    @Bean
    public ServerRSocketConnector serverRSocketConnector(ServerRSocketMessageHandler serverRSocketMessageHandler) {
        return new ServerRSocketConnector(serverRSocketMessageHandler);
    }

    @Bean
    public IntegrationFlow rsocketUpperCaseFlow(ServerRSocketConnector serverRSocketConnector) {
        return IntegrationFlows
                .from(RSockets.inboundGateway("/uppercase")
                        .interactionModels(RSocketInteractionModel.requestChannel)
                        .rsocketConnector(serverRSocketConnector)
                )
                .<Flux<String>, Flux<String>>transform((flux) -> flux.map(String::toUpperCase))
                .get();
    }

}

服务器启动消息映射

pom.xml 中的依赖项。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-rsocket</artifactId>
        </dependency>

应用程序.properties。

spring.rsocket.server.port=7000
spring.rsocket.server.transport=tcp

应用类。

@SpringBootApplication
public class DemoApplication {

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

}

@Controller
class UpperCaseHandler {

    @MessageMapping("/uppercase")
    public Flux<String> uppercase(Flux<String> input) {
        return input.map(String::toUpperCase);
    }
}

客户

在客户端,pom.xml中的依赖关系是这样的。


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-integration</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-rsocket</artifactId>
</dependency>

应用类:


@SpringBootApplication
@EnableIntegration
public class DemoApplication {

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

    @Bean
    public ClientRSocketConnector clientRSocketConnector() {
        ClientRSocketConnector clientRSocketConnector = new ClientRSocketConnector("localhost", 7000);
        clientRSocketConnector.setAutoStartup(false);
        return clientRSocketConnector;
    }

    @Bean
    public IntegrationFlow rsocketUpperCaseRequestFlow(ClientRSocketConnector clientRSocketConnector) {
        return IntegrationFlows
                .from(Function.class)
                .handle(RSockets.outboundGateway("/uppercase")
                        .interactionModel((message) -> RSocketInteractionModel.requestChannel)
                        .expectedResponseType("T(java.lang.String)")
                        .clientRSocketConnector(clientRSocketConnector))
                .get();
    }
}

@RestController
class HelloController {

    @Autowired()
    @Lazy
    @Qualifier("rsocketUpperCaseRequestFlow.gateway")
    private Function<Flux<String>, Flux<String>> rsocketUpperCaseFlowFunction;

    @GetMapping(value = "hello", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> uppercase() {
        return rsocketUpperCaseFlowFunction.apply(Flux.just("a", "b", "c", "d"));
    }
}

在运行客户端和服务器应用程序时,尝试http://localhost:8080/hello通过curl.

当使用使用 InboundGateway 处理消息的serverserver-boot时,输出如下所示。

curl http://localhost:8080/hello

data:ABCD

使用server-boot-messagemapping时,输出正如我所料:

data:A
data:B
data:C
data:D

客户请求者

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-rsocket</artifactId>
        </dependency>

应用类:

@SpringBootApplication
public class DemoApplication {

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

}

@RestController
class HelloController {
    Mono<RSocketRequester> requesterMono;

    public HelloController(RSocketRequester.Builder builder) {
        this.requesterMono = builder.connectTcp("localhost", 7000);
    }

    @GetMapping(value = "hello", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> uppercase() {
        return requesterMono.flatMapMany(
                rSocketRequester -> rSocketRequester.route("/uppercase")
                        .data(Flux.just("a", "b", "c", "d"))
                        .retrieveFlux(String.class)
        );
    }
}

运行此客户端和 3 台服务器时,尝试http://localhost:8080/hello通过curl.

当使用使用 InboundGateway 处理消息的服务器服务器启动时,它会引发类转换异常。

使用server-boot-messagemapping时,输出正如我所料:

data:A
data:B
data:C
data:D

不知道InboundGateway和OutboundGateway的配置问题出在哪里?

4

1 回答 1

1

感谢您提供如此详细的样本!

所以,我所看到的。两个客户端(普通RSocketRequester和 Spring 集成)都可以很好地与普通 RSocket 服务器一起使用。

要使它们与 Spring Integration 服务器一起使用,您必须进行以下更改:

  1. 服务器端:

添加.requestElementType(ResolvableType.forClass(String.class))RSockets.inboundGateway()定义中,因此它将知道将传入的有效负载转换为什么。

  1. 客户端:

    .data(Flux.just("a\n", "b\n", "c\n", "d\n")).

目前 Spring Integration 的服务器端不将传入的数据Flux视为独立有效负载的流。因此,我们尝试将它们全部连接成一个值。新的行分隔符是我们期望独立值的指示符。Spring Messaging 的作用完全相反:它检查预期类型并解码传入的multi-value每个元素,而不是尝试整个解码。Fluxmap()Publisher

这将是一个重大的变化,但可能需要考虑修复逻辑以与RSocket 支持RSocketInboundGateway的常规保持一致。@MessageMapping随意提出一个GH问题!

于 2020-03-02T17:52:10.887 回答