Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebFlux + RSocket + Spring

Can someone tell me or give a ready-made CRUD example using WebFlux, RScoket and Spring (or SpringBoot)?

I studied the RSocket documentation, WebFlux, also wrote my simple examples, but I would like to see a real CRUD application using basic methods using RSocket.

I'll be very grateful. Thanks.

like image 979
Kirill Sereda Avatar asked Feb 25 '20 15:02

Kirill Sereda


People also ask

What is spring RSocket?

Overview. RSocket is an application protocol for multiplexed, duplex communication over TCP, WebSocket, and other byte stream transports, using one of the following interaction models: Request-Response — send one message and receive one back. Request-Stream — send one message and receive a stream of messages back.

Does spring WebFlux use netty?

Spring WebFlux is a part of the Spring framework and provides reactive programming support for web applications. If we're using WebFlux in a Spring Boot application, Spring Boot automatically configures Reactor Netty as the default server.

Is spring WebFlux reactive programming?

Spring WebFlux makes it possible to build reactive applications on the HTTP layer. It is a reactive fully non-blocking, annotation-based web framework built on Project Reactor that supports reactive streams back pressure and runs on non-blocking servers such as Netty, Undertow and Servlet 3.1+ containers.

What is WebFlux in spring?

WebFlux is a Spring reactive-stack web framework. It was added to Spring 5. It is fully non-blocking, supports reactive streams back pressure, and runs on such servers such as Netty, Undertow, and Servlet 3.1+ containers. Spring WebFlux is an alternative to the traditional Spring MVC.


1 Answers

I have maintained a Spring/RSocket sample project with 4 basic interaction modes of RSocket.

If you only require request/reply case for simple CRUD operations, check the request and response mode, and select a transport protocol, TCP or WebSocket.

To implement CRUD operations, just define 4 different routes for them, like define the RESTful APIs using URI, you have to have a good plan for the naming, but in RSocket there are no HTTP methods to help you to differentiate the same routes.

For example, in the server side, we can declare a @Controller to handling messages like this.

@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) {
    }
}

In the client side, if it is a Spring Boot application, you can use RSocket RSocketRequester to interact with the server side like this.

//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()

Of course, if you just build a service exposed by rsocket protocol, the client can be a rsocket-js project or other languages and frameworks, such as Angular, React or Android etc.

Update: I've added a crud sample in my rsocket sample codes, and I have published a post on Medium.

like image 114
Hantsy Avatar answered Oct 17 '22 22:10

Hantsy