Spring Cloud – 在 Zuul 中使用 @LoadBalanced 的 RestTemplate

huangapple go评论81阅读模式
英文:

Spring Cloud - Use @LoadBalaneced RestTemplate in Zuul

问题

以下是您提供的内容的翻译部分:

我在项目中使用了一个常见的Spring Cloud架构:Eureka用于服务发现,Zuul作为反向代理以及一些微服务。我希望能够从Zuul程序中以编程方式访问这些微服务。由于Zuul由Ribbon支持,我只想自动装配一个@LoadBalancedRestTemplate,但显然这并不起作用。以下是我的一些测试设置。

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:

@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);
    }
}

以下是相关的部分堆栈跟踪:

Caused by: org.springframework.web.client.ResourceAccessException: 在对“http://my-service/notify”的GET请求上发生I/O错误:my-service;嵌套异常是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]
	...
Caused by: java.net.UnknownHostException: my-service
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220) ~[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]
	...

我需要配置其他内容才能获得正确的RestTemplate吗?我还尝试过在MainClass上使用@RibbonClient注解,但错误消息仍然相同。

英文:

I have a common Spring Cloud architecture in my project: eureka for service discovery, zuul as a reverse proxy and some microservices. I want to be able to access the microservices from zuul programatically. As zuul is backed by Ribbon I just wanted to autowire a @LoadBalanced RestTemplate, but apparently this does not work. Below is my little test setup.

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:

@RequiredArgsConstructor
@Service
@Slf4j
public class SomeService {
    private final RestTemplate restTemplate;

    @PostConstruct
    public void init() {
        ResponseEntity&lt;Void&gt; response = restTemplate.getForEntity(&quot;http://my-service/notify&quot;, Void.class);
    }
}

Here is the relevant part of the stacktrace:

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for &quot;http://my-service/notify&quot;: 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.&lt;init&gt;(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? I also tried to annotate the MainClass with @RibbonClient, but the error message stays the same.

答案1

得分: 2

这不起作用在后置构造方法中。您需要监听诸如 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);
    }
}
英文:

It doesn't work in post construct. You'll need to listen for an event like ApplicationReadyEvent.

public class SomeService
		implements ApplicationListener&lt;ApplicationReadyEvent&gt; {
    private final RestTemplate restTemplate;

	@Override
	public void onApplicationEvent(ApplicationReadyEvent event) {
        ResponseEntity&lt;Void&gt; response = restTemplate.getForEntity(&quot;http://my-service/notify&quot;, Void.class);
	}
}

huangapple
  • 本文由 发表于 2020年10月20日 02:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64433176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定