在使用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使用的更多相关文章

  1. SpringCloud @FeignClient的类注解@ReqestMapping无效报错:No message available","path":"/xxxx

    最近在使用Feign组合微服务的时候发现在@FeignClient接口类上使用@ReqestMapping无效. 像下面的这个代码: @FeignClient("xxx") @Re ...

  2. FeignClient注解及参数

    一.FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上 1 2 3 4 5 @FeignC ...

  3. @FeignClient

    @FeignClient("APP-PROVIDER")public interface MyFeignClient { @RequestMapping(value = " ...

  4. FeignClient调用POST请求时查询参数被丢失的情况分析与处理

    前言 本文没有详细介绍 FeignClient 的知识点,网上有很多优秀的文章介绍了 FeignCient 的知识点,在这里本人就不重复了,只是专注在这个问题点上. 查询参数丢失场景 业务描述: 业务 ...

  5. 基于FeignClient提供简单的用户查询服务

    前言: 由于系统升级,之前的员工数据库(mongo库)被弃用,改为用python维护的mysql库,其他系统访问通过http请求,表结构对外不可见,其他系统之前对员工mongo库的依赖要解除.每套系统 ...

  6. spring cloud: Hystrix(六):feign的注解@FeignClient:fallbackFactory(类似于断容器)与fallback方法

    fallbackFactory(类似于断容器)与fallback方法 feign的注解@FeignClient:fallbackFactory与fallback方法不能同时使用,这个两个方法其实都类似 ...

  7. spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

    spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix 首先application.yml / applicatoin.propreties的配置项:fe ...

  8. Feign二: @FeignClient 接口调用

    在项目的启动文件加入:@EnableFeignClients 注解, import org.springframework.boot.SpringApplication; import org.spr ...

  9. Spring Cloud 使用 FeignClient 启动报错

    我们首先来看一下报错信息 Description: Field businessFeignClient in com.ysc.service.BusinessConfigService require ...

随机推荐

  1. ActiveMQ的应用实例

    一.部署和启动ActiveMQ 去官网下载:http://activemq.apache.org/ 我下载的是apache-activemq-5.12.0-bin.tar.gz, 解压到本地目录,进入 ...

  2. day17--JQuery

        JQuery(中文社区:http://jquery.cuishifeng.cn/) 模块  类库  在不同语言叫法不一样而已 DOM/BOM/JavaScript的类库 一.查找元素 JQue ...

  3. Codeforces 643C Levels and Regions 斜率优化dp

    Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...

  4. 安卓工作室 android studio 谷歌账号 登录

    作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com 登录你的谷歌账号,开始 添加 云功能 到你的 ...

  5. BZOJ.2034.[2009国家集训队]最大收益(二分图匹配 贪心)

    题目链接 双倍经验:BZOJ.4276.[ONTAK2015]Bajtman i Okrągły Robin(然而是个权限题.区间略有不同) \(Description\) 有\(n\)个任务,完成一 ...

  6. [POI2011]Inspekcja

    [POI2011]Inspekcja 题目大意: 给你一棵\(n(n\le10^6)\)个点的树,\(s\)为起点.每次选择一个点作为目标点\(t_i\),沿最短路走到\(t_i\)再走回\(s\)( ...

  7. 使用xlrd模块操作Excel

    table = data.sheets()[] table = data.sheet_by_index() print(table) #获取正行或是整列的值 con = table.row_value ...

  8. angular.js--------demo1

    <!doctype html><html ng-app> <head> <meta charset="utf-8"> </he ...

  9. Django拾遗--pagination、sitemap、admin、form

    Django拾遗--pagination.sitemap.admin.form pagination 其实这个分页模块的原理就是根据设定的每页条数来分割queryset.查询结果/每页子项数目=页数 ...

  10. C# 论接口的意义---共享协议

    我对接口的理解: 接口是一种协议.是一种模型. 我认为接口的意义: 接口更好的实现了项目资源共享 , 指定了可共享的范围 , 允许可使用而不可篡改的项目资源 . 我认为接口和模型是一类的: 接口一般与 ...