• 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依赖,其内容实现了负载均衡

  • 相关阅读:
    Kubernetes
    Kubernetes
    Kubernetes
    Kubernetes
    Kubernetes
    代价函数——二次代价函数、交叉熵(cross-entropy)、对数似然(log-likelihood cost)(04-1)
    MNIST手写数字分类simple版(03-2)
    tensorflow非线性回归(03-1)
    python三目运算符
    tensorflow版helloworld---拟合线性函数的k和b(02-4)
  • 原文地址:https://www.cnblogs.com/WarBlog/p/15419369.html
Copyright © 2020-2023  润新知