Spring Cloud(7):OpenFeignサービスインターフェース呼び出し

1。概要

Feignは宣言型のWebServiceクライアントです。Feignを使用すると、WebServiceクライアントの作成が簡単になります。

その用途は、サービスインターフェイスを定義し、それに注釈を追加することです。Feignは、プラグ可能なエンコーダーとデコーダーもサポートしています。Spring Cloudは、SpringMvc標準アノテーションとHttpMessageConvertersをサポートするためにFeignをカプセル化しました。FeignをEurekaおよびRibbonと組み合わせて使用​​すると、負荷分散をサポートできます。

ソースアドレス:https://github.com/spring-cloud/spring-cloud-openfeign

1.1何ができるか

Feignは、JavaHttpクライアントの作成を容易にすることを目的としています。
Ribboon + RestTemplateを使用する場合、RestTemplateを使用してhttpリクエストをカプセル化し、テンプレート化された呼び出しメソッドのセットを形成しました。ただし、実際の開発では、サービス依存関係への呼び出しが複数ある可能性があるため、1つのインターフェースが複数の場所で呼び出されることがよくあります。 。、したがって、通常、マイクロサービスごとにいくつかのクライアントクラスをカプセル化して、これらの依存サービス呼び出しをパッケージ化します。したがって、Feignはこれに基づいてさらにカプセル化し、依存サービスインターフェイスの定義を定義および実装するのに役立ちます。Feignの実装では、インターフェイスを作成し、アノテーションを使用して構成するだけで済みます(以前は、Daoインターフェイスは@Mapperアノテーションでマークされていましたが、現在はマイクロサービスであり、Feignアノテーションをマークするだけです)。サービスプロバイダーのインターフェイスバインディングにより、Springクラウドリボンを使用するときにサービスコールクライアントを自動的にカプセル化する開発の量が簡素化されます。

FeignはRibbonを統合します:
Ribbonを使用して支払いサービスリスト情報を維持し、ポーリングを通じてクライアントの負荷分散を実装します。Ribbonとは異なり、サービスバインディングインターフェイスのみをfeignを介して宣言的な方法で定義する必要があります。サービスコールの。

FeignとOpenFeignの違い:

フェイグ OpenFeign
Feignは、SpringCloudコンポーネントの軽量RESTfulHTTPサービスクライアントです。Feignには、サービスレジストリのサービスを呼び出すためのクライアント負荷分散に使用されるリボンが組み込まれています。Feignの使用方法は次のとおりです。Feignのアノテーション定義インターフェイスを使用します。このインターフェイスを呼び出すと、サービスレジストリのサービスを呼び出すことができます OpenFeignは、Spring Cloudが@RequestMappingなどのFeignに基づいてSpringMVCアノテーションをサポートすることです。OpenFeignの@FeignClientは、SpringMVCの@RequestMappingアノテーションの下のインターフェイスを解析し、実装クラスで実行される動的プロキシを通じて実装クラスを生成できます。負荷分散と他のサービスの呼び出し
  • 偽物:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
  • openfeign:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.OpenFeignを使用する手順

2.1インターフェース+アノテーション

マイクロサービス呼び出しインターフェース+ @ FeignClient

2.2新しいcloud-consumer-feign-order80

Feignは消費者側で使用されます

2.3ポン

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.lele.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order80</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.lele.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2.4 yml

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

2.5メインスタートアップクラス

package com.lele.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author: lele
 * @date: 2021/3/17 7:27
 * @description:
 */
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(OrderFeignMain80.class, args);
    }
}

2.6ビジネス

ビジネスロジックインターフェイス+ @ FeignClient構成呼び出しプロバイダーサービス

新しいPaymentFeignServiceインターフェースを作成し、アノテーション@FeignClientを追加します

package com.lele.springcloud.service;

import com.lele.springcloud.entities.CommonResult;
import com.lele.springcloud.entities.Payment;
import feign.Param;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author: lele
 * @date: 2021/3/17 7:31
 * @description:
 */
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    
    

    @GetMapping(value = "/payment/get/{id}")
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}

2.7コントローラー

package com.lele.springcloud.controller;

import com.lele.springcloud.entities.CommonResult;
import com.lele.springcloud.entities.Payment;
import com.lele.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: lele
 * @date: 2021/3/17 7:39
 * @description:
 */
@RestController
@Slf4j
public class OrderFeignController {
    
    

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
    
    
        return paymentFeignService.getPaymentById(id);
    }
}

2.8テスト

  • Eurekaクラスター7001、7002を最初に開始します
  • 8001、8002を開始
  • cloud-consumer-feign-order80を開始します
  • 访问:http:// localhost / Consumer / Payment / get / 1

Feignには、負荷分散構成アイテムが付属しています

ここに画像の説明を挿入します

3.OpenFeignタイムアウト制御

  • 8001 PaymentController
    ここに画像の説明を挿入します

  • 80 PaymentFeignService
    ここに画像の説明を挿入します

  • 80 OrderFeignController
    ここに画像の説明を挿入します

  • 7001、7002を開始

  • 8001を開始

  • feign80
    へのアクセスを開始します:http:// localhost / Consumer / Payment / feign / timeout
    ここに画像の説明を挿入します

OpenFeignはデフォルトで1秒待機しますが、サーバーの処理に1秒以上かかるため、Feignクライアントは待機する必要がなくなり、直接エラーを返します。この状況を回避するために、タイムアウト制御を設定する必要がある場合があります。偽のクライアントの。

yml構成ファイルで開きます:
ここに画像の説明を挿入します

# 设置feign客户端超时时间(openfiegn默认支持riboon)
ribbon:
  # 建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  # 建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000
  • feign80を再起動します
  • 访问:http:// localhost / consumer / payment / feign / timeout
    ここに画像の説明を挿入します

4.OpenFeignログ印刷機能

4.1とは

Feignはログ印刷機能を提供し、FeignでのHttpリクエストの詳細を理解するために、構成を通じてログレベルを調整できます。
つまり、Feignインターフェイスの呼び出しを監視して出力します。

4.2ログレベル

  • NONE:デフォルトではログは表示されません。
  • 基本:リクエストメソッド、URL、レスポンスステータスコード、実行時間のみを記録します。
  • ヘッダー:BASICで定義された情報に加えて、要求と応答のヘッダー情報があります。
  • FULL:HEADERSで定義された情報に加えて、リクエストとレスポンスの本文とメタデータもあります。

4.3ロギングBeanを構成する

ここに画像の説明を挿入します

4.4YMLファイルでログを有効にする必要がある偽のクライアント

ここに画像の説明を挿入します

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.lele.springcloud.service.PaymentFeignService: debug

访问:http:// localhost / Consumer / Payment / get / 1

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/houwanle/article/details/114914778