Skip to content

Creating service clients

lukaszPulawski edited this page May 26, 2015 · 2 revisions

You can create service clients in three different ways: by using @Discover annotation, by using an injectable RatelClientFactory or by using RatelStandaloneFactory. Three ways are described in the following paragraphs:

@Discover annotation

This method works only with spring 4.0 and above. Whenever you need to use a client to a service, just place a @Discover annotation over it It may look like this:

@Component
public class MyApplication {

    /* MyApplication is managed by spring container, this field will contain
     * an injected MyService client. 
     */
    @Discover //<- marked as discoverable service client
    private MyService service; 

Injected RatelClientFactory

This method works also with older spring versions (3.0+). You can inject the RatelClientFactory singleton anywhere in your app (typically in java config) and then use it to produce service clients. Example:

@Configuration
@EnableServiceDiscovery //(1) <- this guarantees that there is a singleton RatelClientFactory in the context
public class MyApplicationConfiguration {
    
    @Autowired
    private RatelClientFactory ratelFactory; //(2) <- inject the singleton 

    @Bean
    public MyService serviceClient() {
        return ratelFactory.getServiceProxy(MyService.class);//(3) <- use it
    }

Standalone factory

If you don't have a spring container in your you still may use Ratel. In such a case you need to use a RatelStandaloneFactory, like that:

     RatelClientFactory clientFactory = RatelStandaloneFactory.fromRatelServer(ratelAddr);

     MyService myServiceClient = clientFactory.getServiceProxy(MyService.class); 

Find out more on Standalone client