Search in sources :

Example 6 with Address

use of io.scalecube.net.Address 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)

Example 7 with Address

use of io.scalecube.net.Address in project scalecube by scalecube.

the class Example2 method main.

/**
 * Start the example.
 *
 * @param args ignored
 */
public static void main(String[] args) {
    // ScaleCube Node node with no members
    Microservices seed = Microservices.builder().discovery("seed", serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).startAwait();
    // Construct a ScaleCube node which joins the cluster hosting the Greeting Service
    final Address seedAddress = seed.discovery().address();
    Microservices ms = 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(new GreetingServiceImpl()).startAwait();
    // Create a proxy to the seed service node
    ServiceCall service = seed.call();
    // Create a ServiceMessage request with service qualifier and data
    ServiceMessage request = ServiceMessage.builder().qualifier(SERVICE_QUALIFIER).data("joe").build();
    // Execute the Greeting Service to emit a single Greeting response
    Publisher<ServiceMessage> publisher = service.requestOne(request, Greeting.class);
    // Convert the Publisher using the Mono API which ensures it will emit 0 or 1 item.
    Mono.from(publisher).subscribe(consumer -> {
        // handle service response
        Greeting greeting = consumer.data();
        System.out.println(greeting.message());
    });
    Mono.whenDelayError(seed.shutdown(), ms.shutdown()).block();
}
Also used : RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) GreetingServiceImpl(io.scalecube.services.examples.helloworld.service.GreetingServiceImpl) Greeting(io.scalecube.services.examples.helloworld.service.api.Greeting) Microservices(io.scalecube.services.Microservices) ServiceCall(io.scalecube.services.ServiceCall) Publisher(org.reactivestreams.Publisher) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) ServiceMessage(io.scalecube.services.api.ServiceMessage) Mono(reactor.core.publisher.Mono) Address(io.scalecube.net.Address) Greeting(io.scalecube.services.examples.helloworld.service.api.Greeting) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ServiceMessage(io.scalecube.services.api.ServiceMessage) ServiceCall(io.scalecube.services.ServiceCall) Address(io.scalecube.net.Address) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) GreetingServiceImpl(io.scalecube.services.examples.helloworld.service.GreetingServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices)

Example 8 with Address

use of io.scalecube.net.Address in project scalecube by scalecube.

the class CompositeDiscoveryExample method main.

/**
 * Main program.
 *
 * @param args arguments
 */
public static void main(String[] args) {
    Microservices seed1 = Microservices.builder().discovery("seed1", serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint)).options(opts -> opts.memberAlias("seed1"))).transport(RSocketServiceTransport::new).startAwait();
    Microservices seed2 = Microservices.builder().discovery("seed2", serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint)).options(opts -> opts.memberAlias("seed2"))).transport(RSocketServiceTransport::new).startAwait();
    final Address seed1Address = seed1.discovery("seed1").address();
    final Address seed2Address = seed2.discovery("seed2").address();
    Microservices ms1 = Microservices.builder().discovery("ms1", endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).options(opts -> opts.memberAlias("ms1")).membership(cfg -> cfg.seedMembers(seed1Address))).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl1()).startAwait();
    Microservices ms2 = Microservices.builder().discovery("ms2", endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).options(opts -> opts.memberAlias("ms2")).membership(cfg -> cfg.seedMembers(seed2Address))).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl2()).startAwait();
    Microservices compositeMs = Microservices.builder().discovery("domain1", endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).options(opts -> opts.memberAlias("domain1")).membership(cfg -> cfg.seedMembers(seed1Address))).discovery("domain2", endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).options(opts -> opts.memberAlias("domain2")).membership(cfg -> cfg.seedMembers(seed2Address))).transport(RSocketServiceTransport::new).startAwait();
    Greeting greeting1 = compositeMs.call().api(GreetingsService1.class).sayHello("hello one").block();
    System.err.println("This is response from GreetingsService1: " + greeting1.message());
    Greeting greeting2 = compositeMs.call().api(GreetingsService2.class).sayHello("hello two").block();
    System.err.println("This is response from GreetingsService2: " + greeting2.message());
}
Also used : ServiceDiscoveryContext(io.scalecube.services.discovery.api.ServiceDiscoveryContext) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Greeting(io.scalecube.services.examples.helloworld.service.api.Greeting) Microservices(io.scalecube.services.Microservices) AfterConstruct(io.scalecube.services.annotations.AfterConstruct) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Mono(reactor.core.publisher.Mono) ServiceMethod(io.scalecube.services.annotations.ServiceMethod) Service(io.scalecube.services.annotations.Service) Address(io.scalecube.net.Address) Greeting(io.scalecube.services.examples.helloworld.service.api.Greeting) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Address(io.scalecube.net.Address) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices)

