[英]Is there a way to use Zuul to proxy inner http calls?
I have a spring-boot
app, that I can make an HTTP
request to, and which will send another HTTP
request to some other resource on the internet.我有一个
spring-boot
应用程序,我可以向它发出HTTP
请求,并将另一个HTTP
请求发送到 Internet 上的其他资源。
@RestController
@SpringBootApplication
public class BookApplication {
@RequestMapping(value = "/available")
public String available() throws Exception {
String url = "https://www.google.com";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuffer response = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return "Spring in Action";
}
public static void main(String[] args) {
SpringApplication.run(BookApplication.class, args);
}
}
And I also have another spring-boot
app that is a Zuul
proxy.而且我还有另一个
spring-boot
应用程序,它是Zuul
代理。
@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public SimpleFilter simpleFilter() {
return new SimpleFilter();
}
}
SimpleFilter class is: SimpleFilter 类是:
public class SimpleFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
return null;
}
}
And application.properties for Zuul
proxy app: Zuul
代理应用的 application.properties:
zuul.routes.books.url=http://localhost:8090
ribbon.eureka.enabled=false
server.port=8080
Basically, everything is from this tutorial tutorial基本上,一切都来自本教程
So I'm wondering if there is a chance to proxy the request to " https://www.google.com " that's done by /available
resource in the BookApplication
?所以我想知道是否有机会将请求代理到由
BookApplication
中的/available
资源完成的“ https://www.google.com ”?
There's no way to do it.没有办法做到这一点。 Zuul is just not meant for that kind of things.
Zuul 只是不适合那种东西。
you can overwrite the standard HttpClient of Zuul by creating a custom bean of CloseableHttpClient.您可以通过创建 CloseableHttpClient 的自定义 bean 来覆盖 Zuul 的标准 HttpClient。 there you can set your proxy configuration.
在那里你可以设置你的代理配置。
@Bean
@Primary
public CloseableHttpClient customHttpClient() {
return HttpClientBuilder.create()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setProxy(new HttpHost("your.proxy.com", 80))
.build();
}
If you want to use different proxies for your routes take a look at my answer in this post: https://stackoverflow.com/a/72207020/19096791如果您想为您的路线使用不同的代理,请查看我在这篇文章中的回答: https ://stackoverflow.com/a/72207020/19096791
Please also take note that zuul is not longer supported by spring cloud.另请注意,spring cloud 不再支持 zuul。 so the recommended way is to switch to spring gateway.
所以推荐的方式是切换到spring gateway。
hope i could helped you希望我能帮助你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.