Search in sources :

Example 1 with CanaryService

use of io.scalecube.services.routings.sut.CanaryService in project scalecube by scalecube.

the class RoutersTest method test_remote_service_tags.

@Test
public void test_remote_service_tags() throws Exception {
    CanaryService service = gateway.call().router(Routers.getRouter(WeightedRandomRouter.class)).api(CanaryService.class);
    Thread.sleep(1000);
    AtomicInteger serviceBCount = new AtomicInteger(0);
    int n = (int) 1e3;
    Flux.range(0, n).flatMap(i -> service.greeting(new GreetingRequest("joe"))).filter(response -> response.getResult().contains("SERVICE_B_TALKING")).doOnNext(response -> serviceBCount.incrementAndGet()).blockLast(Duration.ofSeconds(3));
    System.out.println("Service B was called: " + serviceBCount.get() + " times out of " + n);
    assertTrue((serviceBCount.doubleValue() / n) > 0.5, "Service B's Weight=0.9; more than half of invocations have to be routed to Service B");
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GreetingRequest(io.scalecube.services.sut.GreetingRequest) CanaryService(io.scalecube.services.routings.sut.CanaryService) Test(org.junit.jupiter.api.Test) BaseTest(io.scalecube.services.BaseTest)

Example 2 with CanaryService

use of io.scalecube.services.routings.sut.CanaryService 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

Address (io.scalecube.net.Address)2 Microservices (io.scalecube.services.Microservices)2 ServiceInfo (io.scalecube.services.ServiceInfo)2 ScalecubeServiceDiscovery (io.scalecube.services.discovery.ScalecubeServiceDiscovery)2 CanaryService (io.scalecube.services.routings.sut.CanaryService)2 GreetingServiceImplA (io.scalecube.services.routings.sut.GreetingServiceImplA)2 GreetingServiceImplB (io.scalecube.services.routings.sut.GreetingServiceImplB)2 WeightedRandomRouter (io.scalecube.services.routings.sut.WeightedRandomRouter)2 GreetingRequest (io.scalecube.services.sut.GreetingRequest)2 RSocketServiceTransport (io.scalecube.services.transport.rsocket.RSocketServiceTransport)2 WebsocketTransportFactory (io.scalecube.transport.netty.websocket.WebsocketTransportFactory)2 Mono (reactor.core.publisher.Mono)2 BaseTest (io.scalecube.services.BaseTest)1 Reflect (io.scalecube.services.Reflect)1 ServiceCall (io.scalecube.services.ServiceCall)1 ServiceReference (io.scalecube.services.ServiceReference)1 GREETING_REQUEST_REQ (io.scalecube.services.TestRequests.GREETING_REQUEST_REQ)1 GREETING_REQUEST_REQ2 (io.scalecube.services.TestRequests.GREETING_REQUEST_REQ2)1 ServiceMessage (io.scalecube.services.api.ServiceMessage)1 RandomServiceRouter (io.scalecube.services.routing.RandomServiceRouter)1