1.前言
Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。
Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了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.初探Feign
微服务间的通讯是基于HTTP的,而Feign就是一个伪HTTP请求客户端,通过声明式的Feign客户端可以调用远程微服务,在实现远程微服务调用的过程中,多个实例存在的时候,Ribbon负载均衡器会默认的进行本地负载均衡,默认通过轮询机制进行微服务的调用,Feign默认则是集成了并且支持Ribbon负载均衡。
下面以订单服务(Order)调用商品服务(Product)为例,搭建环境使用Feign远程调用消费服务。
3.商品(Product)工程
pom.xml
springcloud-alibaba-nacos
com.bruce.springcloud-alibaba-nacos
1.0-SNAPSHOT
4.0.0
springcloud-alibaba-provider
org.springframework.boot
spring-boot-starter-web
spring-boot-starter-tomcat
org.springframework.boot
org.springframework.boot
spring-boot-starter-undertow
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.projectlombok
lombok
org.springframework.boot
spring-boot-maven-plugin
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.订单(Order)工程
pom.xml
springcloud-alibaba-nacos
com.bruce.springcloud-alibaba-nacos
1.0-SNAPSHOT
4.0.0
springcloud-alibaba-consumer
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
nacos-client
com.alibaba.nacos
com.alibaba.nacos
nacos-client
1.2.0
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-web
spring-boot-starter-tomcat
org.springframework.boot
org.springframework.boot
spring-boot-starter-undertow
org.projectlombok
lombok
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
6
application.yml
server:
port: 8710
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
enabled: true
application:
name: nacos-order
定义Feign客户端OrderFeign
OrderFeign 中引入了Hystrix熔断器组件起到服务的保护降级作用,@FeignClient注解当中使用断路器支持的有两种方式fallback 和fallbackFactory
关于Feign Hystrix Fallbacks:
第一种:Hystrix支持回退的概念:当它们的电路断开或出现错误时,将执行默认代码路径。要为给定@FeignClient集启用后备,该fallback属性应为实现后备的类名称。您还需要将实现声明为Spring bean。
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: 服务忙,稍后重试" ;
}
}
}
第二种: 如果需要访问引起后备触发器的原因,则可以使用fallbackFactory inside的属性@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 {
@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