OpenFeign
# OpenFeign
# 概念
OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求,用法其实就是编写一个接口,在接口上添加注解即可。
可以简单理解它是借鉴Ribbon的基础之上,封装的一套服务接口+注解的方式的远程调用器。
它的宗旨是在编写Java Http客户端接口的时候变得更加容易,其底层整合了Ribbon,所以也支持负载均衡。
之前我们使用Ribbon的时候,利用RestTemplate对Http请求进行封装处理,但是在实际开发中,由于对服务依赖的调用不可能就一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以OpenFeign在此基础之上做了进一步的封装,由它来帮助我们定义和实现依赖服务接口的定义,我们只需创建一个接口并使用注解的方式来配置它,即可完成对微服务提供方的接口绑定,简化Ribbon的操作。
# 操作使用
创建工程openfeign-consumer-8888
pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>top.damoncai</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
YML配置
server:
port: 8888
spring:
application:
name: nacos-consumer-openFeign
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: '*'
2
3
4
5
6
7
8
9
10
11
12
13
14
15
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//添加此注解
public class OpenFeignConsumer8888App {
public static void main(String[] args) {
SpringApplication.run(OpenFeignConsumer8888App.class, args);
}
}
2
3
4
5
6
7
8
服务提供者对外提供接口
@Service
@FeignClient("nacos-provider")
public interface OpenFeignService {
@GetMapping("info/{id}")
public JsonResult<String> msbSql(@PathVariable("id") Long id);
}
2
3
4
5
6
7
测试类
@RestController
public class DemoController {
@Autowired
private OpenFeignService openFeignService;
@GetMapping("getInfo/{id}")
public JsonResult<String> getInfo(@PathVariable("id") Long id){
return openFeignService.msbSql(id);
}
}
2
3
4
5
6
7
8
9
10
11
测试
http://localhost:8888/getInfo/1
# OpenFeign超时时间控制
OpenFeign 客户端默认等待1秒钟,但是如果服务端业务超过1秒,则会报错。为了避免这样的情况,我们需要设置feign客户端的超时控制。
# 解决办法
由于OpenFeign 底层是ribbon 。所以超时控制由ribbon来控制。在yml文件中配置
# 案例
首先演示超时效果,我们现在9003/9004上设置一个延迟3秒执行的方法,来模仿长业务线调用。
@GetMapping("/timeOut")
public String timeOut() {
try {
System.out.println("延迟响应");
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return serverPort;
}
2
3
4
5
6
7
8
9
10
客户端8888通过OpenFeign来进行调用
客户端报错
设置超时控制案例演示
首先我们需要在8888消费者端的yml文件中配置超时时间,因为OpenFeign本身整合了Ribbon所以,这里其实我们用的是Ribbon来配置
server:
port: 8888
spring:
application:
name: nacos-consumer-openfegin
cloud:
nacos:
discovery:
server-addr: localhost:8848
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接后从服务器读取到可用资源所用的时间
ReadTimeout: 5000
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ConnectTimeout: 5000
management:
endpoints:
web:
exposure:
include: '*'
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
正常响应
# OpenFeign日志打印
Feign 提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节。 简单理解,就是对Feign接口的调用情况进行监控和输出
日志级别:
- NONE:默认的,不显示任何日志;
- BASIC:仅记录请求方法、URL、响应状态码及执行时间;
- HEADERS:除了 BASIC 中定义的信息之外,还有请求和响应的头信息;
- FULL:除了 HEADERS 中定义的信息之外,还有请求和响应的正文及元数据。
# 具体使用
需要在启动类中通过@Bean注解注入OpenFeign的日志功能
@SpringBootApplication
@EnableFeignClients
public class CloudalibabaOpenFeginConsumer8888Application {
public static void main(String[] args) {
SpringApplication.run(CloudalibabaOpenFeginConsumer8888Application.class, args);
}
@Bean
Logger.Level feignLoggerLevel(){
//开启详细日志
return Logger.Level.FULL;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
在yml中配置中配置
server:
port: 8888
spring:
application:
name: nacos-consumer-openfegin
cloud:
nacos:
discovery:
server-addr: localhost:8848
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
logging:
level:
# openfeign日志以什么级别监控哪个接口
top.damoncai.cloudalibabaopenfeginconsumer8888.service.OpenFeginService: debug
management:
endpoints:
web:
exposure:
include: '*'
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
测试效果,发起一次调用以后的日志内容:
# Sentinel整合OpenFegin
在当前的8084项目中引入对应的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2
3
4
激活Sentinel对OpenFeign的支持,所以配置yml
# 激活Sentinel对OpenFeign的支持
feign:
sentinel:
enabled: true
2
3
4
主启动类要添加@EnableFeignClients注解
OpenFegin接口编写
这里我们的接口写法和之前保持一致,但是要注意,我们这里要多增加一个FeignClient的属性:
- fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
//当没有成功调用/info/{id}接口时会走fallback属性标注的类型的处理方法
@Service
@FeignClient(value = "nacos-provider",fallback = FeignServiceImpl.class)
public interface FeignService {
/**
* 远程调用对应方法
*/
@GetMapping("info/{id}")
public JsonResult<String> msbSql(@PathVariable("id") Long id);
}
2
3
4
5
6
7
8
9
10
实现类必须添加@Component注解,否则无法注入到容器中
@Component
public class FeignServiceImpl implements FeignService{
@Override
public JsonResult<String> msbSql(Long id) {
return new JsonResult<>(444,"服务降级返回!");
}
}
2
3
4
5
6
7
编写控制器
@Autowired
private FeignService feignService;
@GetMapping("getInfo/{id}")
public JsonResult<String> getInfo(@PathVariable("id") Long id){
if(id > 3){
throw new RuntimeException("没有该id");
}
return feignService.msbSql(id);
}
2
3
4
5
6
7
8
9
10
访问http://localhost:8084/getInfo/1的地址
断开服务以后