この記事では、Spring Cloud - Hystrixを使用して単一の方法のフォールバックを紹介し、次のように共有しています。
1. Hystrix依存関係に参加します
<Dependency> groupId> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-hystrix </artifactid> </dependency>
2。コントローラーを書き込みます
パッケージcom.chhliu.springboot.restful.controller; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.web.bind.annotation.getMapping; org.springframework.web.bind.annotation.pathvariableをインポートします。 org.springframework.web.bind.annotation.restControllerをインポートします。 com.chhliu.springboot.restful.feignclient.userfeignclientをインポートします。 com.chhliu.springboot.restful.vo.userをインポートします。 com.netflix.hystrix.contrib.javanica.annotation.hystrixcommandをインポートします。 @RestController Public Class RESTTEMPLATECONTROLLERHYSTRIXCOMMAND {@AUTOWIRED PRIVATE USERFEIGNCLIENTクライアント; // feignを使用して安らかなサービス@getMapping( "/get/{id}")@hystrixcommand(fallbackmethod = "findbyidfallback") } // fallbackmethodで指定されたメソッドを上書きします。このメソッドの返品値は、パブリックユーザーfindbyidfallback(long id){user u = new user(); U.SetName( "Zhangsan"); U.setUsername( "Chhliu"); U.SetID(9L); uを返します。 }}3.スタートアップクラスにHystrixのサポートを追加します
@enablecircuitbreaker
4.構成ファイルを追加します
Server.port:7904#eureka serverに登録されているスプリングブートサービスのアプリケーション名はspring.application.name = springboot-rest-template-feign-hystrix eureka.instance.prefer-address = true eurekaサーバー登録サービスのアドレスですeureka.client.service-url.defaultzone = http:// chhliu:chhliu123456@localhost:8764/eureka springboot-h2.ribbon.nfloadbalancerruleclassname = com.netflix.loadbalanser.retrule hystrix.command.default.execution.isolation.thread.timeoutinmilliseconds:1 #to hystrixのフォールバック効果をテストすると、タイムアウト時間はここで1ミリ秒に設定されています
V.テスト
ブラウザ:http:// localhost:7904/get/2に入力します
テスト結果は次のとおりです。
{"id":9、 "username": "chhliu"、 "name": "zhangsan"、 "age":null、 "balance":null}
上記のテスト結果から、接続タイムアウトのため、フォールバック法を直接入力することがわかります。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。