1はじめに
Feign
宣言型web service
クライアントであるため、Webサービスクライアントの作成が容易になります。インターフェイスを作成し、インターフェイスに注釈を追加すると、Feignを使用できます。Feignは、FeignアノテーションまたはJAX-RSアノテーションを使用でき、ホットスワップ可能なエンコーダーとデコーダーもサポートします。
Spring Cloudは、Spring MVCアノテーションのサポートをFeignに追加し、統合しRibbon
、Eureka
Feignを使用するときに負荷分散を提供します。@EnableFeignClients
Feignをオンにするために使用します。
要するに:
Feign采用的是基于接口的注解
Feign默认整合了ribbon,具有负载均衡的能力
Feign默认整合了Hystrix,具有熔断的能力
OpenFeign属性構成リストドキュメント:https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.RELEASE/reference/html/appendix.html
OpenFeign公式チュートリアルドキュメント:https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.RELEASE/reference/html/#configuration-properties
2.フェイグを探索する
マイクロサービス間の通信はHTTPに基づいており、Feignは疑似HTTPリクエストクライアントです。宣言型のFeignクライアントを介して、リモートマイクロサービスを呼び出すことができます。リモートマイクロサービス呼び出しを実装するプロセスで、複数のインスタンスが存在する場合、リボンロードバランサーが実行されます。デフォルトではローカルロードバランシング、デフォルトでは轮询机制
マイクロサービス呼び出しを行うことにより、Feignは統合され、デフォルトでリボンロードバランシングをサポートします。
例として、注文サービス(Order
)を使用して商品サービス(Product
)を呼び出し、Feignを使用して消費者サービスをリモートで呼び出す環境を設定します。
3.製品エンジニアリング
pom.xml
<?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>springcloud-alibaba-nacos</artifactId>
<groupId>com.bruce.springcloud-alibaba-nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-alibaba-provider</artifactId>
<dependencies>
<!-- web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--排除tomcat依赖 -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--undertow容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--nacos服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- lombok插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8715
spring:
cloud:
nacos:
discovery:
#服务注册与发现地址
server-addr: 127.0.0.1:8848
#开启服务注册与发现功能
enabled: true
application:
name: nacos-product
商品サービスインターフェースProviderController
@Slf4j
@RestController
public class ProductController {
@Value("${server.port}")
private String port;
@GetMapping(value = "/getProductInfo/{productId}")
public String getProductInfo(@PathVariable("productId") String productId) {
log.info("请求进来啦");
return "Hello Nacos Discovery " + productId;
}
}
クラスProductProviderAppを開始します
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @BelongsProject: springcloud-alibaba-nacos
* @BelongsPackage: com.bruce
* @CreateTime: 2021-02-18 13:08
* @Description: TODO
*/
@SpringBootApplication
public class AppProvider {
public static void main(String[] args) {
SpringApplication.run(AppProvider.class);
}
}
4.プロジェクトを注文する
pom.xml
<?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>springcloud-alibaba-nacos</artifactId>
<groupId>com.bruce.springcloud-alibaba-nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-alibaba-consumer</artifactId>
<dependencies>
<!-- Alibaba-nacos服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<artifactId>nacos-client</artifactId>
<groupId>com.alibaba.nacos</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- nacos-client -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.0</version>
</dependency>
<!-- hystrix断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- openfeign客户端 ,默认集成并开启了ribbon负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--排除tomcat依赖 -->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--undertow容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- lombok插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8710
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
enabled: true
application:
name: nacos-order
FeignクライアントOrderFeignを定義する
Hystrixヒューズコンポーネントは、サービスを保護および低下させるためにOrderFeignに導入されています。@ FeignClientアノテーションでサーキットブレーカーサポートを使用するには、フォールバックとフォールバックファクトリーの2つの方法があります。
Feign Hystrixフォールバックについて:
1つ目:Hystrixはフォールバックの概念をサポートしています。回路が壊れたりエラーが発生したりすると、デフォルトのコードパスが実行されます。特定の@FeignClientセットのフォールバックを有効にするには、フォールバック属性をフォールバックを実装するクラスの名前にする必要があります。また、実装をSpringBeanとして宣言する必要があります。
import com.thinkingcao.order.feign.callback.OrderFeignFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "nacos-product",fallback = HystrixClientFallback.class)
public interface OrderFeign {
@GetMapping(value = "/getProductInfo/{productId}")
public String getProductInfo(@PathVariable("productId") String productId);
@Component
@Slf4j
static class HystrixClientFallback implements OrderFeign {
@Override
public String getProductInfo(String productId) {
return "fallback; reason was: 服务忙,稍后重试" ;
}
}
}
2番目のタイプ:フォールバックトリガーの原因にアクセスする必要がある場合は、内部でfallbackFactoryの属性@FeignClientを使用できます。率直に言って、サービスコールが失敗した理由を知りたい場合は、次のサーキットブレーカー方式を使用できます。
import com.thinkingcao.order.feign.callback.OrderFeignFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "nacos-product",fallbackFactory = OrderFeignFallbackFactory.class)
public interface OrderFeign {
@GetMapping(value = "/getProductInfo/{productId}")
public String getProductInfo(@PathVariable("productId") String productId);
@Component
@Slf4j
static class OrderFeignFallbackFactory implements FallbackFactory<OrderFeign> {
@Override
public OrderFeign create(Throwable throwable) {
log.error("调用异常:"+ throwable.toString());
return new OrderFeign() {
@Override
public String getProductInfo(String string) {
return "开启断路-fallback; reason was: "+ throwable;
}
};
}
}
サービス消費OrderController
package com.bruce.controller;
import com.bruce.client.OrderFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @BelongsProject: springcloud-alibaba-nacos
* @BelongsPackage: com.bruce.controller
* @CreateTime: 2021-02-18 15:19
* @Description: TODO
*/
@RestController
@Slf4j
public class OrderController {
@Autowired
private OrderFeign orderFeign;
@GetMapping("/getProduct/{productId}")
public String getProduct(@PathVariable("productId") String id){
String response = orderFeign.getProductInfo(id);
log.info("调用服务结束: "+ response);
return response;
}
}
クラスOrderConsumerAppを開始します
1. @SpringCloudApplication:結合されたアノテーション:
@SpringBootApplication(启动SPringBoot程序)、
@EnableDiscoveryClient(开启服务注册与发现)、
@EnableCircuitBreaker(开启断路器)
2. @EnableFeignClients:Feignクライアントを有効にします
package com.bruce;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @BelongsProject: springcloud-alibaba-nacos
* @BelongsPackage: com.bruce
* @CreateTime: 2021-02-18 13:16
* @Description: TODO
*/
//@SpringBootApplication
@SpringCloudApplication
@EnableFeignClients
public class AppConsumer {
public static void main(String[] args) {
SpringApplication.run(AppConsumer.class);
}
}
5.テスト
アドレス:http://127.0.0.1:8710 / getProduct / 10