繁体   English   中英

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

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

我想创建一个Web套接字源(用于spring-cloud-stream-app-starters ),该源目前在git hub上不可用。

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

我是否可以使用Source binding创建一个Spring Boot应用程序,并以@InboundChannelAdapter(value = Source.OUTPUT)批注的方法返回从Web套接字客户@InboundChannelAdapter(value = Source.OUTPUT)收到的数据包。 另外,如何使用WebSocketInboundChannelAdapter启动Websocket服务器并将数据包推送到基础代理。

@Krishas我一定会看一下可用的资源 ,找到与您要完成的工作非常相似的资源 ,并在此之后对新的资源进行建模。 通常,您的建议是正确的。 它应该是带有@Source注释的Spring boot应用程序。 但是,当然,细节在于魔鬼。 因此,我建议创建一个PR,以便我们所有人都可以对其进行审查,并帮助您将其带入可以将其包含在可用启动器库中的状态。

您可以在参考手册中获得一些想法。

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 {

我们也有关于此事的文档

还有一个单脚聊天示例来演示服务器行为。

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

UPDATE

刚刚针对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>

暂无
暂无

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

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