SpringCloud集成OpenFeign

一、简介

OpenFeign客户端是一个web声明式http远程调用工具,直接可以根据服务名称去注册中心拿到指定的服务IP集合,提供了接口和注解方式进行调用,内嵌集成了Ribbon本地负载均衡器。

二、SpringCloud集成OpenFeign

java 复制代码
版本说明:
Spring Cloud Version:Hoxton.SR12
Spring Boot Version:2.3.12.RELEASE

不同版本源码可能会有差异,具体参考版本对照

1、引入依赖

java 复制代码
<!--openfeign-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、在启动类或者配置类上加@EnableFeignClients注解

java 复制代码
@SpringBootApplication
@EnableFeignClients
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3、声明Feign接口

java 复制代码
/**
 * 用户 远程服务
 */
      
//1.1、如果是注册到nacos服务,则填写nacos服务名称,contextId:唯一标识
@FeignClient(value = "user",contextId = "userInfoClient01")
public interface UserFeignApi{
    /** 获取用户信息 */
    @GetMapping("/user/info")
    User getInfo(Long id);
}

//2、如果调用第三方接口,直接通过url调用 (value服务名,url远程调用地址,path统一前缀)
@FeignClient(value = "自定义服务名",url = "http://127.0.0.1:8080",path = "/user")
public interface UserFeignApi{
    /** 获取用户信息 */
    @GetMapping("/info")
    User getInfo(Long id);
}

相同服务名的坑:在使用了Feign的SpringCloud项目启动时,当存在多个FeignClient接口且name值相同 ,就会报错: A bean with that name has already been defined and overriding is disabled。这是因为SpringBoot会为每个FeginClient创建Bean,但当name值相同时SpringBoot会认为是有两个相同名字的Bean需要被创建,由此抛出异常。给每个@FeignClient都设置自己唯一的contextId

4、测试feign接口

java 复制代码
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private UserFeignApi userFeignApi;
 
    @GetMapping("/getInfo")
    public User getInfo(Long id) {
        return userFeignApi.getInfo(id);
    }

5、日志配置(OpenFeign)

java 复制代码
feign:  
  client:
    config: 
      default: 
        loggerLevel: FULL  

# 这里用default就是全局配置,如果是写服务名称user或者唯一标识contextId,则是针对某个微服务的配置
# 日志级别 NONE:默认不打印  BASIC:打印简单信息 HEADERS:打印头信息 FULL:打印全部信息(默认 NONE)

6、OpenFeign超时配置

使用配置文件配置是最推荐的,也是在项目中使用最多的。

java 复制代码
feign:
  client:
    config:
      default:   # 默认全局配置,也可配置单独FeignClient,default替换对应contextId
        connectTimeout: 2000 # 连接超时时间 默认值:10000毫秒
        readTimeout: 5000 # 读取超时时间 默认值:60000毫秒

7、OpenFeign拦截器

拦截器是OpenFeign可用的一种强大的工具,它可以被用来在请求和响应前后进行一些额外的处理

1、通过OpenFeign请求拦截器设置统一请求头

java 复制代码
public class MyHeaderInterceptor implements RequestInterceptor {
    private static String headerName = "token";
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 在这里添加额外的处理逻辑,添加请求头
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes instanceof ServletRequestAttributes) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = attributes.getRequest();
            String value = request.getHeader(headerName);
            requestTemplate.header(headerName, value);
        }
    }
}

在配置文件中添加拦截器配置

java 复制代码
feign:
  client:
    config:
      # 默认配置 如果不单独配置每个服务会走默认配置
      default:
        request-interceptors:
          - com.config.MyHeaderInterceptor 
相关推荐
涡能增压发动积1 天前
同样的代码循环 10次正常 循环 100次就抛异常?自定义 Comparator 的 bug 让我丢尽颜面
后端
云烟成雨TD1 天前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
Wenweno0o1 天前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
swg3213211 天前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
tyung1 天前
一个 main.go 搞定协作白板:你画一笔,全世界都看见
后端·go
gelald1 天前
SpringBoot - 自动配置原理
java·spring boot·后端
Java成神之路-1 天前
SpringMVC 响应实战指南:页面、文本、JSON 返回全流程(Spring系列13)
java·spring·json
殷紫川1 天前
深入拆解 Java 内存模型:从原子性、可见性到有序性,彻底搞懂 happen-before 规则
java·后端
元宝骑士1 天前
FIND_IN_SET使用指南:场景、优缺点与MySQL优化策略
后端·mysql
用户31952370347711 天前
记一次 PostgreSQL WAL 日志撑爆磁盘的排查
后端