본문 바로가기

Back/Java

[Spring/WebSocket] RSocket 예제코드 실행해보기(WIP)

오늘의 주제

Spring에 있는 RSocket

 

 

 

배경

채팅서버를 만들기 위해 Websocket에 대해 공부를 하던중..

전달받은 블로그 포스팅!

전통적인? 방식의 websocket + redis pub/sub과 webflux(rsocket) + redis pub/sub를 비교할수 있을거라 하셨는데..

Websocket도 모르겠는 마당에 RSocket은 무엇인가ㅠㅠㅠㅠㅠㅠㅠㅠㅠ

↓↓↓↓↓

blop post https://dev.to/olibroughton/building-a-scalable-live-stream-chat-service-with-spring-webflux-redis-pubsub-rsocket-and-auth0-22o9

github https://github.com/oli-broughton/livestream-chat

 

 

 

테스트

목표1. 서버 실행하기

예제코드로 서버를 띄웠는데 에러가 한가득..

 

 

실행1. 일단 Oauth 랑 Jwt를 떼어내자!

application.properties에 있던 Oauth 관련 property와

RSocketSecurityConfig.java 클래스를 전체 주석처리함.

 

기존 코드중에 PubSubController 클래스에

jwt 토큰을 받아서 username을 뽑아서 사용하는 로직이 있어서 수정함

@MessageMapping("publish")
Mono<Void> publish(String message) {
    Mono<String> mono = Mono.just(message);
    return mono.flatMap((m) -> messagingService.publish(new Message("test", m)));
}

 

 

 

실행2. Redis 설치 및 property 추가

여전히 남아있는 에러메시지...

redis localhost:6375가 연결할수 없다고 함

정확한 에러메시지는 기억안남 헤헷

 

설치한 Redis를 실행시키고 서버를 돌려봤지만 여전히 같은 에러!

Redis 포트 확인해보니 6379.

설치할때 포트가 6379로 설치되어 버림..

# redis
spring.profiles.active= local
spring.cache.type= redis
spring.redis.host= localhost
spring.redis.port= 6379

application.properties에 요로케 추가

Redis가 켜져있는지 꼭 확인할 것!!!!

 

 

목표2. 웹소켓 연결해보기

Chome 웹스토어에서 테스트 할수 있는 확장 프로그램을 설치함.

나는 `WebSocket Test Client` 라는 걸루 설치함. 근데 별로 좋지는 않은것 같음...

 

연결은 되는데 request를 한번 send하면 연결이 끊겨버림!!!!!!!!!!!!!!!!!!!!!!

왜지?????ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ

 

 

실행1. 소켓연결되는 부분과 메시지 받는 부분 찾아보기

소켓연결과 메시지 받는 곳으로 의심되는 PubSubController 클래스쪽에 break point를 걸어봤는데, 

break point가 안걸린다!!!

왜지이이이이?????????????????????????????????????????????????????????????

 

 

 

실행2. 소켓연결url 맵핑하는 곳 찾아보기

property 파일에 path가 설정되긴 했는데, 이 path를 가져다 쓰는곳은 어딜까?

예제코드로 작성된 소스코드중에는 없다.

그래서 dependency된 아이들중에서 찾아냄!

RSocketServerAutoConfiguration.java 안에 있었음

 

@ConditionalOnProperty 라는 애너테이션으로도 사용이 property를 가능하구나~ 하나배움ㅋㅋ

static class OnRSocketWebServerCondition extends AllNestedConditions {

   OnRSocketWebServerCondition() {
      super(ConfigurationPhase.PARSE_CONFIGURATION);
   }

   @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
   static class IsReactiveWebApplication {

   }

   @ConditionalOnProperty(prefix = "spring.rsocket.server", name = "port", matchIfMissing = true)
   static class HasNoPortConfigured {

   }

   @ConditionalOnProperty(prefix = "spring.rsocket.server", name = "mapping-path")
   static class HasMappingPathConfigured {

   }

   @ConditionalOnProperty(prefix = "spring.rsocket.server", name = "transport", havingValue = "websocket")
   static class HasWebsocketTransportConfigured {

   }

}

 

실제로 break point 걸리는 곳은 요기

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ RSocketServer.class, RSocketStrategies.class, HttpServer.class, TcpServerTransport.class })
@ConditionalOnBean(RSocketMessageHandler.class)
@AutoConfigureAfter(RSocketStrategiesAutoConfiguration.class)
@EnableConfigurationProperties(RSocketProperties.class)
public class RSocketServerAutoConfiguration {

   @Conditional(OnRSocketWebServerCondition.class)
   @Configuration(proxyBeanMethods = false)
   static class WebFluxServerConfiguration {

      @Bean
      @ConditionalOnMissingBean
      RSocketWebSocketNettyRouteProvider rSocketWebsocketRouteProvider(RSocketProperties properties,
            RSocketMessageHandler messageHandler, ObjectProvider<RSocketServerCustomizer> customizers) {
         return new RSocketWebSocketNettyRouteProvider(properties.getServer().getMappingPath(),
               messageHandler.responder(), customizers.orderedStream());
      }

   }
   
   .....
   ...
   ..
   }

 

@EnableConfigurationProperties(RSocketProperties.class)

이 애너테이션으로 RSocketProperties 라는 클래스에 property 값들이 할당되는거고,

properties.getServer().getMappingPath()

이렇게 사용한다.

 

 

 

 

 

오늘은 여기까지... 삽질 많이해서 피곤하다ㅠㅠㅠㅠㅠㅠㅠ