在使用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. canvas放射粒子效果

    这个也是别人的代码,就不多介绍了 写了些注释 body { overflow:hidden; margin:0; padding:0; background-color:#222222 } </ ...

  2. python全栈开发day33-进程间的通信、进程间的数据共享,进程池

    一.昨日内容回顾: 1.  守护进程 1).p.saemon, 2 ).p.terminate 3 ).p.join 2.  同步控制 1).锁,Lock 互斥锁,解决数据安全.进程之间资源抢占问题. ...

  3. (转)HIBERNATE与 MYBATIS的对比

    第一方面:开发速度的对比 就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉得要用好Mybatis还是首先要先理解好H ...

  4. Python中 各种数字类型的判别(numerica, digital, decimal)

    一. 全角和半角 全角:是指一个全角字符占用两个标准字符(或两个半角字符)的位置. 全角占两个字节. 汉字字符和规定了全角的英文字符及国标GB2312-80中的图形符号和特殊字符都是全角字符.在全角中 ...

  5. 在vue项目中使用canvas-nest.js,报parameter 1 is not of type 'Element'

    canvas-nest.js是一款轻量的网页特效,如图: github地址:https://github.com/hustcc/canvas-nest.js 在普通的html项目中,只要将<sc ...

  6. 深度学习(TensorFlow)环境搭建:(二)Ubuntu16.04+1080Ti显卡驱动

    前几天把刚拿到了2台GPU机器组装好了,也写了篇硬件配置清单的文章——<深度学习(TensorFlow)环境搭建:(一)硬件选购和主机组装>.这两台也在安装Ubuntu 16.04和108 ...

  7. 基于URL的高层次Java网络编程

    一致资源定位器URL URL(Uniform Resource Locator)是一致资源定位器的简称,它表示Internet上某一资源的地址.通过URL我们可以访问Internet上的各种网络资源, ...

  8. VsVim - Shortcut Key (快捷键)

    Enable / Disable NuGet 中提供了禁用按钮.另外还可以通过 Ctrl+Shift+F12 在 Visual Studio 中实现 Enable / Disable. 移动光标类命令 ...

  9. 2016年3月10日Android实习日记

    待解决问题: *1:内部ScrollView与外部手势事件滑动冲突问题. *2:Linearlayout+View+LinearLayout横向排列,这其中两个LinearLayout内部各有3个竖向 ...

  10. Unterminated &lt;c:forEach tag

    c:forEach tag意思是这一块有语法错误