Spring Boot apps localhost + different ports = Connection refused

时间:2018-01-15 18:17:19

标签: java spring spring-boot proxy resttemplate

I am getting a Connection refused exception when two spring boot embedded tomcat servers are trying to communicate via localhost (127.0.0.1) on different ports.

  • Service1: localhost:9200 (rest services)
  • Service2: localhost:9201 (spring mvc that communicates with rest service)
  • Service2 (9201) is behind an Apache Proxy (with SSL)

Apache SSL Proxy Config

# spring boot
Prox PreserveHost On
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443
ProxyPass / http://127.0.0.1:9201/
ProxyPassReverse / http://127.0.0.1:9201/

Service1 (REST) application.properties

server.port=9200

Service2 application.properties

server.port=9201
server.use-forward-headers=true
...
core.service.baseUrl=http://127.0.0.1:9200
core.service.protocol=http
core.service.ip=127.0.0.1
core.service.portNumber=9200

Exception

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://127.0.0.1:9200/api/v1/doSomething": Connect to 127.0.0.1:9200 [/127.0.0.1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:9200 [/127.0.0.1] failed: Connection refused (Connection refused)
at 
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666) ~[spring-web-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at 
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) ~[spring-web-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287) ~[spring-web-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]

My RestTemplateFactory

@Component
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {

    @Value("${core.service.username}")
    private String username;

    @Value("${core.service.password}")
    private String password;

    @Value("${core.service.ip}")
    private String ipAddress;

    @Value("${core.service.portNumber}")
    private Integer portNumber;

    @Value("${core.service.protocol}")
    private String protocol;

    private RestTemplate restTemplate;

    public RestTemplate getObject() {
        restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));
        return restTemplate;
    }
    public Class<RestTemplate> getObjectType() {
        return RestTemplate.class;
    }
    public boolean isSingleton() {
        return true;
    }

    public void afterPropertiesSet() {
        HttpHost host = new HttpHost(ipAddress, portNumber, protocol);
        restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactoryBasicAuth(host));
    }
}

I've been troubleshooting this for over a day now and not making a lot of headway. Any ideas would be welcome.

I should point out this works as expected on my local machine. The issues occur when trying to deploy onto the public server.

THANKS!

0 个答案:

没有答案