下面的指導頁非常好,它可以作爲彈簧啓動應用程序中功能區的基本情況。@LoadBalanced RestTemplate調用嵌套的上下文端點
https://spring.io/guides/gs/client-side-load-balancing/
的示例停止一旦端點映射成爲嵌套的工作 - 例如加入
@RequestMapping(值= 「/歡迎」)在類級
@RestController
@SpringBootApplication
@RequestMapping(value = "/welcome") //<------------- ADDED --->
public class SayHelloApplication {
private static Logger log = LoggerFactory.getLogger(SayHelloApplication.class);
@RequestMapping(value = "/greeting")
public String greet() {
,然後改變從
String greeting = this.restTemplate.getForObject("http://say-hello/greeting", String.class);
客戶機中的@LoadBalanced RestTemplate呼叫
至
String greeting = this.restTemplate.getForObject("http://say-hello/welcome/greeting", String.class);
調用與附加堆棧跟蹤失敗,而直接訪問http://localhost:8090/welcome/greeting仍然正常工作。將功能區配置爲將均衡請求加載到長期和嵌套的URL端點(如domain.com/x/y/z/p/q)的適當方式是什麼?
堆棧跟蹤:
java.lang.IllegalStateException: No instances available for say-hello
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:79) ~[spring-cloud-netflix-core-1.1.4.RELEASE.jar:1.1.4.RELEASE]
at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:46) ~[spring-cloud-commons-1.1.1.RELEASE.jar:1.1.1.RELEASE]
at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:85) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:69) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:596) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at hello.UserApplication.hi(UserApplication.java:31) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
功能區僅處理主機名。你的錯誤表示功能區找不到任何say-hello的實例。你重試了嗎?它可能需要一點。 – spencergibb
請務必檢查Eureka,看看是否有爲say-hello服務註冊的實例。 –
根據庫的示例代碼和給定鏈接say-hello.ribbon.eureka.enabled = false。我只做了問題中指定的更改。它似乎只在給定的URL是http:// say-hello/context(該示例有效)時才處理主機名,而當它是http:// say-hello/context/sub-context /時跟蹤) – nmadzharov