Search in sources :

Example 1 with RSocketServiceTransport

use of io.scalecube.services.transport.rsocket.RSocketServiceTransport in project scalecube by scalecube.

the class ServiceRegistryTest method test_listen_to_discovery_events.

@ParameterizedTest
@MethodSource("metadataCodecSource")
public void test_listen_to_discovery_events(MetadataCodec metadataCodec) {
    Sinks.Many<ServiceDiscoveryEvent> processor = Sinks.many().replay().all();
    List<Microservices> cluster = new CopyOnWriteArrayList<>();
    Microservices seed = Microservices.builder().discovery(defServiceDiscovery(metadataCodec)).transport(RSocketServiceTransport::new).services(new AnnotationServiceImpl()).startAwait();
    cluster.add(seed);
    seed.listenDiscovery().subscribe(processor::tryEmitNext, processor::tryEmitError, processor::tryEmitComplete);
    Address seedAddress = seed.discovery().address();
    StepVerifier.create(processor.asFlux().onBackpressureBuffer()).then(() -> {
        Microservices ms1 = Microservices.builder().discovery(defServiceDiscovery(seedAddress, metadataCodec)).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
        cluster.add(ms1);
    }).assertNext(event -> assertEquals(ENDPOINT_ADDED, event.type())).then(() -> {
        Microservices ms2 = Microservices.builder().discovery(defServiceDiscovery(seedAddress, metadataCodec)).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
        cluster.add(ms2);
    }).assertNext(event -> assertEquals(ENDPOINT_ADDED, event.type())).then(() -> cluster.remove(2).shutdown().block(TIMEOUT)).assertNext(event -> assertEquals(ENDPOINT_LEAVING, event.type())).assertNext(event -> assertEquals(ENDPOINT_REMOVED, event.type())).then(() -> cluster.remove(1).shutdown().block(TIMEOUT)).assertNext(event -> assertEquals(ENDPOINT_LEAVING, event.type())).assertNext(event -> assertEquals(ENDPOINT_REMOVED, event.type())).thenCancel().verify(TIMEOUT);
    StepVerifier.create(seed.call().api(AnnotationService.class).serviceDiscoveryEventTypes()).assertNext(type -> assertEquals(ENDPOINT_ADDED, type)).assertNext(type -> assertEquals(ENDPOINT_ADDED, type)).assertNext(type -> assertEquals(ENDPOINT_LEAVING, type)).assertNext(type -> assertEquals(ENDPOINT_REMOVED, type)).assertNext(type -> assertEquals(ENDPOINT_LEAVING, type)).assertNext(type -> assertEquals(ENDPOINT_REMOVED, type)).thenCancel().verify(TIMEOUT);
    Mono.whenDelayError(cluster.stream().map(Microservices::shutdown).toArray(Mono[]::new)).block(TIMEOUT);
}
Also used : Sinks(reactor.core.publisher.Sinks) ENDPOINT_LEAVING(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_LEAVING) StepVerifier(reactor.test.StepVerifier) MetadataCodec(io.scalecube.cluster.metadata.MetadataCodec) JdkMetadataCodec(io.scalecube.cluster.metadata.JdkMetadataCodec) ENDPOINT_ADDED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_ADDED) Duration(java.time.Duration) ServiceDiscoveryEvent(io.scalecube.services.discovery.api.ServiceDiscoveryEvent) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Address(io.scalecube.net.Address) MethodSource(org.junit.jupiter.params.provider.MethodSource) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ENDPOINT_REMOVED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED) JacksonMetadataCodec(io.scalecube.cluster.codec.jackson.JacksonMetadataCodec) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) AnnotationService(io.scalecube.services.sut.AnnotationService) AnnotationServiceImpl(io.scalecube.services.sut.AnnotationServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ServiceDiscoveryFactory(io.scalecube.services.discovery.api.ServiceDiscoveryFactory) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Sinks(reactor.core.publisher.Sinks) Address(io.scalecube.net.Address) ServiceDiscoveryEvent(io.scalecube.services.discovery.api.ServiceDiscoveryEvent) AnnotationServiceImpl(io.scalecube.services.sut.AnnotationServiceImpl) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with RSocketServiceTransport

use of io.scalecube.services.transport.rsocket.RSocketServiceTransport in project scalecube by scalecube.

the class RoutersTest method setup.

@BeforeAll
public static void setup() {
    gateway = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).startAwait();
    gatewayAddress = gateway.discovery().address();
    // Create microservices instance cluster.
    provider1 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(gatewayAddress))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(new GreetingServiceImpl(1)).tag("ONLYFOR", "joe").tag("SENDER", "1").build(), ServiceInfo.fromServiceInstance(new GreetingServiceImplA()).tag("Weight", "0.1").build()).startAwait();
    // Create microservices instance cluster.
    provider2 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(gatewayAddress))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(new GreetingServiceImpl(2)).tag("ONLYFOR", "fransin").tag("SENDER", "2").build(), ServiceInfo.fromServiceInstance(new GreetingServiceImplB()).tag("Weight", "0.9").build()).startAwait();
    TagService tagService = input -> input.map(String::toUpperCase);
    provider3 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(gatewayAddress))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(tagService).tag("tagB", "bb").tag("tagC", "c").build()).startAwait();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) GreetingServiceImplB(io.scalecube.services.routings.sut.GreetingServiceImplB) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) GreetingServiceImplA(io.scalecube.services.routings.sut.GreetingServiceImplA) DummyRouter(io.scalecube.services.routings.sut.DummyRouter) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) GreetingResponse(io.scalecube.services.sut.GreetingResponse) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) RandomServiceRouter(io.scalecube.services.routing.RandomServiceRouter) GREETING_REQUEST_REQ2(io.scalecube.services.TestRequests.GREETING_REQUEST_REQ2) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) ServiceCall(io.scalecube.services.ServiceCall) Duration(java.time.Duration) Map(java.util.Map) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) GreetingRequest(io.scalecube.services.sut.GreetingRequest) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Address(io.scalecube.net.Address) ServiceReference(io.scalecube.services.ServiceReference) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) CanaryService(io.scalecube.services.routings.sut.CanaryService) ServiceInfo(io.scalecube.services.ServiceInfo) Mono(reactor.core.publisher.Mono) TagService(io.scalecube.services.routings.sut.TagService) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Routers(io.scalecube.services.routing.Routers) Flux(reactor.core.publisher.Flux) Microservices(io.scalecube.services.Microservices) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Reflect(io.scalecube.services.Reflect) GREETING_REQUEST_REQ(io.scalecube.services.TestRequests.GREETING_REQUEST_REQ) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) Optional(java.util.Optional) ServiceMessage(io.scalecube.services.api.ServiceMessage) BaseTest(io.scalecube.services.BaseTest) WeightedRandomRouter(io.scalecube.services.routings.sut.WeightedRandomRouter) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) TagService(io.scalecube.services.routings.sut.TagService) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) GreetingServiceImplA(io.scalecube.services.routings.sut.GreetingServiceImplA) GreetingServiceImplB(io.scalecube.services.routings.sut.GreetingServiceImplB) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 3 with RSocketServiceTransport

