• http客户端Feign


    引入依赖


    		<dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-openfeignartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4

    启动类添加注解


    @EnableFeignClients
    @MapperScan("cn.zyw.order.mapper")
    @SpringBootApplication
    public class OrderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(OrderApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    编写FeignClient接口


    import cn.zyw.order.pojo.User;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    @FeignClient("userservice")
    public interface UserClient {
    
        @GetMapping("/user/{id}")
        User findById(@PathVariable("id") Long id);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    使用FeignClient接口


    @Service
    public class OrderService {
    
        @Autowired
        private OrderMapper orderMapper;
        
        @Autowired
        private UserClient userClient;
    
    
        public Order queryOrderById(Long orderId) {
            // 1.查询订单
            Order order = orderMapper.findById(orderId);
            // 2.用Feign远程调用
            User user = userClient.findById(order.getUserId());
            // 3. 封装user到Order
            order.setUser(user);
            // 4.返回
            return order;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    自定义配置


    配置Feign日志

    配置文件方式

    • 全局生效
    feign:
      client:
        config:
          default:     # 这里用default就是全局配置,
            logger-level: : basic
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 局部生效
    feign:
      client:
        config:
          userservice:     # userservice为服务名
            logger-level: : basic
    
    • 1
    • 2
    • 3
    • 4
    • 5

    代码方式

    声明一个Bean

    import feign.Logger;
    import org.springframework.context.annotation.Bean;
    
    public class FeignClientConfiguration {
        @Bean
        public Logger.Level feignLogLevel(){
            return Logger.Level.BASIC;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 全局生效
    @EnableFeignClients(defaultConfiguration = FeignClientConfiguration.class)
    
    • 1
    • 局部生效
    @FeignClient(value = "userservice", configuration = FeignClientConfiguration.class)
    
    • 1

    性能优化


    引入依赖

    		<dependency>
                <groupId>io.github.openfeigngroupId>
                <artifactId>feign-httpclientartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4

    修改配置

    feign:
      httpclient:
        enabled: true   # 开启feign对httpclient的支持
        max-connections: 200    # 最大连接数
        max-connections-per-route: 50   # 每个路径的最大连接数
    
    • 1
    • 2
    • 3
    • 4
    • 5

    最佳实践


    • 创建feign-api子模块
    • 引入openfeign依赖
    • 将UserClient和pojo等移到该模块下

    其他模块引入feign-api依赖,@EnableFeignClients属性中增加basePackagesclients属性

     
            <dependency>
                <groupId>cn.zyw.demogroupId>
                <artifactId>feign-apiartifactId>
                <version>1.0version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    @EnableFeignClients(basePackages = "cn.zyw.feign.clients", defaultConfiguration = FeignClientConfiguration.class)
    
    • 1
    @EnableFeignClients(clients = {UserClient.class}, defaultConfiguration = FeignClientConfiguration.class)
    
    • 1
  • 相关阅读:
    2022Q3家电行业高增长细分市场分析(含热门品类数据)
    Springboot快递代拿系统43l40计算机毕业设计-课程设计-期末作业-毕设程序代做
    MySQL数据库——存储过程-游标(介绍-声明游标、打开游标、获取游标记录、关闭游标,案例)
    公链常用的共识算法
    微信小程序数据存储方式有哪些
    阿里云安全组 设置数据库仅自己电脑IP可登陆
    linux下磁盘分区和逻辑卷管理
    使用Hypothesis生成测试数据
    SpringCloud-Feign
    初级算法_字符串 --- 最长公共前缀
  • 原文地址:https://blog.csdn.net/zhyaw56zhu/article/details/136274040