[英]Spring Cloud - Use @LoadBalaneced RestTemplate in Zuul
I have a common Spring Cloud architecture in my project: eureka for service discovery, zuul as a reverse proxy and some microservices.我的项目中有一个通用的 Spring Cloud 架构:eureka 用于服务发现,zuul 作为反向代理和一些微服务。 I want to be able to access the microservices from zuul programatically.
我希望能够以编程方式从 zuul 访问微服务。 As zuul is backed by Ribbon I just wanted to autowire a
@LoadBalanced
RestTemplate
, but apparently this does not work.由于 zuul 由 Ribbon 支持,我只想自动装配
@LoadBalanced
RestTemplate
,但显然这不起作用。 Below is my little test setup.下面是我的小测试设置。
ZuulGatewayApplication.java: ZuulGatewayApplication.java:
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
SomeService.java: SomeService.java:
@RequiredArgsConstructor
@Service
@Slf4j
public class SomeService {
private final RestTemplate restTemplate;
@PostConstruct
public void init() {
ResponseEntity<Void> response = restTemplate.getForEntity("http://my-service/notify", Void.class);
}
}
Here is the relevant part of the stacktrace:这是堆栈跟踪的相关部分:
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://my-service/notify": my-service; nested exception is java.net.UnknownHostException: my-service
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:748) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:342) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at xxx.xxx.xxxx.zuulgateway.service.SomeService.init(SomeService.java:40) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
... 38 common frames omitted
Caused by: java.net.UnknownHostException: my-service
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:540) ~[na:na]
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182) ~[na:na]
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474) ~[na:na]
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569) ~[na:na]
at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:na]
at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341) ~[na:na]
at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362) ~[na:na]
at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1242) ~[na:na]
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181) ~[na:na]
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075) ~[na:na]
at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1009) ~[na:na]
at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:739) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
Do I need to configure something else to get the correct RestTemplate
?我是否需要配置其他东西才能获得正确的
RestTemplate
? I also tried to annotate the MainClass with @RibbonClient
, but the error message stays the same.我还尝试使用 @RibbonClient 注释
@RibbonClient
,但错误消息保持不变。
It doesn't work in post construct.它在后期构造中不起作用。 You'll need to listen for an event like
ApplicationReadyEvent
.您需要监听
ApplicationReadyEvent
类的事件。
public class SomeService
implements ApplicationListener<ApplicationReadyEvent> {
private final RestTemplate restTemplate;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
ResponseEntity<Void> response = restTemplate.getForEntity("http://my-service/notify", Void.class);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.