目录 1、Feign-简介2、spring-cloud快速整合OpenFeign3、Feign日志4、Feign契约配置5、Feign配置超时时间6、Feign拦截器7、Feign断路器
Feign是Neflix开发的声明式、模块化的Http客户端,集成了Ribbon、RestTemplate实现了负载均衡的执行Http调用,Feign可以帮助我们更加便捷、优雅的调用HTTP api。
spring cloud OpenFeign是对Feign的增强,使其支持spring mvc 注解,另外还整合了Ribbon和Nacos,从而是的Feign使用更加方便,有了feign我们就不用使用resttemplate远程调用了(调用Controller层)。
1)在调用服务中添加依赖
<!--添加openfeign依赖,依赖于sprin-cloud--><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency> 2)配置需要调用的接口
@FeignClient(name = "stock-service", path = "/stock")public interface StockFeignService { // 声明需要调用的接口方法名 @PostMapping("/reduct") String add(@RequestParam("name") String name);} 3)启动类加上注解
@EnableFeignClients 4)直接调用
@RestController@RequestMapping("/order")public class OrderController { @Autowired private StockFeignService stockFeignService; @GetMapping("/getOrder") public String getOrder(){ stockFeignService.add("10"); return "Hello world"; }} 注:使用feign需要在参数前面加上@RequestParam和@PathVariable注解并指定参数,不然获取不到参数。
1)feign日志级别:
2):配置feign日志:
package com.swp.config;import feign.Logger;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;// 全局配置: 使用@Configuration会全局配置到所有服务提供方(被调用方)// 局部配置: 如果只针对某个服务,就不要使用@Configuration,在@FeignClient(name = "stock-service", path = "/stock", configuration = FeignClient.class)中加入configuration = FeignClient.class@Configurationpublic class FeignConfig { @Bean public Logger.Level feignLogerLevel(){ return Logger.Level.FULL; }} yaml配置日志级别
#spring 默认日志级别是info,feign的debug日志不会输出,所以需要配置输出日志级别logging: level: com.swp.feign: debug #只输出feign目录下的日志 日志输出:

若以前使用的feign原生注解,在不想改变原生注解的情况下,可以使用Feign契约配置
feign:client:config:contract: feign.Contract.Default 全局配置:
@Configurationpublic class FeignConfig(){@Beanpublic Request.Options options(){return new Request.Options(5000,10000);}} yaml配置
feign:client:config:mall-order: #对应微服务# 连接超时时间。默认2sconnectTimeout: 5000# 请求处理超时时间,默认5sreadTimeout: 10000 在服务消费者调用服务提供者时触发。
Feign拦截器配置类
import feign.RequestInterceptor;import feign.RequestTemplate;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class CustomFeignInterceptor implements RequestInterceptor { Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public void apply(RequestTemplate requestTemplate) { requestTemplate.header("token", "123"); requestTemplate.query("name", "999"); // requestTemplate.uri("/111"); 拦截所有路径,并修改为/{xx} logger.info("feign拦截器"); }} 注入配置类
// 注入自定义feign拦截器 @Bean public CustomFeignInterceptor customFeignInterceptor(){ return new CustomFeignInterceptor(); }} #开启hysrrisfeign: hystrix: enabled: true 定义SysMgrHystrix ,实现FallbackFactory的create方法来提供服务降级实现类对象的创建,
ServiceSysMgrClient是自己的feign接口
@Componentpublic class SysMgrHystrix implements FallbackFactory<ServiceSysMgrClient> { @Override public ServiceSysMgrClient create(Throwable throwable) { return new ServiceSysMgrClient() { // 下面是feign接口 @Override public Result<List<Orgnode>> getOrGChildren(Integer parentOrgId) { return "被回滚了"; } }; }} 然后在Feign接口上面加上 fallbackFactory = SysMgrHystrix.class

来源地址:https://blog.csdn.net/weixin_44183847/article/details/128850109
--结束END--
本文标题: SpringBoot整合Feign
本文链接: https://www.lsjlt.com/news/407646.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0