use of io.scalecube.services.transport.rsocket.RSocketServiceTransport in project scalecube by scalecube.

the class ServiceRemoteTest method test_remote_serviceA_calls_serviceB_with_dispatcher.

@Test
public void test_remote_serviceA_calls_serviceB_with_dispatcher() {
    // getting proxy from any node at any given time.
    CoarseGrainedServiceImpl another = new CoarseGrainedServiceImpl();
    // Create microservices instance cluster.
    Microservices provider = Microservices.builder().discovery(ServiceRemoteTest::serviceDiscovery).transport(RSocketServiceTransport::new).services(// add service a and b
    another).startAwait();
    // Get a proxy to the service api.
    CoarseGrainedService service = gateway.call().api(CoarseGrainedService.class);
    String response = service.callGreetingWithDispatcher("joe").block(Duration.ofSeconds(5));
    assertEquals(response, " hello to: joe");
    provider.shutdown().then(Mono.delay(TIMEOUT2)).block();
}
Also used : CoarseGrainedServiceImpl(io.scalecube.services.sut.CoarseGrainedServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) CoarseGrainedService(io.scalecube.services.sut.CoarseGrainedService) Test(org.junit.jupiter.api.Test)

Example 4 with RSocketServiceTransport

use of io.scalecube.services.transport.rsocket.RSocketServiceTransport in project scalecube by scalecube.

the class ServiceCallRemoteTest method test_custom_error_mapper.