Example 9 with Address

use of io.scalecube.net.Address in project scalecube by scalecube.

the class ExceptionMapperExample method main.

/**
 * Example runner.
 *
 * @param args program arguments.
 * @throws InterruptedException exception.
 */
public static void main(String[] args) throws InterruptedException {
    Microservices ms1 = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).defaultErrorMapper(// default mapper for whole node
    new ServiceAProviderErrorMapper()).services(ServiceInfo.fromServiceInstance(new ServiceAImpl()).errorMapper(// mapper per service instance
    new ServiceAProviderErrorMapper()).build()).startAwait();
    System.err.println("ms1 started: " + ms1.serviceAddress());
    final Address address1 = ms1.discovery().address();
    Microservices ms2 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(address1))).transport(RSocketServiceTransport::new).services(call -> {
        ServiceA serviceA = call.errorMapper(// service client error mapper
        new ServiceAClientErrorMapper()).api(ServiceA.class);
        ServiceB serviceB = new ServiceBImpl(serviceA);
        return Collections.singleton(ServiceInfo.fromServiceInstance(serviceB).build());
    }).startAwait();
    System.err.println("ms2 started: " + ms2.serviceAddress());
    ms2.call().api(ServiceB.class).doAnotherStuff(0).subscribe(System.out::println, th -> System.err.println("No service client mapper is defined for ServiceB, " + "so default scalecube mapper is used! -> " + th), () -> System.out.println("Completed!"));
    Thread.currentThread().join();
}
Also used : RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Microservices(io.scalecube.services.Microservices) ServiceInfo(io.scalecube.services.ServiceInfo) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Collections(java.util.Collections) Address(io.scalecube.net.Address) Address(io.scalecube.net.Address) Microservices(io.scalecube.services.Microservices) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport)

Example 10 with Address

use of io.scalecube.net.Address in project scalecube by scalecube.

the class Example3 method main.

/**
 * Start the example.
 *
 * @param args ignored
 */
public static void main(String[] args) {
    // ScaleCube Node with no members
    Microservices seed = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).startAwait();
    final Address seedAddress = seed.discovery().address();
    // Construct a ScaleCube node which joins the cluster hosting the Greeting Service
    Microservices ms = 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(new BidiGreetingImpl()).startAwait();
    // Create service proxy
    BidiGreetingService service = seed.call().api(BidiGreetingService.class);
    // Execute the services and subscribe to service events
    service.greeting(Flux.fromArray(new String[] { "joe", "dan", "roni" })).doOnNext(System.out::println).blockLast();
    Mono.whenDelayError(seed.shutdown(), ms.shutdown()).block();
}
Also used : RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Flux(reactor.core.publisher.Flux) Microservices(io.scalecube.services.Microservices) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Mono(reactor.core.publisher.Mono) Address(io.scalecube.net.Address) BidiGreetingImpl(io.scalecube.services.examples.helloworld.service.BidiGreetingImpl) BidiGreetingService(io.scalecube.services.examples.helloworld.service.api.BidiGreetingService) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Address(io.scalecube.net.Address) BidiGreetingService(io.scalecube.services.examples.helloworld.service.api.BidiGreetingService) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices) BidiGreetingImpl(io.scalecube.services.examples.helloworld.service.BidiGreetingImpl)

Aggregations

Address (io.scalecube.net.Address)21 ScalecubeServiceDiscovery (io.scalecube.services.discovery.ScalecubeServiceDiscovery)17 WebsocketTransportFactory (io.scalecube.transport.netty.websocket.WebsocketTransportFactory)17 Mono (reactor.core.publisher.Mono)17 RSocketServiceTransport (io.scalecube.services.transport.rsocket.RSocketServiceTransport)15 Microservices (io.scalecube.services.Microservices)12 ServiceMessage (io.scalecube.services.api.ServiceMessage)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 ServiceDiscoveryEvent (io.scalecube.services.discovery.api.ServiceDiscoveryEvent)6 List (java.util.List)6 ServiceDiscoveryFactory (io.scalecube.services.discovery.api.ServiceDiscoveryFactory)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 Sinks (reactor.core.publisher.Sinks)5 Duration (java.time.Duration)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Flux (reactor.core.publisher.Flux)4 ServiceCall (io.scalecube.services.ServiceCall)3 ServiceInfo (io.scalecube.services.ServiceInfo)3