zoukankan      html  css  js  c++  java
  • 使用idea从零编写SpringCloud项目-Feign

     ps:Fegin和Ribbon 其实是差不多的东西,Fegin里面也是集成了Ribbon,不过咱们写代码不是要优雅嘛,使用Feign就会优雅很多了,看着比直接使用Ribbon舒坦一点

     就不重新构建项目了,实在懒得动手可以从git上把我的代码拉去跑一跑,保证能运行。

     1.首先在order_server里面引入对Fegin的依赖

      

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

     2.在启动类中增加注解@EnableFeginClients

      

     3.可以开始编写咱们的Fegin代码了

    package net.xdclass.order_server.service;
    
    import net.xdclass.order_server.fallback.ProductClientFallback;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    /**
     * @date 2021/6/2 23:30
     */
    //写上服务提供方的服务名称
    @FeignClient(value = "product-service")
    public interface ProductClient {
      //请注意,参数与被调用方实际接口一致
        @GetMapping("/api/v1/product/findById")
        String findById(@RequestParam("id")int id);
    
    }

    4.然后在需要调用接口的地方,注入这个Bean,ProductClient 再调用方法。

    @Autowired
        private ProductClient productClient;

    5.然后调用方法获取返回值

    6.设置负载均衡策略,在配置文件中增加以下配置,下面将默认的轮询机制变更为随机机制

    product:
      ribbon:
        NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

    7.fegin内置了hystrix,使用方式如下

    feign:
      hystrix:
    #开启熔断器
        enabled: true
      client:
        config:
          default:
            #连接超时时间
            connectTimeout: 2000
          #返回超时时间
            readTimeout: 11000

    8.加上fallback = ProductClientFallback.class

    package net.xdclass.order_server.service;
    
    import net.xdclass.order_server.fallback.ProductClientFallback;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    /**
     * @author chengcheng123
     * @date 2021/6/2 23:30
     */
    @FeignClient(value = "product-service",fallback = ProductClientFallback.class)
    public interface ProductClient {
    
        @GetMapping("/api/v1/product/findById")
        String findById(@RequestParam("id")int id);
    
    }

    9.编写ProductClientFallback,继承使用fegin的productClient,然后将这个类用@component 标识为组件,交由IOC管理

    /**
     * @date 2021/6/3 23:33
     */
    @Component
    public class ProductClientFallback implements ProductClient {
        @Override
          public String findById(int id) {
            System.out.println("feign 调用product-service findById 异常");
            return null;
        }
    }

    10.完事,自己验证一下

    怕什么真理无穷 进一寸有一寸的欢喜
  • 相关阅读:
    Vector-Constructors
    C++:多维数组的动态分配(new)和释放(delete)
    C++:多维数组的动态分配(new)和释放(delete)
    COM_利用GetWallpaper()获取墙纸路径
    COM_利用GetWallpaper()获取墙纸路径
    COM 技术相关概念
    COM 技术相关概念
    全排列与next_permutation
    全排列与next_permutation
    屏蔽MFC程序中的ESC键和ENTER键关闭窗口
  • 原文地址:https://www.cnblogs.com/ccbk/p/14849689.html
Copyright © 2011-2022 走看看