FeignClient使用
在使用Spring Cloud开发微服务应用时中,各个微服务服务提供者都是以HTTP接口的形式对外提供服务,因此服务消费者在调用服务提供者时,通过HTTP Client的方式访问。当然我们可以使用JDK原生的`URLConnection`、`Apache的Http Client`、`Netty的异步HTTP Client`, Spring的`RestTemplate`去实现服务间的调用。Spring Cloud对Fegin进行了增强,使Fegin支持了Spring MVC的注解,并整合了Ribbon和Eureka,从而让Fegin的使用更加方便(在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验)。
一、FeignClient工作原理
总结来说,Feign的源码实现的过程如下:
- 首先通过@EnableFeignCleints注解开启FeignCleint
- 根据Feign的规则实现接口,并加@FeignCleint注解
- 程序启动后,会进行包扫描,扫描所有的@ FeignCleint的注解的类,并将这些信息注入到ioc容器中
- 当接口的方法被调用,通过jdk的代理,来生成具体的RequesTemplate
- RequesTemplate在生成Request
- Request交给Client去处理,其中Client可以是HttpUrlConnection、HttpClient也可以是Okhttp
- 最后Client被封装到LoadBalanceClient类,这个类结合类Ribbon做到了负载均衡
工作原理参见:https://zhuanlan.zhihu.com/p/28593019
二、示例
FeignClient相当于Spring Cloud中的RPC,使用示例如下:
(1)Eureka-Server注册中心
application.yml配置如下:
#application.yml
server:
port: 1111
spring:
application:
name:eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
server-url:
defaultZone: http://localhost:${server.port}/eureka/
EurekaServerApplication配置如下:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args)
}
}
(2)Eureka-Producer配置
定义远程服务HelloController
@RestController
public class HelloController {
@GetMapping("/hello")
public String xxx(@RequstParam String name) {
return "hello" + name + ", I'm eureka producer service!";
}
}
Eureka-Client中application.yml配置
server:
port: 1112
spring:
application:
name: eureka-producer
eureka:
client:
server-url:
defaultZone: http://localhost:1111/eureka/
EurekaProducerApplication
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaProducerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProducerApplication.class,args)
}
}
(3)Eureka-Consumer配置
Controller层服务配置如下:
@RestController
public class ConsumerController {
@Autowired
HelloRemote helloRemote; @RequestMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
return helloRemote.hello(name);
}
}
HelloRemote配置
@FeignClient(name="eureka-producer")
public interface HelloRemote {
@RequstMapping("/hello")
String hello(@RequstParam(value="name") String name);
}
application.yml文件配置
server:
port: 1113
spring:
application:
name: eureka-consumer
eureka:
client:
server-url:
defaultZone: http://localhost:1111/eureka
EurekaConsumerApplication配置
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class,args)
}
}
参见:http://www.voidcn.com/article/p-kodllxvn-hr.html
http://xujin.org/sc/sc-fegin01/
http://www.cnblogs.com/jalja/p/7011439.html
http://www.jianshu.com/p/f908171b5025
http://spring-cloud.io/reference/feign/
FeignClient使用的更多相关文章
- SpringCloud @FeignClient的类注解@ReqestMapping无效报错:No message available","path":"/xxxx
最近在使用Feign组合微服务的时候发现在@FeignClient接口类上使用@ReqestMapping无效. 像下面的这个代码: @FeignClient("xxx") @Re ...
- FeignClient注解及参数
一.FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上 1 2 3 4 5 @FeignC ...
- @FeignClient
@FeignClient("APP-PROVIDER")public interface MyFeignClient { @RequestMapping(value = " ...
- FeignClient调用POST请求时查询参数被丢失的情况分析与处理
前言 本文没有详细介绍 FeignClient 的知识点,网上有很多优秀的文章介绍了 FeignCient 的知识点,在这里本人就不重复了,只是专注在这个问题点上. 查询参数丢失场景 业务描述: 业务 ...
- 基于FeignClient提供简单的用户查询服务
前言: 由于系统升级,之前的员工数据库(mongo库)被弃用,改为用python维护的mysql库,其他系统访问通过http请求,表结构对外不可见,其他系统之前对员工mongo库的依赖要解除.每套系统 ...
- spring cloud: Hystrix(六):feign的注解@FeignClient:fallbackFactory(类似于断容器)与fallback方法
fallbackFactory(类似于断容器)与fallback方法 feign的注解@FeignClient:fallbackFactory与fallback方法不能同时使用,这个两个方法其实都类似 ...
- spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix
spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix 首先application.yml / applicatoin.propreties的配置项:fe ...
- Feign二: @FeignClient 接口调用
在项目的启动文件加入:@EnableFeignClients 注解, import org.springframework.boot.SpringApplication; import org.spr ...
- Spring Cloud 使用 FeignClient 启动报错
我们首先来看一下报错信息 Description: Field businessFeignClient in com.ysc.service.BusinessConfigService require ...
随机推荐
- 攻击图生成工具mulval的安装和配置
https://doc.mbalib.com/view/8620168b35e50fbe9b005fee33c192ad.html http://www.bingbig.com/227.html
- 放弃WebView,使用Crosswalk做富文本编辑器
版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4100132.html 为什么放弃WebView Androi ...
- “教你如何玩转Web响应式布局” 的更多相关文章
“教你如何玩转Web响应式布局” 的更多相关文章 网址:http://www.360doc.com/relevant/641896074_more.shtml
- BZOJ2618 [Cqoi2006]凸多边形 凸包 计算几何
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2618 题意概括 给出多个凸包,求面积交. 题解 首先我们考虑两个凸包相交的情况. 例题:HDU16 ...
- Visual Studio 代码补全功能有时候会失效的原因
大部分时候失效是因为你的代码有的地方有错误!!
- 洛谷 p1434 滑雪【记忆化搜索】
<题目链接> Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- python的os模块总结
python的os模块总结 目录 常用方法和属性总结 文件操作 目录操作 常用方法和属性总结 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir(&quo ...
- 不一样的go语言-athens私仓安装
前言 本系列文章曾多次提及go的依赖管理,提到了私仓,构件系统等概念,也曾提及当前流行的go构件系统,如athens,jfrog artifactory.鉴于jfrog的收费特性,本文只选择ath ...
- C#:几种数据库的大数据批量插入(转)
在之前只知道SqlServer支持数据批量插入,殊不知道Oracle.SQLite和MySql也是支持的,不过Oracle需要使用Orace.DataAccess驱动,今天就贴出几种数据库的批量插入解 ...
- manjaro 配置 独立显卡驱动
参考 https://blog.csdn.net/weixin_42205310/article/details/81905293 尝试多次 只有这篇配置成功. ①先解决依赖sudo pacman - ...