我试图从我的一个微服务中获取外部服务。我将Spring Cloud,Eureka用于注册表,并将Spring Boot作为主要框架。
Map<String, String> urlVariables = new HashMap<>();
urlVariables.put("ip_address", IP);
urlVariables.put("port", PORT);
ResponseObject state =
restTemplate.getForObject("http://{ip_address}:{port}/state/", ResponseObject.class, urlVariables);
从我看到的情况来看,Spring Cloud将Ribbon注入为Rest模板的HTTP客户端,当我尝试点击该IP(例如193.172.x.x
:)时,它会产生以下错误:
java.lang.IllegalStateException:org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor)上的193.172.xx实例在org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory.createRequest(RibbonClientHttpRequestFactory.java:64)上不可用.java:76),位于org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:567),位于org.springframework.web.client.RestTemplate.execute(RestTemplate.java:540)。 client.RestTemplate.getForObject(RestTemplate.java:247)
似乎Ribbon尝试查找具有该名称的微服务实例,而不是在外部查找。有什么方法可以配置功能区以查找外部IP,还是仅用于内部使用?
您正在注入RestTemplate的@LoadBalanced版本。您必须确保RestTemplate是普通的香草模板。您可以使用创建它new RestTemplate()
。如果是Bean,只需添加一个限定符以确保您注入的是正确版本的RestTemplate
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句