Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
简而言之:
Feign 采用的是基于接口的注解
Feign 整合了ribbon,具有负载均衡的能力
整合了Hystrix,具有熔断的能力
方志朋:https://www.fangzhipeng.com/springcloud/2018/08/03/sc-f3-feign.html
1.准备工作
继续用上一节的工程, 启动eureka-server,端口为8761;
2.创建springboot,名字provider-order-feign-9003,在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-netflix-eureka-client,下面一样,“三板斧”
a.pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jason</groupId>
<artifactId>jason-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jason</groupId>
<artifactId>provider-order-feign-9003</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-order-feign-9003</name>
<description>微服务-order-Feign</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
b.配置。在工程的配置文件application文件配置如下
server.port=9003
#注册到eureka服务端的微服务名称
spring.application.name=provider-order
#注册到eureka服务端的地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#eureka.client.service-url.defaultZone: http://www.eureka9000.com:9000/eureka/
#点击具体的微服务,右下角是否显示ip
eureka.instance.prefer-ip-address=true
#显示微服务的名称
eureka.instance.instance-id=provider-order-9003
mybatis.configuration.map-underscore-to-camel-case=true
#配置数据库
spring.datasource.url=jdbc:mysql://localhost:3306/jason-cloud?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=admin
logging.level.com.jason.dao=debug
c.注解。在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能:
3.定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了provide-order服务的“/queryOrdersByUserId/{userId}”接口,代码如下:
在Web层的controller层,对外暴露一个”/queryOrdersByUserId/{userId}”的API接口,通过上面定义的Feign客户端OrderService来消费服务。代码如下:
4.启动服务,访问http://localhost:9003/order/queryOrdersByUserId/1
1.Feign添加日志
1.1 创建Feign的配置文件,并在其中设置日志等级
package com.jason.provider.order.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Jason
* @since 2020/6/2
*/
@Configuration
public class OrderClientConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.HEADERS;
}
}
1.2 在FeignClient接口中指定此配置
1.3 配置文件 application.properties 开启日志记录
## 将Feign接口的日志级别设置成DEBUG,因为Feign的logger.Level只对DEBUG做出响应
logging.level.com.jason.provider.order.service.OrderService=debug
访问接口 http://localhost:9003/order/queryOrdersByUserId/1,控制台打印请求详情日志
如果出现访问超时,是因为熔断器默认1秒未请求到结果即为超时,此时,面对网络环境较差情况,则需要调整超时时间。
2.Feign配置 Fallback
2.1 创建回调类 OrderClientFallback,OrderClientFallback回调实现,由spring创建使用@Component(其他的注册也可以)注解,并且实现OrderService,对 queryOrdersByUserId 方法回调时设置返回值。
package com.jason.provider.order.config;
import com.jason.provider.order.service.OrderService;
import com.jason.provider.order.vo.OrderVo;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jason
* @since 2020/6/3
*/
@Component
public class OrderClientFallback implements OrderService {
@Override
public List queryOrdersByUserId(String userId) {
return new ArrayList<>();
}
}
2.2 @FeignClient 配置 fallback
2.3 在配置中开启 hystrix,feign.hystrix.enabled=true
SpringCloud第二篇:Ribbon(F版本)