SpringCloud集成OpenFeign

06-23 1324阅读

一、简介

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

SpringCloud集成OpenFeign
(图片来源网络,侵删)

二、SpringCloud集成OpenFeign

版本说明:
Spring Cloud Version:Hoxton.SR12
Spring Boot Version:2.3.12.RELEASE

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

1、引入依赖


    org.springframework.cloud
    spring-cloud-starter-openfeign

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

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

3、声明Feign接口

/**
 * 用户 远程服务
 */
      
//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接口

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

5、日志配置(OpenFeign)

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

6、OpenFeign超时配置

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

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

7、OpenFeign拦截器

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

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

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);
        }
    }
}

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

feign:
  client:
    config:
      # 默认配置 如果不单独配置每个服务会走默认配置
      default:
        request-interceptors:
          - com.config.MyHeaderInterceptor 
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]