scalecube / scalecube-services

Microservices library - scalecube-services is a high throughput, low latency reactive microservices library built to scale. It features: API-Gateways, service-discovery, service-load-balancing, the architecture supports plug-and-play service communication modules and features. built to provide performance and low-latency real-time stream-processing
http://scalecube.github.io/
Apache License 2.0
612 stars 106 forks source link

How to dynamically register services #825

Closed zhou-hao closed 2 years ago

zhou-hao commented 2 years ago

Some scenes,I need dynamically register services after Microservices started.

ronenhamias commented 2 years ago

after microservices has started its already an immutable object. microservices are automatically registered and discovered within a node and they publish locally and remotely the services they host within a Microservices node in the network of microservices based on SWIM.

you can checkout the examples and the tests inside the project

https://github.com/scalecube/scalecube-services/tree/master/services-examples

the main idea is that any node can be a seed one address that allows all services to join the cluster of services. using gossip and SWIM discovery protocol once a member joined the cluster all other members automatically aware of it it means that seed is only used for first handshake and act as a well known address to join the cluster after this there is no need of seed until next member want to join. so its not mandatory to join the cluster via seed in case somehow you know the address of the any node.

when a member joins a cluster he can just start or start pointing to some seed member. final Address seedAddress = seed.discovery().address();

     .membership(cfg -> cfg.seedMembers(seedAddress)))

  // 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();
``
zhou-hao commented 2 years ago

Sorry,Maybe you don't understand my scenes.

ServiceMethodRegistry registry = new ServiceMethodRegistryImpl();

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)
            .methodRegistry(registry)
            .startAwait();

// register a service after Microservices started
  registry.registerService(ServiceInfo
                                 .fromServiceInstance(new GreetingServiceImpl())
                                  ....
                                 .build())
ronenhamias commented 2 years ago

ohh i see once microservices is started its immutable. so any change that is done to method registry will not take effect. this is because on start there is introspection of the microservices and registration of them to the cluster. so adding anything to the registry after start does not make any sense.

zhou-hao commented 2 years ago

MembershipEvent has UPDATED Type, Is there a plan to support ServiceDiscoveryEvent.Type ?

https://github.com/scalecube/scalecube-services/blob/909a2e1f3ec525ce60a06a5196255f74c0f509a5/services-discovery/src/main/java/io/scalecube/services/discovery/ScalecubeServiceDiscovery.java#L185-L198

ronenhamias commented 2 years ago

scalecube is an open source project - anyone is welcome to fork/offer improvements. pull request are considered/accepted with a reasoning what is the motivation for the improvement.

zhou-hao commented 2 years ago

ok thanks

发自我的iPhone

------------------ Original ------------------ From: Ronen @.> Date: Thu,Nov 18,2021 6:44 PM To: scalecube/scalecube-services @.> Cc: 老周 @.>, Author @.> Subject: Re: [scalecube/scalecube-services] How to dynamically register services (Issue #825)

scalecube is an open source project - anyone is welcome to fork/offer improvements. pull request are considered/accepted with a reasoning what is the motivation for the improvement.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.