如何让Netty通过System属性监听所有接口?

时间:2017-04-07 13:21:48

标签: netty pact mockserver

我正在使用https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-junitMockServer使用Netty,使用{{3}}。

启动的Netty仅侦听环回(netstat -tulpn输出:)

tcp 0 0 127.0.0.1:21221 0.0.0.0:* LISTEN 27681/java

,但我想听听外面的世界"。我希望启动的Netty接受我的LAN-IP地址上的连接(例如,192.168.x.y:21221作为本地地址),甚至是每个到Netty端口的传入连接(0.0.0.0:21221作为本地地址)。由于我的框架链以某种方式使Netty配置,很好,很远,我想在使用System属性启动我的应用程序时这样做。像

这样的东西
    System.setProperty("io.netty.listenAddress", "0.0.0.0");

这有可能吗?

1 个答案:

答案 0 :(得分:0)

您可以在{Pact-> MockServer->)Netty前放置ReverseProxy来达到您的要求。

这是一个完整的例子。对于拥有ReverseProxy,使用LittleProxy。如上所述,此示例使用pact-jvm-consumer-junit,并执行HTTP调用HtmlUnit

import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactProviderRule;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.model.MockProviderConfig;
import au.com.dius.pact.model.PactFragment;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.littleshoot.proxy.ChainedProxyAdapter;
import org.littleshoot.proxy.ChainedProxyManager;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;

public class HelloIT {

  private static final String PROVIDER_SERVICE_PATH = "/provider-endpoint";

  @Rule
  public PactProviderRule pactProviderServiceMock
    = new PactProviderRule("provider-service", this);

  private final String providerServiceReverseProxyUrl;

  private static HttpProxyServer createReverseProxy(final MockProviderConfig mockProviderConfig) {
    final String ip = mockProviderConfig.getHostname();
    final int port = mockProviderConfig.getPort();

    final ChainedProxyManager cpm = (httpRequest, chainedProxies) -> {
      final ChainedProxyAdapter chainedProxy = new ChainedProxyAdapter() {
        @Override
        public InetSocketAddress getChainedProxyAddress() {
          return new InetSocketAddress(ip, port);
        }
      };
      chainedProxies.add(chainedProxy);
    };

    return DefaultHttpProxyServer.bootstrap()
      .withChainProxyManager(cpm)
      .withPort(0)
      .withAllowLocalOnly(false)
      .withAllowRequestToOriginServer(true)
      .start();
  }

  {
    /*
     * Pact-mock for provider-service listens on loopback only. Using reverse proxy for interaction
     * between consumer-service and provider-service on external IP.
     */
    final HttpProxyServer providerServiceReverseProxy
      = HelloIT.createReverseProxy(pactProviderServiceMock.getConfig());

    this.providerServiceReverseProxyUrl
      = "http://<your-external-IP>:" + providerServiceReverseProxy.getListenAddress().getPort();
  }

  @Pact(consumer = "consumer-service")
  public PactFragment createFragment(final PactDslWithProvider builder) {
    return builder
      .uponReceiving("Some request")
      .path(PROVIDER_SERVICE_PATH)
      .method("GET")
      .willRespondWith()
      .status(200)
      .toFragment();
  }

  @Test
  @PactVerification
  public void index() throws IOException {
    final Page page
      = new WebClient().getPage(this.providerServiceReverseProxyUrl + PROVIDER_SERVICE_PATH);
    Assert.assertEquals(200, page.getWebResponse().getStatusCode());
  }
}