3

我試圖從我的一個微服務中尋找外部服務。我正在使用Spring Cloud,Eureka的註冊表和Spring引導作爲主要框架。使用Spring Cloud從微服務命中外部IP

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

從我所看到的,春天的雲注入絲帶至於其餘的模板中的HTTP客戶端,當我嘗試打這個IP(如:193.172.x.x)它產生以下錯誤:

java.lang.IllegalStateException: No instances available for 193.172.x.x at org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory.createRequest(RibbonClientHttpRequestFactory.java:64) at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:76) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:567) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:540) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:247)

看起來Ribbon正在嘗試使用該名稱來查找微服務實例,而不是向外看。有沒有辦法配置功能區來查找外部IP,還是僅用於內部使用?

回答

4

您注射RestTemplate的@LoadBalanced版本。你必須確保你的RestTemplate是一個普通的香草。你可以用new RestTemplate()創建它。如果它是一個bean,只需添加一個限定符以確保您注入了RestTemplate的正確版本。

1

在你可以嘗試的就是用服務ID在你的代碼和配置的實際情況:比你配置端點的靜態列表爲您服務

myExternalService.ribbon.listOfServers=http://ip:port 

ResponseObject state = 
      restTemplate.getForObject("http://myExternalService/state/", ResponseObject.class, urlVariables); 

這種服務不使用服務發現的方式。

http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-ribbon-without-eureka

+0

它確實有效,但只有當我禁用尤里卡。不幸的是,我需要尤里卡以及這項服務。有沒有什麼辦法可以讓兩者同時工作? –

+2

是的,使用'@ LoadBalanced'作爲像Marcin那樣的功能區,並且將非eureka休息模板標記爲'@ Primary'。 – spencergibb

相關問題