概述:Feign是一个声明式WebService客户端、使用Feign能让编写WebSevice客户端更加简单。
它的使用方法是定义一个服务接口然后在上面添加注解。Feign也支持可拔插式的编码器和解码器。springcloud对feign进行了封装,使其支持了springmvc标准注解和JttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡
feign旨在使编写javaHttp客户端变得更容易
在使用ribbon+resttemplate时,利用resttemplate对http请求的封装处理,形成了一套模板化的调用方法,但在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用,所以,feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口定义,在feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是dao接口上面标注mapper注解,现在是一个微服务接口上面标注一个feign注解即可),即可完成对服务提供方的接口绑定,简化了使用springcloud ribbon时,自动封装服务调用客户端开发量。
feign集成了ribbon:利用ribbon维护的服务列表信息,并通过轮询实现了客户端的负载均衡,而与ribbon不同的是,通过feign只需要定义服务绑定接口且以声明的方法,优雅而简单的实现了服务调用
实际操作
pom文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</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-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>com.gwh</groupId>
<artifactId>common-api-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
yml文件配置
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://server2:7002/eureka,http://server1:7001/eureka,http://server3:7003/eureka
启动类:开启注解EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class OpenFeign {
public static void main(String[] args) {
SpringApplication.run(OpenFeign.class,args);
}
}
feign配合接口使用
@Component
@FeignClient(value = "CLOUD-SERVICE")//微服务名称
public interface IPayService {
@GetMapping(value = "/payment/get/{id}")//服务提供方的映射方法
CommonResoult<Payment> getPaymentById(@PathVariable("id") Long id);
}
controller调用
@RestController
@Slf4j
public class OpenClient {
@Resource
private IPayService payService;
@GetMapping(value = "/consumer/pay/get/{id}")
public CommonResoult<Payment> getDataById(@PathVariable("id") Long id){
return payService.getPaymentById(id);
}
}
openfeign超时控制
openfeign默认等待1秒钟,超时报错
若需要设置超时时间可以再配置中设置超时时间
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://server2:7002/eureka,http://server1:7001/eureka,http://server3:7003/eureka
#设置feign客户端超时时间(openfeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用时间
ConnectTimeout: 5000
openfeign日志配置
日志配置类
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author gwh
* @description
* @time 2021/5/7 16:57
*/
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
yml文件开启
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://server2:7002/eureka,http://server1:7001/eureka,http://server3:7003/eureka
#设置feign客户端超时时间(openfeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用时间
ConnectTimeout: 5000
logging:
level:
#feign日志以什么级别监控哪个接口
com.gwh.cloud.service.IPayService: debug