Search in sources :

Example 6 with ScalecubeServiceDiscovery

use of io.scalecube.services.discovery.ScalecubeServiceDiscovery 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 7 with ScalecubeServiceDiscovery

use of io.scalecube.services.discovery.ScalecubeServiceDiscovery 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 8 with ScalecubeServiceDiscovery

use of io.scalecube.services.discovery.ScalecubeServiceDiscovery 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)

Example 9 with ScalecubeServiceDiscovery

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

the class RSocketNettyColocatedEventLoopGroupTest method setUp.

@BeforeEach
public void setUp() {
    this.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 = this.gateway.discovery().address();
    Microservices facade = 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 Facade()).startAwait();
    final Address facadeAddress = facade.discovery().address();
    this.ping = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(facadeAddress))).transport(RSocketServiceTransport::new).services((PingService) () -> Mono.just(Thread.currentThread().getName())).startAwait();
    this.pong = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(facadeAddress))).transport(RSocketServiceTransport::new).services((PongService) () -> Mono.just(Thread.currentThread().getName())).startAwait();
}
Also used : ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mono(reactor.core.publisher.Mono) Inject(io.scalecube.services.annotations.Inject) ServiceMethod(io.scalecube.services.annotations.ServiceMethod) Service(io.scalecube.services.annotations.Service) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) Microservices(io.scalecube.services.Microservices) ServiceCall(io.scalecube.services.ServiceCall) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Optional(java.util.Optional) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Address(io.scalecube.net.Address) BaseTest(io.scalecube.services.BaseTest) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Address(io.scalecube.net.Address) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Microservices(io.scalecube.services.Microservices) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with ScalecubeServiceDiscovery

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

the class ServiceAuthRemoteTest method beforeAll.

@BeforeAll
static void beforeAll() {
    StepVerifier.setDefaultTimeout(TIMEOUT);
    principalMapper = authData -> new UserProfile(authData.get("name"), authData.get("role"));
    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 SecuredServiceImpl()).principalMapper(principalMapper).build()).startAwait();
    serviceWithoutAuthenticator = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(new AnotherSecuredServiceImpl()).principalMapper(principalMapper).build()).startAwait();
    partiallySecuredService = 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 PartiallySecuredServiceImpl()).principalMapper(principalMapper).build()).startAwait();
}
Also used : StepVerifier(reactor.test.StepVerifier) HashMap(java.util.HashMap) RejectedSetupException(io.rsocket.exceptions.RejectedSetupException) PartiallySecuredServiceImpl(io.scalecube.services.sut.security.PartiallySecuredServiceImpl) SecuredServiceImpl(io.scalecube.services.sut.security.SecuredServiceImpl) Authenticator(io.scalecube.services.auth.Authenticator) AfterAll(org.junit.jupiter.api.AfterAll) SecuredService(io.scalecube.services.sut.security.SecuredService) UserProfile(io.scalecube.services.sut.security.UserProfile) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Map(java.util.Map) PartiallySecuredService(io.scalecube.services.sut.security.PartiallySecuredService) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) UnauthorizedException(io.scalecube.services.exceptions.UnauthorizedException) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) AnotherSecuredService(io.scalecube.services.sut.security.AnotherSecuredService) Mono(reactor.core.publisher.Mono) AnotherSecuredServiceImpl(io.scalecube.services.sut.security.AnotherSecuredServiceImpl) ServiceDiscovery(io.scalecube.services.discovery.api.ServiceDiscovery) DisplayName(org.junit.jupiter.api.DisplayName) PrincipalMapper(io.scalecube.services.auth.PrincipalMapper) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Collections(java.util.Collections) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) UserProfile(io.scalecube.services.sut.security.UserProfile) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) PartiallySecuredServiceImpl(io.scalecube.services.sut.security.PartiallySecuredServiceImpl) SecuredServiceImpl(io.scalecube.services.sut.security.SecuredServiceImpl) AnotherSecuredServiceImpl(io.scalecube.services.sut.security.AnotherSecuredServiceImpl) PartiallySecuredServiceImpl(io.scalecube.services.sut.security.PartiallySecuredServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) AnotherSecuredServiceImpl(io.scalecube.services.sut.security.AnotherSecuredServiceImpl) BeforeAll(org.junit.jupiter.api.BeforeAll)

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