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.完事,自己验证一下

使用idea从零编写SpringCloud项目-Feign的更多相关文章

  1. 从零构建Java项目(Maven+SpringBoot+Git) #02 奥斯丁项目

    前两天我说要写个项目来持续迭代,有好多小伙伴都表示支持和鼓励,项目的第一篇这不就来了么~我给项目取了个名字,英文名叫做:austin,中文名叫做:奥斯丁 名字倒没有什么特别的含义,我单纯觉得这个名字好 ...

  2. 从零搭建一个SpringCloud项目之Feign搭建

    从零搭建一个SpringCloud项目之Feign搭建 工程简述 目的:实现trade服务通过feign调用user服务的功能.因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要 ...

  3. SpringCloud——Eureka Feign Ribbon Hystrix Zuul等关键组件的学习与记录

    SpringCloud--Eureka Feign Ribbon Hystrix Zuul等关键组件的学习与记录 前言:本篇是对近日学习狂神SpringCloud教程之后的感想和总结,鉴于对Sprin ...

  4. CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL

    CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL +BIT祝威+悄悄在此留下版了个权的信息说: 开始 本文用step by step的方式,讲述如何使 ...

  5. SpringCloud 在Feign上使用Hystrix(断路由)

    SpringCloud  在Feign上使用Hystrix(断路由) 第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中 ...

  6. SpringCloud(5)---Feign服务调用

    SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...

  7. idea创建springcloud项目图文教程(EurekaServer注册中心)

    http://blog.csdn.net/hcmony/article/details/77854999 idea创建springcloud项目图文教程(EurekaServer注册中心)(六) 1, ...

  8. SpringCloud使用Feign出现java.lang.ClassNotFoundException: org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory异常

    废话不多说!!! 在SpringCloud项目中配置了Feign来调用restful接口,项目启动的时候报错,报错信息如下: 找不到org.springframework.cloud.client.l ...

  9. springcloud 之 feign的重复性调用 优化

    最近有一个springcloud的feign请求,用于获取坐标经纬度的信息,返回结果永远是固定不变的,所以考虑优化一下,不然每次转换几个坐标都要去请求feign,返回的所有坐标信息,数据量太大导致耗时 ...

  10. SpringCloud之Feign负载均衡(四)

    整合Feign pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <arti ...

随机推荐

  1. iOS MacOS 系统时间(时间戳)格式化

    #pragma mark -原始数据是20220608155116,加工成2022/06/08 15:51:16 -(NSString *)timeString:(NSString *)toIndex ...

  2. accept 类型列表

    *.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video *.ac3 audio/ac3 AC3 Audio *.asf allpication/vnd.ms-as ...

  3. https代理服务器(四)java动态签发【失败】

    https://zhuanlan.zhihu.com/p/355241710?utm_id=0 http://t.zoukankan.com/xiaxj-p-8961131.html https:// ...

  4. SAP abap外部断点

    如图所示:

  5. Vulnhub 靶场 DIGITALWORLD.LOCAL: SNAKEOIL

    Vulnhub 靶场 DIGITALWORLD.LOCAL: SNAKEOIL 前期准备 靶机地址:https://www.vulnhub.com/entry/digitalworldlocal-sn ...

  6. HiveServer2启动报TezConfiguration类NoClassDefFoundError错误

    错误信息如下: 2021-01-03 20:11:26,355 WARN [main] server.HiveServer2: Error starting HiveServer2 on attemp ...

  7. file类型的input框获取文件

  8. linux 安装 talib 的完美姿势!

    安装 TA-Lib $ wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz $ tar -zxvf ta-li ...

  9. 看K线学炒股(8.9)

    今天是食品饮料类题材大涨的一天,相应板块涨了6个多点,看着真是诱人.我以前关注的两只股票:海天味业和三全食品,今天都大涨.三全食品接近涨停.这种票容易选出来但也很难拿住.比如前些天买入了,结果8.5的 ...

  10. 2357. 使数组中所有元素都等于零 (Easy)

    问题描述 2357. 使数组中所有元素都等于零 (Easy) 给你一个非负整数数组 nums .在一步操作中,你必须: 选出一个正整数 x , x 需要小于或等于 nums 中 最小 的 非零 元素. ...