【问题标题】:Using Spring HTTP Invoker in Micronaut在 Micronaut 中使用 Spring HTTP Invoker
【发布时间】:2018-11-13 14:46:22
【问题描述】:

我们有很多小型 Spring Boot 应用程序,它们是迁移到 Micronaut 的潜在候选者。他们中的大多数使用 Springs HTTP Invoker 相互通信。

这是一个将执行远程调用的客户端服务 bean 的示例。

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

BrokerService 看起来像像这样

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);
}

Micronaut 中是否有使用 Spring HTTP Invoker 的方法?

【问题讨论】:

  • 你能显示 BrokerService 的代码吗?
  • @JamesKleeh 感谢您的回复。您究竟想从 BrokerService 中看到什么?接口够用还是想看服务端实现?
  • 我认为没有什么可以阻止您在 Micronaut 中使用 spring http 调用程序,但是您可以使用 micronaut http 客户端实现相同的目标,而无需向 spring http 调用程序添加依赖项。如果您要使用另一个接口扩展该接口并使用 @Client 对其进行注释并扩展方法以添加 @Get/@Put/etc 注释,我相信它应该可以工作

标签: micronaut


【解决方案1】:

添加spring远程依赖:

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

我没有运气以通常的方式注入代理,但这有效:


@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();
  }
}

然后你可以@Inject Micronaut 端的RemoteService。 对我们来说它有效,但我不知道为什么需要调用 afterPropertiesSet()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多