0
votes

We have plenty of small Spring Boot applications that are potential candidates for a migration to Micronaut. Most of them use Springs HTTP Invoker to communicate with each other.

Here is an example of a client side service bean that will perform a remoting call.

  @Bean
  public HttpInvokerProxyFactoryBean brokerService() {
    HttpInvokerProxyFactoryBean invoker = buildHttpInvokerProxyFactoryBean();
    invoker.setServiceUrl(remotingBaseUrl + BrokerService.URI);
    invoker.setServiceInterface(BrokerService.class);
    return invoker;
  }

The BrokerService looks e.g. like this

public interface BrokerService {

    /**
    * Creates a new offer of the given data.
    *
    * @param ctx All relevant data to create a new offer.
    * @return the newly created offer instance.
    */
    Offer createOffer(OfferCreationContext ctx);
}

Is there a way in Micronaut to use the Spring HTTP Invoker?

1
Can you show the code for BrokerService ? - James Kleeh
@JamesKleeh Thank you for replying. What exactly would you like to see from the BrokerService? Is the interface enough or would you like to see the server implementation? - saw303
I don't think anything prevents you from using spring http invoker in Micronaut, however you could likely achieve the same thing with the micronaut http client without having to add a dependency to spring http invoker. If you were to extend that interface with another interface and annotate it with @Client and extend the methods to add the @Get/@Put/etc annotations, I believe it should work - James Kleeh

1 Answers

1
votes

Add the spring remoting dependencies:

  implementation 'org.springframework:spring-context:$version'
  implementation 'org.springframework:spring-web:$version'

I had no luck injecting the Proxy the usual way, but this works:


@Factory
public class RemotingConfig {

  @Bean
  @Singleton
  public RemoteService remoteService(
      @Value("${remoting.base.url:`http://localhost:8080`}")
          String remotingBaseUrl) {
    HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean();
    invoker.setHttpInvokerRequestExecutor(new SimpleHttpInvokerRequestExecutor());
    invoker.setServiceUrl(remotingBaseUrl + RemoteService.URI);
    invoker.setServiceInterface(RemoteService.class);
    // hack around lack of Spring infrastructure
    invoker.afterPropertiesSet();
    return (RemoteService) invoker.getObject();
  }
}

Then you can @Inject the RemoteService on the Micronaut side. For us it works, but I don't know why the call to afterPropertiesSet() is needed.