无法将HTTP请求发送到另一个Docker容器

时间:2019-06-24 12:39:25

标签: spring-boot docker docker-compose httprequest microservices

当我尝试将HTTP请求从一个容器发送到另一个容器时,我得到

(拒绝连接);嵌套的异常是java.net.ConnectException

我确定错误的原因是无效的地址。尽管我已经检查过类似的问题,但我自己却无法解决。

mainApp的Dockerfile(发送请求的文件)

FROM openjdk:8
ADD target/mainApp.war mainApp.war
EXPOSE 8078
ENTRYPOINT ["java", "-jar", "mainApp.war"]

我在这里发出请求

String pepAddress =  "http://microservice3:8080";

public ResponseEntity<String> getUser() {

       RestTemplate restTemplate = new RestTemplate();
       restTemplate.setErrorHandler(new ErrorHandler());

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 1080));
        requestFactory.setProxy(proxy);
        restTemplate.setRequestFactory(requestFactory);

        UriComponentsBuilder builder = UriComponentsBuilder
                .fromHttpUrl(pepAddress + "/getUser")
                .queryParam("userId","1");

        HttpEntity<?> httpEntity = new HttpEntity<>(httpHeaders);

        ResponseEntity<String> result = restTemplate
                .exchange(builder.toUriString(),
                            HttpMethod.GET,
                            httpEntity,
                            String.class);
        return result;
    }

SecondApp Dockerfile

FROM openjdk:8
ADD target/PEP.war PEP.war
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "PEP.war"]

还有docker-compose.yml

version: '2'
services:
  microservice1:
    image: mainapp
    expose:
      - 8078
    networks:
      - test

  microservice3:
    image: pep
    expose:
      - 8080
    networks:
      - test

networks:
  test: 
    external: true

0 个答案:

没有答案