Springowy hello world - reactor.ipc.netty

0

Próbuję napisać springowy hello world, coś w stylu: https://4programmers.net/Mikroblogi/View/21850

public class Server {
    public static void main(String[] args) {
        RouterFunction<?> route = route(GET("/hello"),
                request -> {
                    Mono<String> hi = Mono.just("hi");
                    return ServerResponse.ok().contentType(TEXT_PLAIN).body(hi, String.class);
                });
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
        ReactorHttpHandlerAdapter adapter =
                new ReactorHttpHandlerAdapter(httpHandler);
        HttpServer server = HttpServer.create("localhost", 8080);
        server.startAndAwait(adapter);
    }
}

Jednak nie mam możliwości zaimportowania (ipc jest na czerwono):

import reactor.ipc.netty.http.server.HttpServer;

Próbowałem poszukać w oficjalnej doumentacji, jednak po wklejeniu importu w google i wejściu w pierwszy link dostaje 404 :)
Gdzie może leżeć problem ?

1

A używasz Gradle/Mavena, ewentualnie sam zaciągasz jary, czy spodziewasz się że Intellij ma wszystkie javove biblioteki na świecie? Poza tym wybrałeś sobie wybitnie słaby przykład na Springowy Hello World.

0

używam gradle, a dlaczego to wybitnie słaby przykład ?

1

No to dodałeś dependency do WebFluxa?

Bo to jest nowy framework Springa który oparty jest na architekturze nieblokującej. Pozornie kod i projekt jest prostszy, ale idea i "filozofia" programowania asynchronicznego jest sporo trudniejsza od klasycznej, blokującej. Jak chcesz się nauczyć Springa to radziłbym ci popatrzeć się na ich oficjalne tutoriale lub milion innych artykułów dostępnych na necie.

1
compile group: 'io.projectreactor.ipc', name: 'reactor-netty', version: '0.7.11.RELEASE'
0

Niestety mam ten sam problem w najnowszej wersji webfluxa i powyższy import od @jarekr000000 nie rozwiązuje problemu.

Używam mavena:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
			<version>2.0.6.RELEASE</version>
		</dependency>

Tak działa, jednak jak przejdziemy na ostatni realese webfluxa:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
			<version>2.1.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>io.projectreactor.ipc</groupId>
			<artifactId>reactor-netty</artifactId>
			<version>0.7.11.RELEASE</version>
		</dependency>

to kod startu serwera z pierwszego postu wywali się z błędem

Error:(28, 15) java: no suitable method found for startAndAwait(org.springframework.http.server.reactive.ReactorHttpHandlerAdapter)
    method reactor.ipc.netty.NettyConnector.<T>startAndAwait(T) is not applicable
      (inference variable T has incompatible bounds
        lower bounds: java.util.function.BiFunction<reactor.ipc.netty.http.server.HttpServerRequest,reactor.ipc.netty.http.server.HttpServerResponse,? extends org.reactivestreams.Publisher<java.lang.Void>>
        lower bounds: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter)
    method reactor.ipc.netty.NettyConnector.<T>startAndAwait(T,java.util.function.Consumer<reactor.ipc.netty.tcp.BlockingNettyContext>) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))

Wygląda na to że HttpServerRequest i HttpServerResponse w ReactorHttpHandlerAdapter nie idą z paczki .ipc w nowym realese i stąd błąd kompilacji.

Ma ktoś działający kod startu serwera z pierwszego posta dla wersji 2.1.0.RELEASE?

0

Spring jak i WebFlux ([CIACH!]) to gówna .

0
package com.example.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.ipc.netty.http.server.HttpServer;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;

public class DemoApplication {

	public static void main(String[] args) {
        RouterFunction<ServerResponse> routerFunction = RouterFunctions
                .route(GET("/check"),
                        request -> ServerResponse.ok().body(Mono.just("Hello There"), String.class));
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction);
        ReactorHttpHandlerAdapter adapter =
                new ReactorHttpHandlerAdapter(httpHandler);
        HttpServer.create("localhost", 8080).startAndAwait(adapter);


	}
}

buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
baseName = 'gs-reactive-rest-service'
version = '0.1.0'
}

sourceCompatibility = 1.8

repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}

dependencies {
compile('org.springframework.boot:spring-boot-starter-webflux')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('io.projectreactor:reactor-test')
}


1
package com.example.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.ipc.netty.http.server.HttpServer;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;

public class DemoApplication {

	public static void main(String[] args) {
        RouterFunction<ServerResponse> routerFunction = RouterFunctions
                .route(GET("/check"),
                        request -> ServerResponse.ok().body(Mono.just("Hello There"), String.class));
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction);
        ReactorHttpHandlerAdapter adapter =
                new ReactorHttpHandlerAdapter(httpHandler);
        HttpServer.create("localhost", 8080).startAndAwait(adapter);


	}
}

buildscript {
	ext {
		springBootVersion = '2.0.5.RELEASE'
	}
	repositories {
		mavenCentral()
		maven { url "https://repo.spring.io/snapshot" }
		maven { url "https://repo.spring.io/milestone" }
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
	baseName = 'gs-reactive-rest-service'
	version =  '0.1.0'
}

sourceCompatibility = 1.8

repositories {
	mavenCentral()
	maven { url "https://repo.spring.io/snapshot" }
	maven { url "https://repo.spring.io/milestone" }
}


dependencies {
	compile('org.springframework.boot:spring-boot-starter-webflux')
	testCompile('org.springframework.boot:spring-boot-starter-test')
	testCompile('io.projectreactor:reactor-test')
}


2

@scibi92: ok, ale z tego co widzę to masz Springa w wersji 2.0.5.RELEASE (nie używam gradla więc pewny nie jestem). Dla 2.1.0.RELEASE ten kod działa:


public class Server {
    public static void main(String[] args) {
        RouterFunction<?> route;
        route = route(GET("/hello"),
                request -> {
                    Mono<String> hi = Mono.just("hi");
                    return ServerResponse.ok().contentType(TEXT_PLAIN).body(hi, String.class);
                });
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
        ReactorHttpHandlerAdapter adapter =
                new ReactorHttpHandlerAdapter(httpHandler);
        HttpServer server = HttpServer.create().host("localhost").port(8080);
        DisposableServer disposableServer = server.handle(adapter).bind().block();
        disposableServer.onDispose().block();
  }
}
0

Mi sie wydaje ze zrobiono jakis refactorig w reactor-netty. Porownajcie sobie, nie ma ipc na branchu 8.

https://github.com/reactor/reactor-netty/tree/0.7.x/src

https://github.com/reactor/reactor-netty/tree/0.8.x/src

Ja probowalem wczoraj odpalic przyklad z server.startAndAwait z kursu @jarekr000000 i sromotnie na tym poleglem. (tzn jak uzylem starej konfiguracji w Gradlu wszystko fajnie dziala, jak uzylem Springa 2.2 i gradla wygenerowanego przez: https://start.spring.io/ to przestaje.

Owszem mozna dolozyc w zaleznosciach cos w stylu: "compile group: 'io.projectreactor.ipc', name: 'reactor-netty', version: '0.7.11.RELEASE'" ale i tak nie chce to dzialac.

W sumie nie wiem jak to na razie uruchomic, jesli ktos ma dzialajacy startAndAwait na najnowszej wersji to chetnie zobacze co i jak.

0

@jarekr000000: czyli patrzac na kod ktory podeslales, nie uzywamy juz :

"import reactor.ipc.netty.http.server.HttpServer;"

tylko:

import reactor.netty.http.server.HttpServer

I nieco nam sie zmienia sposob odpalania serwera.

1 użytkowników online, w tym zalogowanych: 0, gości: 1