偽物の紹介
Feignは宣言されたWebサービスクライアントであるため、Webサービスクライアントの書き込みが容易になります。 feignを使用してインターフェイスを作成し、注釈を付けます。 Feign AnnotationやJax-RS Annotationなど、プラグ可能な注釈サポートがあります。 Feignは、Pluggable EncoderとDecoderもサポートしています。 Spring Cloudは、Spring MVCに注釈を追加します。 Spring Webはデフォルトでhttpmessageconverterを使用し、Spring CloudはRibbonとEurekaが提供するLoad-Balanced HTTP Client Feignを統合します。
宣言的な休憩クライアント:feign
まず、eureka_register_serviceプロジェクト(登録センター)およびbiz-service-0プロジェクト(サービスプロデューサー)を開始します
mavenプロジェクトeureka_feign_clientを作成します
pom.xml
<parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-parent </artifactid> <バージョン> 1.4.3.Release </version> <raterative path/> <! - リポジトリの外観の親 - > </parent> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <deprencies> <shentency> <shentency> <edreatency> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-feign </artifactid> </dependency> <dependency> springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </dependency> <依存関係> <グループ<グループ< <artifactid> spring-boot-starter-web </artifactid> </dependency> <dependency> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> <scope> test </scope> </dependencies> <Dependencies> <Dependencies> <GroupId> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <version> brixton.sr5 </version> <type> pom </type> <scope> import </scope> </dependency> </dependencies> </despencymanagemation>
メインアプリケーションクラスで@EnableFeignClientsアノテーションを介してfeign機能を有効にする
ファイルfeignapplication.javaを開始します
@springbootapplication@enablediscoveryclient@enabablefeignclientspublic class feignapplication {public static void main(string [] args){springApplication.run(feignapplication.class、args); }}Service Interfaceクラスのuserclient.javaを定義します
@feignclient( "biz-service-0")注釈を使用して、このインターフェイスの対応するbiz-service-0サービスをバインドします
@feignclient( "biz-service-0")public interface userclient {@requestmapping(method = requestmethod.get、value = "/getuser")public user getuserinfo(); @RequestMapping(method = requestmethod.get、value = "/getUser")public string getUserInfostr(); @RequestMapping(method = requestmethod.get、value = "/info")public string info();}次のように、上記のWebレイヤーで定義されたusercontrollerに電話してください
@RestControllerPublic Class userController {@autowired userclient userclient; @RequestMapping(value = "/getUserInfo"、method = requestmethod.get)public user getUserInfo(){return userclient.getuserInfo(); } @RequestMapping(value = "/getUserInfostr"、method = requestMethod.get)public string getUserInfoStr(){return userclient.getuserInfostr(); } @RequestMapping(value = "/info"、method = requestmethod.get)public string info(){return userclient.info(); }} Application.Properties構成変数
spring.application.name = feign-consumerserver.port = 8004eureka.client.serviceurl.defaultzone = http:// localhost:8000/eureka/
http://127.0.0.1:8004/getuserinfoをご覧ください
要約:
実際、HTTPコールサービスメソッドはFeignを通じてカプセル化されているため、クライアントはローカルメソッドを呼び出すようにメソッドを直接呼び出します。 Dubboのリモートサービスを公開する方法と同様に、違いはDubboがプライベートバイナリプロトコルに基づいているのに対し、Feignは本質的にHTTPクライアントです。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。