TLDR:要配置一个Spring Boot应用程序以显示支持WebSocket传输的RSocket接口(interface),需要做些什么?
我同时学习RSocket和Spring Boot,所以请多多包涵。
在我的奋斗中,我能够构建一个非常简单且人为设计的Spring Boot应用程序实现,该实现使用由第二个Spring Boot应用程序使用RSocket作为协议(protocol)提供/公开的API,但是,我只能在以下情况下实现此目的使用TcpClientTransport
。
从我的角度来看,WebsocketTransport
在客户端->服务器体系结构中更可能被使用并且更有用,但是,我还没有找到任何有效的示例或文档来说明如何正确配置使用WebSocket接受RSocket消息的Spring Boot应用程序。作为交通工具。
奇怪的是,在我的测试中,我的使用者(客户端)似乎确实建立了到服务器/生产者的WebSocket连接,但是,“握手”似乎挂起,并且从未完全建立连接。我已经使用JavaScript库(rsocket-websocket-client,rsocket-rpc-core等)和Java库(io.rsocket.transport.netty.client.WebsocketClientTransport)进行了测试,并且服务器似乎表现出相同的效果行为不分。
重申一下,使用TCPTransport我可以连接到服务器并调用请求,但是使用WebsocketTransport
时,从未建立连接。
旨在通过WebsocketClientTransport
支持RSocket的Spring Boot应用程序有什么要求,而过去将spring-boot-starter-rsocket
作为依赖项使用了呢?
服务器
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.M5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-rsocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
...
application.properties
spring.rsocket.server.port=8081
management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=*
SpringBootRSocketServerApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRSocketServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRSocketServerApplication.class, args);
}
}
UserRSocketController
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
@Slf4j
@Controller
public class UserRSocketController {
@Autowired
private UserRepository userRepository;
@MessageMapping("usersList")
public Mono<List<User>> usersList() {
log.info("Handling usersList request.");
return Mono.just(this.userRepository.getUsers());
}
@MessageMapping("usersStream")
Flux<User> usersStream(UserStreamRequest request) {
log.info("Handling request for usersStream.");
List<User> users = userRepository.getUsers();
Stream<User> userStream = Stream.generate(() -> {
Random rand = new Random();
return users.get(rand.nextInt(users.size()));
});
return Flux.fromStream(userStream).delayElements(Duration.ofSeconds(1));
}
@MessageMapping("userById")
public Mono<User> userById(GetUserByIdRequest request) {
log.info("Handling request for userById id: {}.", request.getId());
return Mono.just(this.userRepository.getUserById(request.getId()));
};
}
启动记录
:: Spring Boot :: (v2.2.0.M5)
2019-09-08 21:40:02,986 INFO [main] org.springframework.boot.StartupInfoLogger: Starting SpringBootRSocketServerApplication on REDACTED with PID 22540 (REDACTED)
2019-09-08 21:40:02,988 INFO [main] org.springframework.boot.SpringApplication: No active profile set, falling back to default profiles: default
2019-09-08 21:40:04,103 INFO [main] org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver: Exposing 14 endpoint(s) beneath base path '/actuator'
2019-09-08 21:40:04,475 INFO [main] org.springframework.boot.rsocket.netty.NettyRSocketServer: Netty RSocket started on port(s): 8081
2019-09-08 21:40:04,494 INFO [main] org.springframework.boot.web.embedded.netty.NettyWebServer: Netty started on port(s): 8080
2019-09-08 21:40:04,498 INFO [main] org.springframework.boot.StartupInfoLogger: Started SpringBootRSocketServerApplication in 1.807 seconds (JVM running for 2.883)
消费者/客户
ClientConfiguration.java
import io.rsocket.RSocket;
import io.rsocket.RSocketFactory;
import io.rsocket.frame.decoder.PayloadDecoder;
import io.rsocket.transport.ClientTransport;
//import io.rsocket.transport.netty.client.TcpClientTransport;
import io.rsocket.transport.netty.client.WebsocketClientTransport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.rsocket.MetadataExtractor;
import org.springframework.messaging.rsocket.RSocketRequester;
import org.springframework.messaging.rsocket.RSocketStrategies;
import org.springframework.util.MimeTypeUtils;
@Configuration
public class ClientConfiguration {
@Bean
public RSocket rSocket() {
// ClientTransport transport = TcpClientTransport.create(8081);
// ^--- TCPTransport works fine
ClientTransport transport = WebsocketClientTransport.create(8081);
// ^--- Connection hangs and application startup stalls
return RSocketFactory
.connect()
.mimeType(MetadataExtractor.ROUTING.toString(), MimeTypeUtils.APPLICATION_JSON_VALUE)
.frameDecoder(PayloadDecoder.ZERO_COPY)
.transport(transport)
.start()
.block();
}
@Bean
RSocketRequester rSocketRequester(RSocketStrategies rSocketStrategies) {
return RSocketRequester.wrap(rSocket(), MimeTypeUtils.APPLICATION_JSON, MimeTypeUtils.APPLICATION_JSON, rSocketStrategies);
}
}
启动记录
:: Spring Boot :: (v2.2.0.M5)
2019-09-08 21:40:52,331 INFO [main] org.springframework.boot.StartupInfoLogger: Starting SpringBootRsocketConsumerApplication on REDACTED with PID 18904 (REDACTED)
2019-09-08 21:40:52,334 INFO [main] org.springframework.boot.SpringApplication: No active profile set, falling back to default profiles: default
最佳答案
您只需要两件事就可以使RSocket应用程序使用websocket传输公开端点:
首先,您需要webflux和rsocket依赖,因为您可能还需要提供网页和静态资源:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-rsocket</artifactId>
</dependency>
然后,您需要在
application.properties
文件中相应地配置RSocket服务器:#server.port=8080 this is already the default
spring.rsocket.server.transport=websocket
spring.rsocket.server.mapping-path=/rsocket
您可以在Spring Boot reference documentation about RSocket中找到有关此内容的更多信息。
Websocket客户端现在可以连接到
ws://localhost:8080/rsocket
。请注意,从当前的2.2.0 SNAPSHOT开始,RSocket协议(protocol)已得到发展,并且rsocket-js库当前正在发展,尤其是在元数据支持方面。您会发现a working sample here as well。
在Java客户端方面,Spring Boot为您提供了一个
RSocketRequester.Builder
,它已经使用编解码器和拦截器针对您的需要进行了配置和自定义:@Component
public class MyService {
private final RSocketRequester rsocketRequester;
public MyService(RSocketRequester.Builder builder) {
this.rsocketRequester = builder
.connectWebSocket(URI.create("ws://localhost:8080/rsocket"))
.block();
}
}