2

以下のガイド ページは素晴らしく、スプリング ブーツ アプリケーションのリボンのベース ケースとして機能します。

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() {

そして、クライアントの @LoadBalanced RestTemplate 呼び出しを

String greeting = this.restTemplate.getForObject("http://say-hello/greeting", String.class);

String greeting = this.restTemplate.getForObject("http://say-hello/welcome/greeting", String.class);

http://localhost:8090/welcome/greetingに直接アクセスしても問題なく動作しますが、接続されたスタックトレースで呼び出しが失敗します。domain.com/x/y/z/p/q などの長くネストされた URL エンドポイントへの要求を負荷分散するようにリボンを構成する適切な方法は何ですか?

スタックトレース:

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]
4

2 に答える 2

2

問題は、ルートからに移動するようにハンドラーを@RequestMapping変更したクラスに追加することです。ロード バランサーが機能し続けることができるようにするには、ユーザー アプリ内で使用されているものを更新する必要があります。成功する//welcome/PingUrlSayHelloConfigurationnew PingUrl(false, "/welcome/")

于 2016-08-15T08:59:43.130 に答える
0

また、このサンプル アプリケーションを使用して、素晴らしいリボンを使い始めました。

明確にするために、デザインについてもう少し説明したいと思います。

  1. "/complete/user" フォルダーの下にあるユーザー アプリケーションは " client "アプリケーションであり、" curl http://{host}:8888 "経由でアクセスできます。一方、「/complete/say-hello」フォルダーの下のsay-helloアプリケーションは「サービス プロバイダー」です。例で指示されているように、 {host}:8090{host}:9092、および{host}:9999を介して 3 つのインスタンスをスピンアップする必要があります -- /complete/user/src/main/resources/application.ymlを確認でき ます見てみる;
  2. 「クライアント」ユーザーアプリケーションに埋め込まれたリボンは、デフォルトの Ping 戦略によって一連の負荷分散サービス インスタンス (上記のようにインスタンスをスピンアップすると 3 つになります) を維持します。 URL。ここでコードを見ることができるように、デフォルトでは「/」です(これもURIを指定することによって構成可能です): @Bean public IPing ribbonPing(IClientConfig config) { return new PingUrl(); } さて、あなたの問題に戻りましょう。

SayHelloApplication.javaを明示的に追加して の URI マッピングを変更したら@RequestMapping(value = "/welcome")

@RequestMapping(value = "/")
public String home() {
  log.info("Access /");
  return "Hi!";
}

say-hello アプリケーションの「/」ではなく、「/welcome/」である「/welcome」の下のルート パスを意味します。

次に、たとえば「http://{host}:8090/」である実際の「/」のマッピングはありません。この場合、Ping は 1 つずつ失敗し、最終的にリボンはすべてのサービス インスタンスを異常とマークするため、「say-hello に使用できるインスタンスはありません」という結果になります。

于 2016-11-10T09:52:45.970 に答える