1. ふりとは何ですか
Feign も負荷分散を実現するためのものですが、Ribbon よりも使い方が簡略化されており、実際にはリボンをベースにカプセル化されており、インターフェイスを呼び出すことで負荷分散を実現できます。Feign とリボンは両方とも Netflix によって提供されます。Feign は、宣言型のテンプレート化された Web サービス クライアントであり、Web サービス クライアントを作成する開発者の操作を簡素化します。開発者は、シンプルなインターフェイスと注釈を通じて HTTP API を呼び出すことができます。開発がより簡素化され、高速化されます。Spring Cloud Feign も Netflix Feign をベースとした二次開発であり、Ribbon と Hystrix を統合し、プラガブル、アノテーションベース、負荷分散、サービス融合などの一連の便利な機能を備えています。実際の開発では、Ribbon に代わるものです。
ibbon+RestTemplate と比較すると、Feign はコード開発を大幅に簡素化します Feign は、Feign アノテーション、JAX-RS アノテーション、Spring MVC アノテーションなどを含む複数のアノテーションをサポートします Spring Cloud は Feign を最適化し、Ribbon と Eureka を統合するため、Feign はより使いやすくなります。
2. リボンとフェーンの違い
リボンは一般的な HTTP クライアント ツールであり、Feign はリボンをベースに実装されています。
3. フェイクの利点
1. Feign は宣言型 Web サービス クライアントです。
2. Feign アノテーション、Spring MVC アノテーション、JAX-RS アノテーションのサポート
3. Feign はリボンに基づいて実装されており、より使いやすくなっています
4. Feign は Hystrix を統合し、サービス融合機能を備えています
四、実戦!
- モジュールを作成し、pom.xml を次のように構成します。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
- 構成ファイル application.yml を作成します。構成は次のとおりです。
server:
port: 8050
spring:
application:
name: feign
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
- 次のコードを使用して起動クラスを作成します。
package com.zing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(FeignApplication.class, args);
}
}
注解说明:
* @EnableFeignClients:声明其为 Feign 客户端
- 宣言型インターフェイスを作成します。コードは次のとおりです。
package com.zing.feign;
import java.util.Collection;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import com.zing.entity.Student;
@FeignClient(value = "provider")
public interface IFeignService {
@GetMapping("/student/findAll")
public Collection<Student> findAll();
@GetMapping("/student/index")
public String index();
}
5. ハンドラー コードは次のとおりです。
package com.zing.controller;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zing.entity.Student;
import com.zing.feign.IFeignService;
@RestController
@RequestMapping("/feign")
public class FeignHandler {
@Autowired
private IFeignService feignservice;
@GetMapping("/findAll")
public Collection<Student> findAll(){
return feignservice.findAll();
}
@GetMapping("/index")
public String index() {
return feignservice.index();
}
}
- feign のロード バランシング機能をテストする
(1) 登録センターを別々に起動し、異なるポートを持つ 2 つのサービス プロバイダーを使用します。feign の登録センター ページは次のとおりです。(2)
http://localhost:8050/feign/index に複数回アクセスします。 2 つのポート アドレスが交互に表示されるのを見ると、Feign が負荷分散を達成していることがわかります。以下の図に示すように、
7. Feign のヒューズ メカニズムをテストします
(1) 最初にすべてのサービス プロバイダーを停止し、登録センターと Feign のサービスのみを維持し、次の図に示すように登録センターを開きます。 2
) http://localhost:8050 に再度アクセスします。/feign/index 次の図に内容が表示されます。
(3) エラー メッセージが直接公開されないようにするには、サービス ヒューズ メカニズムを追加し、アプリケーションを変更する必要があります。 yml は次のようになります。
server:
port: 8050
spring:
application:
name: feign
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
設定手順:
- feign.hystrix.enable: ヒューズ メカニズムを有効にするかどうか。デフォルトは false です。
(4) IFeignService の実装クラス FeignServiceImpl を作成し、その中にフォールトトレラントな処理機構を定義し、@Component アノテーションを介して FeignServiceImpl インスタンスを IOC コンテナに注入するコードは以下のとおりです。
package com.zing.feign.impl;
import java.util.Collection;
import org.springframework.stereotype.Component;
import com.zing.entity.Student;
import com.zing.feign.IFeignService;
@Component
public class FeignServiceImpl implements IFeignService{
@Override
public Collection<Student> findAll() {
return null;
}
@Override
public String index() {
return "服务器维护中。。。";
}
}
(5) ダウングレード処理用IFeignServiceのインターフェース定義に@FeignClientのfallback属性を定義し、マッピングを設定し、FeignServiceImplにマッピングします。IFeignService を変更した後のコードは次のようになります。
package com.zing.feign;
import java.util.Collection;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import com.zing.entity.Student;
import com.zing.feign.impl.FeignServiceImpl;
@FeignClient(value = "provider",fallback = FeignServiceImpl.class)
public interface IFeignService {
@GetMapping("/student/findAll")
public Collection<Student> findAll();
@GetMapping("/student/index")
public String index();
}
(6) 手順 (1) と (2) を繰り返すと、次のインターフェイスが表示され、サービス ヒューズ メカニズムが機能していることがわかります。図に示すように:
V. まとめ
このコードでは、Feign を使用して負荷分散とサービス融合を実現します。Feign の構成とその使用の基本原理を理解した後、Hystix のフォールト トレランス メカニズムを注意深く学習しましょう。次回の記事Spring Cloud (8): Hystix フォールト トレランス メカニズムをお楽しみに。
開発エンジニアの一人も継続学習段階にあり、普段のちょっとした体験談を随時共有しています。私の書いた文章を読んでくれた方が、寄り道をせずに、仕事や勉強の成功を祈っていただければ幸いです。
ブロガーの経験は限られていますが、欠点がある場合は、コミュニケーションを取り、一緒に改善することを歓迎します。同じ CSDN の皆さんと一緒に進歩していきたいと思っています。著者 | スイートリトルスイートポテト
プロデュース | リトルスイートポテト