基于Java对STOMP服务端进行测试

一、目标

上一节我们说了构建SpringBoot + WebSocket+STOMP指定推送消息。我们这一节对它进行测试。

我们预期的并发目标:

  • 支持10000+ 用户
  • 每个用户同时发布 500条数据

我们准备的环境:

  • -Xms512m
  • -Xmx4096m
  • CPU 12核 20线程
  • 内存 16G

二、服务端改动

之前的服务端我们是这样设置的 config.enableSimpleBroker(),这种设置属于入门级使用,它非常简单但仅支持 STOMP 命令的子集(无确认、收据等)。

但我们测试时要求确认订阅状态,来确定订阅是否成功。所以必须对之前的配置类进行改动,来满足要求。

客户端订阅确认配置:

 @Bean
    public ApplicationListener<SessionSubscribeEvent> webSocketEventListener(
            final AbstractSubscribableChannel clientOutboundChannel) {
   
        return event -> {
   
            Message<byte[]> message = event.getMessage();
            StompHeaderAccessor stompHeaderAccessor = StompHeaderAccessor.wrap(message);
            if (stompHeaderAccessor.getReceipt() != null) {
   
                stompHeaderAccessor.setHeader("stompCommand", StompCommand.RECEIPT);
                stompHeaderAccessor.setReceiptId(stompHeaderAccessor.getReceipt());
                clientOutboundChannel.send(
                        MessageBuilder.createMessage(new byte[0], stompHeaderAccessor.getMessageHeaders()));
            }
        };
    }

三、测试程序

3.1 依赖

  <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>websocket-client</artifactId>
            <version>9.4.48.v20220622</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>websocket-server</artifactId>
			<version>9.4.48.v20220622</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
			<version>9.4.48.v20220622</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-client</artifactId>
			<version>9.4.48.v20220622</version>
            <scope>test</scope>
        </dependency>

3.1 用户量和消息数

	//用户量
	private static final int NUMBER_OF_USERS = 10000;

	//消息数
	private static final int BROADCAST_MESSAGE_COUNT = 500;

3.2 测试端口是否可用

		String host = "localhost";
		if (args.length > 0) {
   
			host = args[0];
		}

		int port = 6060;
		if (args.length > 1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用以下步骤测试 Spring Stomp: 1. 启动 Spring Boot 应用程序,并确保已启动 STOMP 消息代理。 2. 使用 WebSocket 客户端连接到 STOMP 代理。您可以使用浏览器中的 WebSocket 客户端插件,如 Chrome 的 Simple WebSocket Client 或 Firefox 的 Simple WebSocket Client。或者,您可以使用一个命令行工具,如 wscat。 3. 在连接成功后,使用 STOMP 协议发送消息。例如,您可以使用 STOMP SEND 命令将消息发送到目标主题。 4. 您可以使用 STOMP SUBSCRIBE 命令订阅目标主题,并接收来自 STOMP 代理的消息。 5. 验证接收到的消息是否与您预期的消息匹配。 这是一个简单的示例: ``` // 客户端代码 var socket = new SockJS('/websocket'); var stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { console.log('Connected: ' + frame); stompClient.subscribe('/topic/messages', function(message) { console.log('Received: ' + message); }); }); stompClient.send("/app/sendMessage", {}, JSON.stringify({'message': "Hello, world!"})); // 服务器端代码 @MessageMapping("/sendMessage") @SendTo("/topic/messages") public String sendMessage(String message) { return "Received message: " + message; } ``` 在此示例中,客户端连接到 WebSocket 端点 /websocket,并订阅了主题 /topic/messages。然后,客户端使用 STOMP SEND 命令发送消息到主题 /app/sendMessage。服务器端使用 STOMP MESSAGE 命令接收消息,并将其发送回客户端。最后,客户端将接收到来自服务器的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timi先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值