我一直维护a Spring/RSocket sample project with 4 basic interaction modes of RSocket。
如果您只需要简单 CRUD 操作的请求/回复案例,请检查 请求和响应 模式,并选择传输协议 TCP 或 WebSocket。
要实现 CRUD 操作,只需为它们定义 4 个不同的路由,比如使用 URI 定义 RESTful API,你必须对命名有一个好的计划,但是在 RSocket 中没有 HTTP 方法可以帮助你区分相同的路线。
例如,在服务器端,我们可以声明一个@Controller 来处理这样的消息。
@Controller
class ProfileController {
@MessageMapping("fetch.profile.{name}")
public Mono<Profile> greet(@DestinationVariable String name) {
}
@MessageMapping("create.profile")
public Mono<Message> greet(@Payload CreateProfileRequest p) {
}
@MessageMapping("update.profile.{name}")
public Mono<Message> greet(@DestinationVariable String name, @Payload UpdateProfileRequest p) {
}
@MessageMapping("delete.profile.{name}")
public Mono<Message> greet(@DestinationVariable String name) {
}
}
在客户端,如果是Spring Boot应用,可以像这样使用RSocketRSocketRequester与服务端交互。
//fetch a profile by name
requester.route("fetch.profile.hantsy").retrieveMono()
//create a new profile
requester.data(new CreateProfileRequest(...)).route("create.profile").retrieveMono()
//update the existing profile
requester.data(new UpdateProfileRequest(...)).route("update.profile.hantsy").retrieveMono()
//delete a profile
requester.route("delete.profile.hantsy").retrieveMono()
当然,如果你只是构建一个rsocket协议暴露的服务,客户端可以是rsocket-js项目或其他语言和框架,如Angular、React或Android等。
更新:我在我的 rsocket 示例代码中添加了一个crud sample,并且我已经发布了a post on Medium。