Search in sources :

Example 11 with ScalecubeServiceDiscovery

use of io.scalecube.services.discovery.ScalecubeServiceDiscovery in project scalecube by scalecube.

the class PrincipalMapperAuthExample method main.

/**
 * Main program.
 *
 * @param args arguments
 */
public static void main(String[] args) {
    Microservices service = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(() -> new RSocketServiceTransport().authenticator(authenticator())).services(ServiceInfo.fromServiceInstance(new SecuredServiceByApiKeyImpl()).principalMapper(PrincipalMapperAuthExample::apiKeyPrincipalMapper).build()).services(ServiceInfo.fromServiceInstance(new SecuredServiceByUserProfileImpl()).principalMapper(PrincipalMapperAuthExample::userProfilePrincipalMapper).build()).startAwait();
    Microservices userProfileCaller = Microservices.builder().discovery(endpoint -> discovery(service, endpoint)).transport(() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())).startAwait();
    String responseByUserProfile = userProfileCaller.call().api(SecuredServiceByUserProfile.class).hello(UUID.randomUUID().toString()).block(Duration.ofSeconds(3));
    System.err.println("### Received 'userProfileCaller' response: " + responseByUserProfile);
    Microservices apiKeyCaller = Microservices.builder().discovery(endpoint -> discovery(service, endpoint)).transport(() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())).startAwait();
    String responseByApiKey = apiKeyCaller.call().api(SecuredServiceByApiKey.class).hello(UUID.randomUUID().toString()).block(Duration.ofSeconds(3));
    System.err.println("### Received 'apiKeyCaller' response: " + responseByApiKey);
}
Also used : ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ServiceInfo(io.scalecube.services.ServiceInfo) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) Authenticator(io.scalecube.services.auth.Authenticator) ServiceEndpoint(io.scalecube.services.ServiceEndpoint) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices) CredentialsSupplier(io.scalecube.services.auth.CredentialsSupplier) Duration(java.time.Duration) Map(java.util.Map) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) UnauthorizedException(io.scalecube.services.exceptions.UnauthorizedException) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices)

Example 12 with ScalecubeServiceDiscovery

use of io.scalecube.services.discovery.ScalecubeServiceDiscovery in project scalecube by scalecube.

the class Example2 method main.

/**
 * Main method.
 *
 * @param args - program 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();
    final Address gatewayAddress = gateway.discovery().address();
    Microservices service2Node = 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(new Service2Impl()).startAwait();
    Microservices service1Node = 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(new Service1Impl()).startAwait();
    gateway.call().api(Service1.class).remoteCallThenManyDelay(100).publishOn(Schedulers.parallel()).take(10).log("receive     |").collectList().log("complete    |").block();
    Mono.whenDelayError(gateway.shutdown(), service1Node.shutdown(), service2Node.shutdown()).block();
}
Also used : RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Microservices(io.scalecube.services.Microservices) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Schedulers(reactor.core.scheduler.Schedulers) Mono(reactor.core.publisher.Mono) Address(io.scalecube.net.Address) 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 13 with ScalecubeServiceDiscovery

use of io.scalecube.services.discovery.ScalecubeServiceDiscovery in project scalecube by scalecube.

the class Example1 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("ms", 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 service proxy
    GreetingsService service = seed.call().api(GreetingsService.class);
    // Execute the services and subscribe to service events
    service.sayHello("joe").subscribe(consumer -> System.out.println(consumer.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) Microservices(io.scalecube.services.Microservices) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Mono(reactor.core.publisher.Mono) GreetingsService(io.scalecube.services.examples.helloworld.service.api.GreetingsService) Address(io.scalecube.net.Address) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Address(io.scalecube.net.Address) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) GreetingsService(io.scalecube.services.examples.helloworld.service.api.GreetingsService) GreetingServiceImpl(io.scalecube.services.examples.helloworld.service.GreetingServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices)

Example 14 with ScalecubeServiceDiscovery

use of io.scalecube.services.discovery.ScalecubeServiceDiscovery in project scalecube by scalecube.

the class Example1 method main.

/**
 * Main method.
 *
 * @param args - program 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();
    final Address gatewayAddress = gateway.discovery().address();
    Microservices service2Node = 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(new Service2Impl()).startAwait();
    Microservices service1Node = 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(new Service1Impl()).startAwait();
    gateway.call().api(Service1.class).manyDelay(100).publishOn(Schedulers.parallel()).take(10).log("receive     |").collectList().log("complete    |").block();
    Mono.whenDelayError(gateway.shutdown(), service1Node.shutdown(), service2Node.shutdown()).block();
}
Also used : RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Microservices(io.scalecube.services.Microservices) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Schedulers(reactor.core.scheduler.Schedulers) Mono(reactor.core.publisher.Mono) Address(io.scalecube.net.Address) 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 15 with ScalecubeServiceDiscovery

use of io.scalecube.services.discovery.ScalecubeServiceDiscovery in project scalecube by scalecube.

the class Example1 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).defaultContentType(// set explicit default data format
    PROTOSTUFF).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 GreetingServiceImpl()).startAwait();
    seed.call().api(GreetingsService.class).sayHello("joe (on default dataFormat PROTOSTUFF)").subscribe(consumer -> System.out.println(consumer.message()));
    seed.call().contentType(JSON).api(GreetingsService.class).sayHello("alice (on JSON dataFormat)").subscribe(consumer -> System.out.println(consumer.message()));
    seed.call().contentType(OCTET_STREAM).api(GreetingsService.class).sayHello("bob (on java native Serializable/Externalizable dataFormat)").subscribe(consumer -> System.out.println(consumer.message()));
    seed.onShutdown().block();
    ms.onShutdown().block();
}
Also used : RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) GreetingServiceImpl(io.scalecube.services.examples.helloworld.service.GreetingServiceImpl) Microservices(io.scalecube.services.Microservices) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) GreetingsService(io.scalecube.services.examples.helloworld.service.api.GreetingsService) Address(io.scalecube.net.Address) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) 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)

Aggregations

ScalecubeServiceDiscovery (io.scalecube.services.discovery.ScalecubeServiceDiscovery)19 WebsocketTransportFactory (io.scalecube.transport.netty.websocket.WebsocketTransportFactory)19 RSocketServiceTransport (io.scalecube.services.transport.rsocket.RSocketServiceTransport)17 Address (io.scalecube.net.Address)15 Microservices (io.scalecube.services.Microservices)15 Mono (reactor.core.publisher.Mono)15 Duration (java.time.Duration)8 ServiceMessage (io.scalecube.services.api.ServiceMessage)7 Test (org.junit.jupiter.api.Test)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 ServiceInfo (io.scalecube.services.ServiceInfo)5 ServiceCall (io.scalecube.services.ServiceCall)4 Authenticator (io.scalecube.services.auth.Authenticator)4 UnauthorizedException (io.scalecube.services.exceptions.UnauthorizedException)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4 BaseTest (io.scalecube.services.BaseTest)3 ServiceEndpoint (io.scalecube.services.ServiceEndpoint)3 CredentialsSupplier (io.scalecube.services.auth.CredentialsSupplier)3 GreetingServiceImpl (io.scalecube.services.examples.helloworld.service.GreetingServiceImpl)3 HashMap (java.util.HashMap)3