• 【微服务|OpenFeign】OpenFeign快速入门|基于Feign的服务调用


    OpenFeign

    OpenFeign为微服务架构下服务之间的调用提供了解决方案,OpenFeign是一种声明式、模板化的HTTP客户端。

    在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。

    OpenFeign可以用来简化HTTP的调用,前面我们使用的RestTemplate实现REST API调用,代码大致如下:

    @GetMapping("/buy/{id}")
    public Product order() {
        Product product = restTemplate.getForObject("http://shop-serviceproduct/product/1", Product.class);
        return product; }
    
    • 1
    • 2
    • 3
    • 4

    由代码可知,我们是使用拼接字符串的方式构造URL的,该URL只有一个参数。但是,在现实中,URL中往往含有多个参数。这时候我们如果还用这种方式构造URL,那么就会非常痛苦。

    feign客户端调用的事项:

    1. 如果请求参数没有加上注解的话,默认采用post请求发送。
    2. 服务的名称命名不能够有下划线,只能使用中划线,否则会报下列的错。

    feign和opoenfeign

    1. 他们底层都是内置了Ribbon,去调用注册中心的服务。
    2. Feign是Netflix公司写的,是SpringCloud组件中的一个轻量级RESTful的HTTP服务客户端,是SpringCloud中的第一代负载均衡客户端。
    3. OpenFeign是SpringCloud自己研发的,在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等。是SpringCloud中的第二代负载均衡客户端。
    4. Feign本身不支持Spring MVC的注解,使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。
    5. OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

    Feign是Springcloud组件中的一个轻量级Restful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。

     <!--feign-->
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-feign</artifactId>
     </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    OpenFeign是springcloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

     <!--openfeign-->
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-openfeign</artifactId>
     </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    基于Feign的服务调用

    引入依赖

    在服务消费者 shop_service_order 添加Fegin依赖

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

    启动类添加Feign的支持

    /**
     * 消费者
     *
     * @author issavior
     */
    @EnableFeignClients("com.ossa.common.feignapi")
    @EnableDiscoveryClient
    @SpringBootApplication(scanBasePackages = "com.ossa")
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    通过@EnableFeignClients注解开启Spring Cloud Feign的支持功能

    启动类激活FeignClient

    创建一个Feign接口,此接口是在Feign中调用微服务的核心接口

    在服务消费者 shop_service_order 添加一个 ProductFeginClient 接口

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @author issavior
     */
    @FeignClient(value = "ossa-service-producer")
    @RequestMapping(value = "/producer")
    public interface ProducerFeign {
    
        /**
         * 根据ID查询商品
         *
         * @param id 商品的主键ID
         * @return 相关商品的信息
         */
        @GetMapping(value = "/{id}")
        String producerById(@PathVariable(value = "id") String id);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 定义各参数绑定时,@PathVariable、@RequestParam、@RequestHeader等可以指定参数属性,在Feign中绑定参数必须通过value属性来指明具体的参数名,不然会抛出异常
    • @FeignClient:注解通过name指定需要调用的微服务的名称,用于创建Ribbon的负载均衡器。
      所以Ribbon会把 ossa-service-producer 解析为注册中心的服务。

    配置请求提供者的调用接口

    修改 OrderController ,添加ProductFeginClient的自动注入,并在order方法中使用ProductFeginClient 完成微服务调用。

    @Autowired
    private ProducerFeign producerFeign;
    
    @GetMapping("/feign/{id}")
    public String feignConsumerById(@PathVariable(value = "id") String id) {
    
        return producerFeign.producerById(id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    力扣刷题 day43:10-13
    Kotlin 开发Android app(九):Android两大布局LinearLayout和RelativeLayout
    MySQL根据备注查询表、字段
    【Filament】基于物理的光照(PBR)
    Vue3中如何使用ref获取元素节点全面解析
    深度学习二三事-回顾那些经典卷积神经网络
    人人都是艺术家!AI工具Doodly让潦草手绘变精美画作
    Flutter:webview_flutter插件使用
    Caller 服务调用 - Dapr
    安全性证明
  • 原文地址:https://blog.csdn.net/CSDN_SAVIOR/article/details/125493986