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 
相关推荐
DKunYu13 分钟前
7.SpringCloudConfig
spring cloud·微服务
芒克芒克40 分钟前
本地部署SpringBoot项目
java·spring boot·spring
小突突突1 小时前
Spring框架中的单例bean是线程安全的吗?
java·后端·spring
iso少年1 小时前
Go 语言并发编程核心与用法
开发语言·后端·golang
掘金码甲哥1 小时前
云原生算力平台的架构解读
后端
码事漫谈1 小时前
智谱AI从清华实验室到“全球大模型第一股”的六年征程
后端
码事漫谈1 小时前
现代软件开发中常用架构的系统梳理与实践指南
后端
Mr.Entropy2 小时前
JdbcTemplate 性能好,但 Hibernate 生产力高。 如何选择?
java·后端·hibernate
sww_10262 小时前
Spring-AI和LangChain4j区别
java·人工智能·spring
YDS8292 小时前
SpringCloud —— MQ的可靠性保障和延迟消息
后端·spring·spring cloud·rabbitmq