zoukankan      html  css  js  c++  java
  • 服务调用Feign入门

    1 前言

    • 前面我们使用的RestTemplate实现REST API的调用,代码如下:
    package com.sunxiaping.order.controller;
    
    
    import com.sunxiaping.order.domain.Product;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RequestMapping(value = "/order")
    public class OrderController {
        @Autowired
        private RestTemplate restTemplate;
    
    
        @GetMapping(value = "/buy/{id}")
        public Product buy(@PathVariable(value = "id") Long id) {
            Product product = restTemplate.getForObject("http://localhost:9003/product/findById/" + id, Product.class);
            return product;
        }
    }
    
    • 由上面的代码可知,我们是使用字符串拼接的方式构造URL的,该URL只有一个参数。但是,在现实中,URL中往往含有多个参数,这个时候,我们如果还需要字符串拼接的方式构造URL,将会非常痛苦。

    2 Feign简介

    • Feign是Netflix公司开发的声明式、模板化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。
    • 1️⃣Feign可以帮助我们更加便捷、优雅的调用HTTP的API。
    • 2️⃣在SpringCloud中,使用Feign非常简单–创建一个接口,并在接口上添加一些注解,代码就完成了。
    • 3️⃣Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。
    • 4️⃣SpringCloud对Feign进行了增强,使得Feign支持了SpringMVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加简单。

    3 基于Feign的服务调用

    3.1 导入Feign的相关jar包的Maven坐标

    • 修改部分:
    <!-- 导入openFeign的依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
    • 完整部分:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>spring_cloud_demo</artifactId>
            <groupId>org.sunxiaping</groupId>
            <version>1.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>order-service-feign9005</artifactId>
    
        <dependencies>
            <!-- 导入openFeign的依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
        </dependencies>
    </project>
    

    3.2 在启动类上添加Feign的支持

    • 启动类:
    package com.sunxiaping.order;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    
    @SpringBootApplication
    @EnableFeignClients //添加对Feign的支持,@EnableFeignClients注解开启SpringCloudFeign的支持功能
    public class Order9005Application {
        public static void main(String[] args) {
            SpringApplication.run(Order9005Application.class, args);
        }
    }
    

    3.3 配置调用接口

    • ProductClientFeign.java
    package com.sunxiaping.order.feign;
    
    
    import com.sunxiaping.order.domain.Product;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    /**
     * 声明需要调用的微服务名称
     *
     * @FeignClient name: 服务提供者的名称
     */
    @FeignClient(name = "service-product")
    public interface ProductFeignClient {
        
        /**
         * 配置需要调用的微服务的接口
         *
         * @param id
         * @return
         */
        @GetMapping(value = "/product/findById/{id}")
        Product findById(@PathVariable(value = "id") Long id);
    }
    

    3.4 通过配置调用的接口调用远程微服务

    • OrderController.java
    package com.sunxiaping.order.controller;
    
    
    import com.sunxiaping.order.domain.Product;
    import com.sunxiaping.order.feign.ProductFeignClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping(value = "/order")
    public class OrderController {
    
        @Autowired
        private ProductFeignClient productFeignClient;
    
    
        @GetMapping(value = "/buy/{id}")
        public Product buy(@PathVariable(value = "id") Long id) {
    
            Product product = productFeignClient.findById(id);
    
            return product;
        }
    }
    

    3.5 测试

    基于Feign的服务调用测试

    4 Feign和Ribbon的联系

    • Ribbon是一个基于HTTP和TCP客户端的负载均衡工具。它可以在客户端配置RibbonServerList(服务端列表),使用HttpClient或者RestTemplate模拟HTTP请求,步骤相当繁琐。
    • Feign是在Ribbon的基础上进行了一次改进,是一个使用起来更加方便的HTTP客户端。采用接口的方式,只需要创建一个接口,然后在上面添加注解,将需要调用的其他服务的方法定义成抽象方法即可,不需要自己构建HTTP请求。然后就像调用自身工程的方法调用个,而感觉不到调用远程方法,使得编写客户端变得非常容易。

    5 负载均衡

    • Feign本身已经集成了Ribbon的依赖和自动配置,因此我们不需要额外的引入依赖,也不需要再注册RestTemplate对象。我们可以通过ribbon.xx来进行全局配置,或者通过服务名.ribbon.xx来对指定的服务配置。

    • 启动两个商品微服务,并将其注册到Eureka中,重新测试可以返现使用Ribbon的轮询策略进行负载均衡。

    Feign的负载均衡

  • 相关阅读:
    MBProgressHUD使用
    IOS AFNetworking
    UIView 注意问题
    IOS动画
    UIView 设置背景图片
    iOS UILabel圆角
    IOS项目删除Git
    ios开发者到真机测试
    使用Google的Gson实现对象和json字符串之间的转换
    Spring MVC异常处理
  • 原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/13764528.html
Copyright © 2011-2022 走看看