Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更简单
它的使用方法是定义一个服务接口然后在上面添加注解
。Feign也支持可拔插式的编码器和解码器。
Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。
Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
GitHub地址
:https://github.com/spring-cloud/spring-cloud-openfeign
Feign 和 OpenFeign的区别
微服务调用接口+@FeignClient
<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>cloud2020artifactId>
<groupId>com.qygroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-consumer-feign-order80artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>com.qygroupId>
<artifactId>cloud-api-commonsartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
server:
port: 80
spring:
application:
name: Order
#Eureka
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: false
# Eureka服务的地址 http://${eureka.instance.hostname}:${server.port}/eureka/
service-url:
defaultZone: http://eureka7001.com:7001/eureka
package com.qy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
// 启动Feign
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class,args);
}
}
package com.qy.springcloud.service;
import com.qy.springcloud.entities.CommonResult;
import com.qy.springcloud.entities.Payment;
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;
@Component
@FeignClient(value = "CLOUD-SERVICE-PAYMENT")
public interface OrderService {
/**
* 这里GetMapping里的映射地址写 提供服务模块 里边的controler层中的调用地址
*/
@GetMapping("/payment/get/{id}")
//@PathVariable("id") 这里写的时候一定要带上 ("id") 否则获取不到id这个参数
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
package com.qy.springcloud.controller;
import com.qy.springcloud.entities.CommonResult;
import com.qy.springcloud.entities.Payment;
import com.qy.springcloud.service.OrderService;
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;
@RestController
@Slf4j
public class OrderFeignController {
@Autowired
private OrderService orderService;
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
return orderService.getPaymentById(id);
}
}
访问:http://localhost/consumer/payment/get/3
总的来说,要使用OpenFeign的话,
一
: 在启动类上添加注解 @EnableFeignClients
二
: 在业务层上添加注解 @FeignClient(value = “CLOUD-SERVICE-PAYMENT”),并指明提供服务的name,也就是提供服务的yml文件中的spring.application.name
在业务层注意,@GetMapping("/payment/get/{id}")
要与提供服务的映射地址一致!
相当于,在业务层调用服务名为CLOUD-SERVICE-PAYMENT
的地址映射为@GetMapping("/payment/get/{id}")
的服务
三
:测试即可。(OpenFeign自带负载均衡)
Feign 替代了 Ribbon + RestTemplate
@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeout(){
try {
/*暂停3秒*/
TimeUnit.SECONDS.sleep(3);
}catch (Exception e){
e.printStackTrace();
}
return serverPort;
}
Service层:
@GetMapping("/payment/feign/timeout")
public String timeout();
Controller层:
@GetMapping("/consumer/payment/feign/timeout")
public String paymentFeignTimeout(){
//open-feign-ribbon 客户端一般默认等待1秒钟
return orderService.timeout();
}
在Order80的yml文件中添加配置
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
@Configuration
public class MyConfig {
@Bean
Logger.Level feignLoggerLevel(){
/*打印Full级别,FULL级别最全*/
return Logger.Level.FULL;
}
}
logging:
level:
#feign日志以什么级别监控哪个接口
com.qy.springcloud.service.OrderService: debug