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. MAC 不带XIB新建ViewController

    - (void)loadView{ NSView *view = [[NSView alloc]init]; self.view = view; } MAC 开发的小伙伴

  2. node_modules修改?

    1.直接改node_modules的内容..... 但是下次npm i之后那个包的代码又恢复原状 2.独立维护需要改的包 把需要改的包复制下来,修改,推送到npm上. 项目里用新包即可,但是增加了维护 ...

  3. BIP查询框添加查询条件

    // 搜索框添加查询条件 viewModel.on("afterMount", function (data) { let agentId = viewModel.getParam ...

  4. error: the option `Z` is only accepted on the nightly compiler

    问题记录 $ cargo expand Checking helo v0.1.0 (/Users/Buzz/Documents/git/rust-lang/hello) error: the opti ...

  5. Odoo12 + Windows+Visual Studio Code环境安装

    参考 https://www.cnblogs.com/ecprodoo/p/13195748.html 1.要用odoo12需要安装以下几个软件 (1)Python 3.7, Python 3.8支持 ...

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

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

  7. win7安装AutoCAD2019

    1.Win7专业版64位先安装SP1补丁 2.根证书下载 MicrosoftRootCertificateAuthority2011.cer 链接:http://go.microsoft.com/fw ...

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

  9. 【vue】Vue-router

    Vue-router 安装 npm install vue-router --save-dev vue-cli中已经选择安装了vue-router,那这里不需要重复安装了 解读route 路径```s ...

  10. StatefulWidget 有状态组件 、 页面上绑定数据、改变页面数据

    一.Flutter 中自定义有状态组件 在 Flutter 中自定义组件其实就是一个类,这个类需要继承 StatelessWidget/StatefulWidget. StatelessWidget ...