如何为 spring-cloud-stream-app-starters 项目创建自定义源应用程序

How to create a custom source app for spring-cloud-stream-app-starters project

我想创建一个网络套接字源(用于 spring-cloud-stream-app-starters)目前在 [=23= 上不可用] 枢纽

我浏览了一些可用的资源,但有些困惑,可能是因为我不熟悉该框架。

我可以使用 Source binding 和 return 以 @InboundChannelAdapter(value = Source.OUTPUT) 注释方法从网络套接字客户端接收的数据包创建一个 spring 启动应用程序吗? ? 另外,如何使用 WebSocketInboundChannelAdapter 启动 websocket 服务器并将数据包推送到底层代理。?

@Krishas 我肯定会查看可用的 来源 ,找到与您要完成的内容非常相似的来源,并在它之后为您的新来源建模。 一般来说,你的建议是正确的。 . .它应该是 Spring 带有 @Source 注释的启动应用程序。但当然,细节决定成败。所以我的建议是创建一个 PR,这样我们都可以对其进行审查,并帮助您将其带到我们可以将其包含在可用启动器池中的状态。

您可以在 Reference Manual 中获得一些想法。

WebSocketInboundChannelAdapter 是一个事件驱动的通道适配器,它不是可轮询的源。所以,你需要的只是这个 @Bean 和对 Source.OUTPUT.

的适当引用

WebSocketInboundChannelAdapter 没有启动服务器。这是责任:

/**
 * The {@link IntegrationWebSocketContainer} implementation for the {@code server}
 * {@link org.springframework.web.socket.WebSocketHandler} registration.
 * <p>
 * Registers an internal {@code IntegrationWebSocketContainer.IntegrationWebSocketHandler}
 * for provided {@link #paths} with the {@link WebSocketHandlerRegistry}.
 * <p>
 * The real registration is based on Spring Web-Socket infrastructure via {@link WebSocketConfigurer}
 * implementation of this class.
 *
 * @author Artem Bilan
 * @author Gary Russell
 * @since 4.1
 */
public class ServerWebSocketContainer extends IntegrationWebSocketContainer
        implements WebSocketConfigurer, SmartLifecycle {

我们也对此事有 documentation

还有一个 stomp-chat 示例来演示服务器行为。

我认为在这种 source 应用程序中您不需要 "underlying broker":您只需通过网络套接字接收消息并将它们发布到 Source.OUTPUT。为什么这里需要 STOMP 经纪人?

更新

刚刚针对 Rabbit Binder 测试了此代码:

@SpringBootApplication
@EnableBinding(Source.class)
public class CloudStreamWebSocketSourceApplication {

    @Bean
    public WebSocketInboundChannelAdapter webSocketInboundChannelAdapter() {
        WebSocketInboundChannelAdapter webSocketInboundChannelAdapter =
                new WebSocketInboundChannelAdapter(serverWebSocketContainer());
        webSocketInboundChannelAdapter.setOutputChannelName(Source.OUTPUT);
        return webSocketInboundChannelAdapter;
    }

    @Bean
    public IntegrationWebSocketContainer serverWebSocketContainer() {
        return new ServerWebSocketContainer("/test")
                .withSockJs()
                .setAllowedOrigins("*");
    }

    public static void main(String[] args) throws IOException {
        SpringApplication.run(CloudStreamWebSocketSourceApplication.class, args);
        System.out.println("Done");
    }

}

我的测试用例是这样的:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CloudStreamWebSocketSourceApplicationTests {

    @LocalServerPort
    private String port;

    @Test
    public void testWebSocketStreamSource() throws IOException, InterruptedException {
        StandardWebSocketClient webSocketClient = new StandardWebSocketClient();

        ClientWebSocketContainer clientWebSocketContainer =
                new ClientWebSocketContainer(webSocketClient, "ws://localhost:" + this.port + "/test/websocket");
        clientWebSocketContainer.start();

        WebSocketSession session = clientWebSocketContainer.getSession(null);

        session.sendMessage(new TextMessage("foo"));

        Thread.sleep(10000);
    }

}

这是我的依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-integration</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>