zoukankan      html  css  js  c++  java
  • Feign快速入门

    Feign是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign

    其作用:帮助我们优雅的实现http请求的发送

    Feign的使用步骤

    添加依赖

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>

    开启Feign

    @SpringBootApplication
    @MapperScan("com.marw.mapper")
    @EnableFeignClients
    public class OrderServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(OrderServiceApplication.class, args);
        }
    }

    编写Feign客户端(基于SpringMVC的注解来声明远程调用的信息)

    @FeignClient("userservice") //服务名称:userservice
    public interface UserClient {
        @GetMapping("/user/{id}") //请求方式:GET、请求路径:/user/{id}、请求参数:Long id
        User findById(@PathVariable("id") Long id); //返回值类型:User
    }

    远程调用的信息

    • 服务名称:userservice
    • 请求方式:GET
    • 请求路径:/user/{id}
    • 请求参数:Long id
    • 返回值类型:User

    Feign客户端完成远程调用

        @Autowired
        private UserClient userClient; //依赖注入Feign客户端
    
        public Order queryById(Long id){
            Order order = orderMapper.findById(id);
            User user = userClient.findById(order.getUserId()); // 完成远程调用
            order.setUser(user);
            return order;
        }

    查看Feign依赖,其内容实现了负载均衡

  • 相关阅读:
    Django视图之ORM数据库查询操作API
    Django视图之ORM更改数据库连接——配置MySQL库
    TLPI读书笔记第2章-基本概念2
    TLPI读书笔记第2章-基本概念1
    linux防火墙常用命令
    linux配置yum软件仓库
    linux进程管理
    linux文件权限
    linux用yum安装软件
    linux磁盘划分
  • 原文地址:https://www.cnblogs.com/WarBlog/p/15419369.html
Copyright © 2011-2022 走看看