Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dynamically register services #825

Closed
zhou-hao opened this issue Nov 16, 2021 · 6 comments
Closed

How to dynamically register services #825

zhou-hao opened this issue Nov 16, 2021 · 6 comments

Comments

@zhou-hao
Copy link
Contributor

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

@ronenhamias
Copy link
Member

ronenhamias commented Nov 16, 2021

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
Copy link
Contributor Author

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
Copy link
Member

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
Copy link
Contributor Author

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

private ServiceDiscoveryEvent toServiceDiscoveryEvent(MembershipEvent membershipEvent) {
ServiceDiscoveryEvent discoveryEvent = null;
if (membershipEvent.isAdded() && membershipEvent.newMetadata() != null) {
discoveryEvent = newEndpointAdded(decodeMetadata(membershipEvent.newMetadata()));
}
if (membershipEvent.isRemoved() && membershipEvent.oldMetadata() != null) {
discoveryEvent = newEndpointRemoved(decodeMetadata(membershipEvent.oldMetadata()));
}
if (membershipEvent.isLeaving() && membershipEvent.newMetadata() != null) {
discoveryEvent = newEndpointLeaving(decodeMetadata(membershipEvent.newMetadata()));
}
return discoveryEvent;

@ronenhamias
Copy link
Member

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
Copy link
Contributor Author

zhou-hao commented Nov 18, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants