赞
踩
GitHub - OpenFeign/feign: Feign makes writing java http clients easierFeign makes writing java http clients easier. Contribute to OpenFeign/feign development by creating an account on GitHub.https://github.com/OpenFeign/feignFeign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API,目的是让编写HTTP接口的客户端变得更简洁和直接。
在Spring Cloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。
Feign是一个声明式的Web服务客户端,让编写Web服务客户端变的更加容易。只需要创建一个接口并添加注解即可
优势:
Feign可以做到使用 HTTP 请求远程服务时就像调用本地方法一样的体验,开发者完全感知不到这是远程方法,更感知不到这是个 HTTP 请求,开发者无需关注与远程的交互细节,更无需关注分布式环境开发。
Feign 和 OpenFeign 的区别(精简记忆版)?
1.支持spring mvc 注解
2.整合了更多的扩展 (请求重试策略、超时控制、请求拦截器)
准备工作:创建一个模块,用来提供服务:
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <dependency>
- <groupId>cn.hutool</groupId>
- <artifactId>hutool-all</artifactId>
- </dependency>
- server.port=8080
- spring.application.name=order-service-app
- spring.cloud.nacos.discovery.server-addr=192.168.11.91:8848
-
- /*
- * Copyright (c) 2020, 2024, All rights reserved.
- *
- */
- package com.by.controller;
-
- import cn.hutool.core.thread.ThreadUtil;
- import com.by.model.OrderQuery;
- import com.by.model.OrderingDTO;
- import org.springframework.web.bind.annotation.*;
-
- import java.math.BigDecimal;
-
- /**
- * <p>Project: spring-could-alibaba-root - OrderController</p>
- * <p>Powered by scl On 2024-04-25 11:26:56</p>
- * <p>描述:<p>
- *
- * @author 孙臣龙 [1846080280@qq.com]
- * @version 1.0
- * @since 17
- */
- @RestController
- @RequestMapping("/api/order")
- public class OrderController {
-
-
- @GetMapping("/ordering")
- public String ordering() throws InterruptedException {
- //睡眠15s用来验证请求处理超时时间
- ThreadUtil.safeSleep(15000);
- return "下单成功";
- }
-
- @GetMapping("/ordering1")
- public OrderingDTO ordering1() {
- OrderingDTO orderingDTO = OrderingDTO.builder().id(10086L).needPay(BigDecimal.valueOf(100)).build();
- return orderingDTO;
- }
-
- @PostMapping("/ordering2")
- public OrderingDTO ordering2(@RequestBody OrderingDTO orderingDTO) {
- return orderingDTO;
- }
-
- @GetMapping("/select")
- public String select(OrderQuery orderingDTO) {
- String msg="";
- if (orderingDTO.getId()!=null){
- msg="根据id查询成功,id:"+orderingDTO.getId();
- }
- if (orderingDTO.getName()!=null){
- msg="根据name查询成功,name:"+orderingDTO.getName();
- }
- return msg;
- }
- }
服务调用方操作下列内容:
- <!-- openfeign 远程调用 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
- /*
- * Copyright (c) 2020, 2024, All rights reserved.
- *
- */
- package com.by.feign;
-
- import com.by.config.FeignConfig;
- import com.by.model.OrderQuery;
- import com.by.model.OrderingDTO;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.cloud.openfeign.SpringQueryMap;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- /**
- * <p>Project: spring-could-alibaba-root - UserService</p>
- * <p>Powered by scl On 2024-04-25 15:57:48</p>
- * <p>描述:<p>
- *
- * @author 孙臣龙 [1846080280@qq.com]
- * @version 1.0
- * @since 17
- */
- //@FeignClient(name = "order-service-app",configuration = FeignConfig.class)
- @FeignClient(name = "order-service-app")
- @RequestMapping("/api/order")
- public interface UserService {
-
- // @GetMapping("/api/order/ordering")
- @GetMapping("/ordering")
- String ordering();
-
-
- @GetMapping("/ordering1")
- OrderingDTO ordering1();
-
-
- @PostMapping("/ordering2")
- OrderingDTO ordering2(@RequestBody OrderingDTO orderingDTO);
-
- @GetMapping("/select")
- String select(@SpringQueryMap OrderQuery orderingDTO);
-
-
- }
- package com.by;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.openfeign.EnableFeignClients;
-
- @SpringBootApplication
- @EnableFeignClients
- public class UserService01Application {
-
- public static void main(String[] args) {
- SpringApplication.run(UserService01Application.class, args);
- }
-
- }
- /*
- * Copyright (c) 2020, 2024, All rights reserved.
- *
- */
- package com.by.controller;
-
- import com.by.feign.UserService;
- import com.by.model.OrderQuery;
- import com.by.model.OrderingDTO;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- import java.math.BigDecimal;
-
- /**
- * <p>Project: spring-could-alibaba-root - UserController</p>
- * <p>Powered by scl On 2024-04-25 15:59:27</p>
- * <p>描述:<p>
- *
- * @author 孙臣龙 [1846080280@qq.com]
- * @version 1.0
- * @since 17
- */
- @RestController
- @RequestMapping("/api/user")
- public class UserController {
-
- @Autowired
- private UserService orderService;
- @Autowired
- private RestTemplate restTemplate;
-
- @GetMapping("/shopping")
- public String shopping() {
- // String forObject = restTemplate.getForObject("http://nacos-a/api/nacos-a/test", String.class);
-
- String ordering = orderService.ordering();
- return "";
- }
-
-
- @GetMapping("/shopping1")
- public String shopping1() {
- OrderingDTO orderingDTO = orderService.ordering1();
- return "";
- }
-
- @PostMapping("/shopping2")
- public String shopping2() {
- OrderingDTO orderingDTO1 = OrderingDTO.builder().id(1L).needPay(BigDecimal.valueOf(100)).build();
- OrderingDTO orderingDTO = orderService.ordering2(orderingDTO1);
- return "";
- }
-
-
- @GetMapping("/select")
- public String select() {
- //String select = orderService.select(OrderQuery.builder().id(1).build());
- String select = orderService.select(OrderQuery.builder().name("scl").build());
- return "";
- }
-
-
- }
- /*
- * Copyright (c) 2020, 2024, All rights reserved.
- *
- */
- package com.by.config;
-
- import feign.Logger;
- import feign.Request;
- import org.springframework.context.annotation.Configuration;
-
- /**
- * <p>Project: spring-could-alibaba-root - FeignConfig</p>
- * <p>Powered by scl On 2024-04-25 15:23:34</p>
- * <p>描述:<p>
- *
- * @author 孙臣龙 [1846080280@qq.com]
- * @version 1.0
- * @since 17
- */
- @Configuration
- public class FeignConfig {
-
- //配置日志
- //@Bean
- Logger.Level level() {
- return Logger.Level.FULL;
- }
-
- //配置超时时间
- // @Bean
- public Request.Options options() {
- return new Request.Options(5000, 10000);
- }
-
-
- }
- /*
- * Copyright (c) 2020, 2024, All rights reserved.
- *
- */
- package com.by.config;
-
- import org.springframework.cloud.client.loadbalancer.LoadBalanced;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
- import org.springframework.web.client.RestTemplate;
-
- /**
- * <p>Project: spring-could-alibaba-root - RestTemplateA</p>
- * <p>Powered by scl On 2024-04-23 11:23:50</p>
- * <p>描述:<p>
- *
- * @author 孙臣龙 [1846080280@qq.com]
- * @version 1.0
- * @since 17
- */
- @Configuration
- public class RestTemplateA {
-
- @Bean
- @LoadBalanced
- public RestTemplate restTemplate() {
- RestTemplate restTemplate = new RestTemplate();
- restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
- return restTemplate;
- }
- }
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @Builder
- public class OrderQuery {
- private Integer id;
- private String name;
- }
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @Builder
- public class OrderingDTO {
- private Long id;
- private BigDecimal needPay;
- }
Feign 提供了很多的扩展机制,让用户可以更加灵活的使用。
有时候我们遇到 Bug,比如接口调用失败、参数没收到等问题,或者想看看调用性能,就需要配置 Feign 的日志了,以此让 Feign 把请求信息输出来。
Feign 提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节。说白了就是对Feign接口的调用情况进行监控和输出
通过源码可以看到日志等级有 4 种,分别是:
NONE【性能最佳,适用于生产】:不记录任何日志(默认值)。
BASIC【适用于生产环境追踪问题】:仅记录请求方法、URL、响应状态代码以及执行时间。
HEADERS:记录BASIC级别的基础上,记录请求和响应的header。
FULL【比较适用于开发及测试环境定位问题】:记录请求和响应的header、body和元数据。
注意一:此处配置@Configuration注解就会全局生效,如果想指定对应某一个服务生效,就不能配置.
注意二: 因为feign调试日志是debug级别输出,springboot默认的日志级别是info,所以feign的debug日志级别就不会输出,一定要结合
logging.level.com.beiyou = debug
- @Configuration
- public class FeignConfig
- {
- @Bean
- Logger.Level feignLoggerLevel()
- {
- return Logger.Level.FULL;
- }
- }
局部配置,让调用的微服务生效,在@FeignClient 注解中指定使用的配置类
- logging.level.com.beiyou = debug开启日志
- ##配置feign 的日志级别
- #-- default 全局配置
- feign.client.config.default.loggerLevel=NONE
- #-- nacos-a 具体服务名
- feign.client.config.nacos-a.loggerLevel=FULL
通过 Options 可以配置连接超时时间和读取超时时间
- @Configurationpublic class FeignConfig {
- @Bean public Request.Options options() {
- return new Request.Options(10L, TimeUnit.SECONDS, 60L,TimeUnit.SECONDS,true);
- }}
Java
- #全局配置feign.client.config.default.connectTimeout=5000
- feign.client.config.default.readTimeout=10000
- 这里default 是一个特殊的客户端名称,用于表示全局配置。设置 connectTimeout 和 readTimeout 属性的值来定义全局的连接超时时间和读取超时时间。
- #局部配置 请将 <clientName> 替换为实际的Feign客户端名称。feign.client.config.<clientName>.connectTimeout=5000
- feign.client.config.<clientName>.readTimeout=10000
- feign.client.config.order-service.connectTimeout=5000 # 连接超时时间,默认10s order-service: #对应微服务feign.client.config.order-service.readTimeout=10000 # 请求处理超时时间,默认60s
-
补充说明: Feign的底层用的是Ribbon,但超时时间以Feign配置为准
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。