侦探不适用于websocket客户端

时间:2018-08-08 14:54:59

标签: spring-cloud-sleuth

我有一个微服务,该微服务正在连接到WebSocket服务器,并从服务器接收消息。我想将Sleuth集成到此应用程序中,因为这是我的一组微服务的网关。但是,当我连接到WebSocket服务器并开始接收消息时,我无法在日志中的日志中找到traceId,spanid。我期待着这样的事情:

function shownavlist(allnavitems){
  const items = allnavitems.map(h => (
    [

      <a key={1} href="#" className="dropdown-toggle navbar-link" data-toggle="dropdown">
        {h.heading}
        <span className="caret"></span>
      </a>

      <ul key={2} className="dropdown-menu {{getTheme slug}}" role="menu">
        {h.links.map(display => (
          <li key={display.order}>
            <a className="navbar-link notAllowed" href="">{display.heading}</a>
          </li>
        ))}
      </ul>

    ]
  ))

  return items;

}

但是我得到了:

2018-08-09 11:26:20.222  INFO [kumarman-betsync-adapter-service-v2,0a011711c2470359,0a011711c2470359,true] 7230 --- [ient-SecureIO-2] c.b.b.a.service.BetSyncEventIdExtractor  : SubscribeResponse feed received for eventId=7519016
2018-08-09 11:26:20.224  INFO [kumarman-betsync-adapter-service-v2,0a011711c2470359,2b028dadad5c440d,true] 7230 --- [ient-SecureIO-2] c.b.b.a.k.message.KafkaMessageSender     : sent payload to topic='betsyncEventTopic'

我的代码如下:

2018-08-09 11:26:20.222  INFO [kumarman-betsync-adapter-service-v2,,,] 7230 --- [ient-SecureIO-2] c.b.b.a.service.BetSyncEventIdExtractor  : SubscribeResponse feed received for eventId=7519016
2018-08-09 11:26:20.224  INFO [kumarman-betsync-adapter-service-v2,,,] 7230 --- [ient-SecureIO-2] c.b.b.a.k.message.KafkaMessageSender     : sent payload to topic='betsyncEventTopic'

我们的pom条目是:

  @Service
    @Slf4j
    @AllArgsConstructor
    public class BetsyncWebsocketAdapter {

        private final BetsyncWebSocketHandler betsyncWebSocketHandler;

        public void startNewConnection() {
                try {
                    webSocketSession = new StandardWebSocketClient().doHandshake(
                            betsyncWebSocketHandler,
                            new WebSocketHttpHeaders(),
                            new URI(getBetsyncConnectionUrl()))
                            .get(KafkaAdminClient.MAX_TIME_LIMIT_TO_CONNECT, TimeUnit.SECONDS);
                } catch (Exception e) {
                    log.error("exception occurred", e);
                }
        }

        /**
         * This will start the new connection after server start
         */
        @EventListener(ApplicationReadyEvent.class)
        public void onServerStart() {
            startNewConnection();
        }
    }

@Slf4j
@Service
@AllArgsConstructor
public class BetsyncWebSocketHandler extends TextWebSocketHandler {

    private static StringBuilder betSyncMessage = new StringBuilder();

    private final LoginRequestBuilder loginRequestBuilder;
    private final BetsyncMessageProcessing betsyncMessageProcessing;

    @Override
    public void afterConnectionEstablished(final WebSocketSession session) throws IOException {
        session.sendMessage(new TextMessage(loginRequestBuilder.getLoginRequestDto()));
    }

    @Override
    protected void handleTextMessage(final WebSocketSession session, final TextMessage message) {
        betSyncMessage.append(message.getPayload());
        if (message.isLast()) {
            betsyncMessageProcessing.process(betSyncMessage.toString());
            betSyncMessage = new StringBuilder();
        } else {
            log.debug("we have received a partial message starting with {}", getFirstXCharacter(message.getPayload(), 50));
        }
    }

    @Override
    protected void handlePongMessage(final WebSocketSession session, final PongMessage message) {
        log.info("Response for Ping message received from the server");
    }

    @Override
    public boolean supportsPartialMessages() {
        return true;
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        log.error("server connection closed");
    }
}

1 个答案:

答案 0 :(得分:0)

您要自己创建套接字处理

 webSocketSession = new StandardWebSocketClient().doHandshake(
                            betsyncWebSocketHandler,
                            new WebSocketHttpHeaders(),
                            new URI(getBetsyncConnectionUrl()))
                            .get(KafkaAdminClient.MAX_TIME_LIMIT_TO_CONNECT, TimeUnit.SECONDS);

当您直接使用Spring Integration组件时,Spring Cloud Sleuth支持WebSockets。由于您是手动操作,因此必须确保跟踪上下文得到传播,并且您可以自己读取它。