saw303
saw303

Reputation: 9082

Using Spring HTTP Invoker in Micronaut

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?

Upvotes: 0

Views: 115

Answers (1)

Marco Nembrini
Marco Nembrini

Reputation: 111

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.

Upvotes: 1

Related Questions