2017-10-13 26 views
0

我有一個利用微服務的項目。我正在嘗試使用wire-mock來創建一個測試環境。使用wiremock-docker代理多個URL

如何使用docker-compose代理多個URL。

這是我的docker-compose文件。

networks: 
    ft-simulator: 
     external: false 
services: 
    app: 
     depends_on: 
     - nginx 
     environment: 
     - SPRING_PROFILES_ACTIVE=simulator 
     - JAVA_FLAGS=-Dhttp.proxyHost=wiremock -Dhttp.proxyPort=8080 
     healthcheck: 
      interval: 1m 
      retries: 3 
      test: 
      - CMD 
      - curl 
      - -f 
      - http://localhost:8080/health 
      timeout: 10s 
     image: ft-simulator:latest 
     ports: 
      - "8080:8080" 
     networks: 
      ft-simulator: 
       aliases: 
       - bcp 
    nginx: 
     image: nginx 
     ports: 
      - "80:80" 
      - "443:443" 
     networks: 
     - ft-simulator 
    wiremock: 
     image: rodolpheche/wiremock:2.8.0-alpine 
     networks: 
     - ft-simulator 
     ports: 
      - "8081:8080" 
     volumes: 
      - "$PWD/stubs:/home/wiremock" 
     command: ["--proxy-all=http://bcp:8080, http://www.google.com"] 
version: '3.1' 

Google只是一個佔位符,直到它啓動。

當我跑碼頭工人,組成了我得到以下錯誤

crp11070m:wiremock-simulation john.hamlett$ docker logs -f wiremocksimulation_wiremock_1 
Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in authority at index 7: http://bcp:8080, http://www.google.com 
    at java.net.URI.create(URI.java:852) 
    at com.github.tomakehurst.wiremock.standalone.CommandLineOptions.proxyHostHeader(CommandLineOptions.java:274) 
    at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:123) 
    at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:72) 
    at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:65) 
    at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.main(WireMockServerRunner.java:113) 
Caused by: java.net.URISyntaxException: Illegal character in authority at index 7: http://bcp:8080, http://www.google.com 
    at java.net.URI$Parser.fail(URI.java:2848) 
    at java.net.URI$Parser.parseAuthority(URI.java:3186) 
    at java.net.URI$Parser.parseHierarchical(URI.java:3097) 
    at java.net.URI$Parser.parse(URI.java:3053) 
    at java.net.URI.<init>(URI.java:588) 
    at java.net.URI.create(URI.java:850) 
    ... 5 more 

回答

0

這是不可能的代理到一個以上的目標URL在使用--proxy-all參數的同時。

您可以做的是根據傳入請求,通過爲每個路由創建帶有代理響應的存根映射來改變代理目標,例如,

路線1:

{ 
    "request": { 
     "urlPattern": "/route1/.*" 
    }, 

    "response": { 
     "proxyBaseUrl" : "http://target-host-1.com" 
    } 
} 

路線2:

{ 
    "request": { 
     "urlPattern": "/route2/.*" 
    }, 

    "response": { 
     "proxyBaseUrl" : "http://target-host-2.com" 
    } 
} 

注意,URL路徑部分將被添加到URL,所以你用代理到http://target-host-1.com/route1/whateverhttp://target-host-2.com/route2/whatever請求結束。

+0

謝謝,是否有可能嘲笑外部依賴?比如我從http://www.sweetviz.com/bestimage.jpg抓取圖片?我有一個將JSON返回給我的應用程序的外部服務。如果不可能,我可以將其內部化。 –