@Test
public void test_custom_error_mapper() {
    GreetingService service = new ServiceCall().errorMapper(message -> {
        throw new RuntimeException("custom error mapper");
    }).transport(new RSocketServiceTransport().start().block().clientTransport()).router(ServiceCallRemoteTest::route).api(GreetingService.class);
    StepVerifier.create(service.exceptionRequest(new GreetingRequest())).expectErrorSatisfies(throwable -> {
        Assertions.assertEquals(RuntimeException.class, throwable.getClass());
        Assertions.assertEquals("custom error mapper", throwable.getMessage());
    }).verify(TIMEOUT);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) GREETING_REQ(io.scalecube.services.TestRequests.GREETING_REQ) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) GREETING_THROWING_VOID_REQ(io.scalecube.services.TestRequests.GREETING_THROWING_VOID_REQ) StepVerifier(reactor.test.StepVerifier) GreetingResponse(io.scalecube.services.sut.GreetingResponse) Disabled(org.junit.jupiter.api.Disabled) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) GREETING_FAILING_VOID_REQ(io.scalecube.services.TestRequests.GREETING_FAILING_VOID_REQ) Duration(java.time.Duration) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) GreetingRequest(io.scalecube.services.sut.GreetingRequest) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) GREETING_REQUEST_TIMEOUT_REQ(io.scalecube.services.TestRequests.GREETING_REQUEST_TIMEOUT_REQ) GREETING_VOID_REQ(io.scalecube.services.TestRequests.GREETING_VOID_REQ) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) GREETING_NO_PARAMS_REQUEST(io.scalecube.services.TestRequests.GREETING_NO_PARAMS_REQUEST) ServiceException(io.scalecube.services.exceptions.ServiceException) EmptyGreetingResponse(io.scalecube.services.sut.EmptyGreetingResponse) GREETING_FAIL_REQ(io.scalecube.services.TestRequests.GREETING_FAIL_REQ) GREETING_EMPTY_REQUEST_RESPONSE(io.scalecube.services.TestRequests.GREETING_EMPTY_REQUEST_RESPONSE) ServiceRegistry(io.scalecube.services.registry.api.ServiceRegistry) Publisher(org.reactivestreams.Publisher) GREETING_ERROR_REQ(io.scalecube.services.TestRequests.GREETING_ERROR_REQ) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) SimpleQuoteService(io.scalecube.services.sut.SimpleQuoteService) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) GreetingService(io.scalecube.services.sut.GreetingService) QuoteService(io.scalecube.services.sut.QuoteService) Assertions(org.junit.jupiter.api.Assertions) GREETING_REQUEST_REQ(io.scalecube.services.TestRequests.GREETING_REQUEST_REQ) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) Optional(java.util.Optional) ServiceMessage(io.scalecube.services.api.ServiceMessage) Collections(java.util.Collections) GreetingRequest(io.scalecube.services.sut.GreetingRequest) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) GreetingService(io.scalecube.services.sut.GreetingService) Test(org.junit.jupiter.api.Test)

Example 5 with RSocketServiceTransport

use of io.scalecube.services.transport.rsocket.RSocketServiceTransport in project scalecube by scalecube.

the class ServiceTagsExample method main.

/**
 * Main runner.
 *
 * @param args arguments
 */
public static void main(String[] args) {
    Microservices gateway = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).startAwait();
    Address seedAddress = gateway.discovery().address();
    Microservices services1 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(seedAddress))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(new GreetingServiceImplA()).tag("Weight", "0.3").build()).startAwait();
    Microservices services2 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(seedAddress))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(new GreetingServiceImplB()).tag("Weight", "0.7").build()).startAwait();
    CanaryService service = gateway.call().router(WeightedRandomRouter.class).api(CanaryService.class);
    for (int i = 0; i < 10; i++) {
        Mono.from(service.greeting(new GreetingRequest("joe"))).doOnNext(success -> {
            success.getResult().startsWith("B");
            System.out.println(success);
        });
    }
}
Also used : GreetingServiceImplB(io.scalecube.services.routings.sut.GreetingServiceImplB) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) CanaryService(io.scalecube.services.routings.sut.CanaryService) GreetingServiceImplA(io.scalecube.services.routings.sut.GreetingServiceImplA) Microservices(io.scalecube.services.Microservices) ServiceInfo(io.scalecube.services.ServiceInfo) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) GreetingRequest(io.scalecube.services.sut.GreetingRequest) Mono(reactor.core.publisher.Mono) Address(io.scalecube.net.Address) WeightedRandomRouter(io.scalecube.services.routings.sut.WeightedRandomRouter) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Address(io.scalecube.net.Address) WeightedRandomRouter(io.scalecube.services.routings.sut.WeightedRandomRouter) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) GreetingRequest(io.scalecube.services.sut.GreetingRequest) CanaryService(io.scalecube.services.routings.sut.CanaryService) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) GreetingServiceImplA(io.scalecube.services.routings.sut.GreetingServiceImplA) Microservices(io.scalecube.services.Microservices) GreetingServiceImplB(io.scalecube.services.routings.sut.GreetingServiceImplB)

Aggregations

RSocketServiceTransport (io.scalecube.services.transport.rsocket.RSocketServiceTransport)26 ScalecubeServiceDiscovery (io.scalecube.services.discovery.ScalecubeServiceDiscovery)22 WebsocketTransportFactory (io.scalecube.transport.netty.websocket.WebsocketTransportFactory)22 Mono (reactor.core.publisher.Mono)18 Address (io.scalecube.net.Address)16 Microservices (io.scalecube.services.Microservices)13 Duration (java.time.Duration)12 Test (org.junit.jupiter.api.Test)11 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)9 ServiceMessage (io.scalecube.services.api.ServiceMessage)8 GreetingServiceImpl (io.scalecube.services.sut.GreetingServiceImpl)8 StepVerifier (reactor.test.StepVerifier)8 BeforeAll (org.junit.jupiter.api.BeforeAll)7 AfterAll (org.junit.jupiter.api.AfterAll)6 GreetingResponse (io.scalecube.services.sut.GreetingResponse)5 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 Publisher (org.reactivestreams.Publisher)5 ServiceInfo (io.scalecube.services.ServiceInfo)4 Authenticator (io.scalecube.services.auth.Authenticator)4