我正在尝试请求项目的外部服务,我可以将它 curl 到dedocker容器中并放在代理后面,然后得到有效的响应。
// Request example
curl https://api.something.com
请求是https。
但是,当从java代码(在容器内)发出请求时,我会超时得到http503。
tldr:我认为问题是…代理没有应用到java/spring引导上。
我的application.properties是:
server.address=0.0.0.0
我在.mvn/jvm.config上设置了代理变量,只用于下载maven包。
-Dhttp.proxyHost=<proxy_host>
-Dhttp.proxyPort=<proxy_port>
-Dhttps.proxyHost=<proxy_host>
-Dhttps.proxyPort=<proxy_port>
我还尝试添加jvm.config:
-Dhttp.setProxy=true
-Dhttps.setProxy=true
-Djava.net.useSystemProxies=true
但是由于某些原因,配置没有应用到代码上。
public T getRequest(String url, Class<T> typeClass) {
try {
ResponseEntity<T> responseEntity = this.REST_TEMPLATE.getForEntity(url, typeClass);
return responseEntity.getBody();
} catch (HttpClientErrorException e) {
throw new ResponseStatusException(e.getStatusCode(), e.getResponseBodyAsString());
} catch (RuntimeException e) {
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "Serviço indisponivel", e);
}
}
我还尝试在resttemplate构造函数上添加代理变量(503 by timeout):
InetSocketAddress inetSocketAddress = new InetSocketAddress(this.PROXY_HOST, this.PROXY_PORT);
Proxy proxy = new Proxy(Proxy.Type.HTTP, inetSocketAddress);
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setProxy(proxy);
this.REST_TEMPLATE = new RestTemplate(requestFactory);
我的dockerfile是:
FROM maven:3.6.3-jdk-11-slim
ENV https_proxy <proxy_host>:<proxy_port>
ENV http_proxy <proxy_host>:<proxy_port>
ENV HTTP_PROXY <proxy_host>:<proxy_port>
ENV HTTPS_PROXY <proxy_host>:<proxy_port>
WORKDIR /app
COPY ./ /app/
RUN mvn package
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","app.jar"]
我的pom.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>something</groupId>
<artifactId>something</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>something</name>
<description>something</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.4.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
如前所述,我可以在容器中毫无问题地 curl 服务,但是如果我在dockerfile中添加no\u proxy变量,我也可以从 curl 中超时得到503。
所以我认为问题是…代理没有应用到java上。
有人能帮我吗?java/spring引导配置有时非常混乱。
暂无答案!
目前还没有任何答案,快来回答吧!