5.1、写一个 Feign 害户端

新建项目:

依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-openfeign</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <scope>test</scope>
  21. </dependency>

关于服务中心使用的是8762(之前的案列写过)

地址:https://www.cnblogs.com/Mrchengs/p/10645911.html

 
配置文件:
  1. spring.application.name=feign
  2. server.port=
  3. eureka.client.service-url.defaultZone=http://localhost:8762/eureka/
 
主配置类:

@EnableFeignClients:开启Feign Client功能

  1. @EnableFeignClients(basePackages = "com.cr.eurekafeignclient.feign")
  2. @EnableDiscoveryClient
  3. @SpringBootApplication
  4. public class EurekaFeignClientApplication {
  5.  
  6. public static void main(String[] args) {
  7. SpringApplication.run(EurekaFeignClientApplication.class, args);
  8. }
  9. }
上述的三个步骤已经开启了Feign的功能
 
实现之前eureka client的/port服务的调用

EurekaClientFeign.java

  1. @FeignClient(value = "CLINET",configuration = feignconfig.class)
  2. public interface EurekaClientFeign {
  3. @GetMapping("/port")
  4. String port();
  5.  
  6. }
@FeignClient:
value:远程调用其他服务的服务名
feignconfig.class为Feign Client的配置类
port():通过Feign来调用CLIENT服务的“/port”的API接口

feignconfig.java

  1. @Configuration
  2. public class feignconfig {
  3.  
  4. @Bean
  5. public Retryer feignRetryer(){
  6. return new Retryer.Default(, TimeUnit.SECONDS.toMillis(1L),);
  7. }
  8. }
配置类:注入一个feignRetryer的Retryer的Bean
注入Bean之后,Feign在远程调用失败之后会进行重试
 

FeignService.java

  1. @Service
  2. public class FeignService {
  3.  
  4. @Autowired
  5. EurekaClientFeign eurekaClientFeign;
  6.  
  7. public String port(){
  8. return eurekaClientFeign.port();
  9. }
  10. }
在Service层FeignService注入EurekaClientFeign去调用port()方法

FeignController.java

  1. @RestController
  2. public class FeignController {
  3. @Autowired
  4. FeignService feignService;
  5.  
  6. @GetMapping("/feign")
  7. public String sayPort(){
  8. return feignService.port();
  9. }
  10. }
自动注入FeignService 去调用其port()方法
FeignService 通过 EurekaClientFeign远程调用CLIENT服务的API接口“/port”
此时的两个提供者:

分别是8089和8090端口
 
启动服务:

Feign Client 程调用了 eureka-client 务(8089、8090)两个端口的实例“/port”API接口
Feign Client 有负载均衡的能力。

spring-cloud-starter-openfeign 的porn 文件

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-openfeign-core</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-web</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-commons</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>io.github.openfeign</groupId>
  19. <artifactId>feign-core</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>io.github.openfeign</groupId>
  23. <artifactId>feign-slf4j</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>io.github.openfeign</groupId>
  27. <artifactId>feign-hystrix</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>io.github.openfeign</groupId>
  31. <artifactId>feign-java8</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  36. <optional>true</optional>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.cloud</groupId>
  40. <artifactId>spring-cloud-starter-netflix-archaius</artifactId>
  41. <optional>true</optional>
  42. </dependency>

5.2、FeignClient详解

@FeignClient注解源码

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5.  
  6. package org.springframework.cloud.openfeign;
  7.  
  8. import java.lang.annotation.Documented;
  9. import java.lang.annotation.ElementType;
  10. import java.lang.annotation.Retention;
  11. import java.lang.annotation.RetentionPolicy;
  12. import java.lang.annotation.Target;
  13. import org.springframework.core.annotation.AliasFor;
  14.  
  15. @Target({ElementType.TYPE})
  16. @Retention(RetentionPolicy.RUNTIME)
  17. @Documented
  18. public @interface FeignClient {
  19. @AliasFor("name")
  20. String value() default "";
  21.  
  22. /** @deprecated */
  23. @Deprecated
  24. String serviceId() default "";
  25.  
  26. String contextId() default "";
  27.  
  28. @AliasFor("value")
  29. String name() default "";
  30.  
  31. String qualifier() default "";
  32.  
  33. String url() default "";
  34.  
  35. boolean decode404() default false;
  36.  
  37. Class<?>[] configuration() default {};
  38.  
  39. Class<?> fallback() default void.class;
  40.  
  41. Class<?> fallbackFactory() default void.class;
  42.  
  43. String path() default "";
  44.  
  45. boolean primary() default true;
  46. }
FeignClient 注解被@Target(ElementType TYPE)修饰:
表示 FeignClient 注解的作用目标在接口上
 
@Retention(RetentionPolicy.RUNTll\伍)注解表明该注解会在 lass 字节码文件中存在,
在运行时可以通过反射获取到
 
@Documented 表示该注解将被包含在 Javadoc 中。
 
@Feign Client 注解用于创建声明式 API 接口,该接口是RESTful 风格的。
Feign 被设计成插拔式的,可以注入其他组件和 Feign一起使用
最典型的是如果 Ribbon 可用, Feign 会和Ribbon 结合进行负载均衡。
 
注解中的属性:
---value()和 name() 样,是被调用的服务的 Serviceld
---url ()直接填写硬编码的Uri 地址
---decode404()即 404 是被解码,还是抛异常。
---configuration ()指明 FeignClient 配置类,
    默认的配置类为 FeignClientsConfiguration类,在缺省的情况下 这个类注入
    了默认的 DecoderEncoder、 Contract 等配置的 Bean
---fall back()为配置熔断器的处理类。

5.3、FeignClient的配置

Feign Client 默认的配置类为 FeignClientsConfiguration,这个类在在 pring-cloud-netflix-core的jar下
现这个类注入了很多 Feign 相关的配置 Bean(默认不配置的时候使用默认的)
Decoder、Encoder 、Contract个类在没有Bean被注入的情况下,会自动注入默认配置的 Bean

  1. @Configuration
  2. public class FeignClientsConfiguration {
  3. @Autowired
  4. private ObjectFactory<HttpMessageConverters> messageConverters;
  5. @Autowired(
  6. required = false
  7. )
  8. private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList();
  9. @Autowired(
  10. required = false
  11. )
  12. private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList();
  13. @Autowired(
  14. required = false
  15. )
  16. private Logger logger;
  17.  
  18. public FeignClientsConfiguration() {
  19. }
  20.  
  21. @Bean
  22. @ConditionalOnMissingBean
  23. public Decoder feignDecoder() {
  24. return new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
  25. }
  26.  
  27. @Bean
  28. @ConditionalOnMissingBean
  29. @ConditionalOnMissingClass({"org.springframework.data.domain.Pageable"})
  30. public Encoder feignEncoder() {
  31. return new SpringEncoder(this.messageConverters);
  32. }
  33.  
  34. @Bean
  35. @ConditionalOnClass(
  36. name = {"org.springframework.data.domain.Pageable"}
  37. )
  38. @ConditionalOnMissingBean
  39. public Encoder feignEncoderPageable() {
  40. return new PageableSpringEncoder(new SpringEncoder(this.messageConverters));
  41. }
  42.  
  43. @Bean
  44. @ConditionalOnMissingBean
  45. public Contract feignContract(ConversionService feignConversionService) {
  46. return new SpringMvcContract(this.parameterProcessors, feignConversionService);
  47. }
  48.  
  49. @Bean
  50. public FormattingConversionService feignConversionService() {
  51. FormattingConversionService conversionService = new DefaultFormattingConversionService();
  52. Iterator var2 = this.feignFormatterRegistrars.iterator();
  53.  
  54. while(var2.hasNext()) {
  55. FeignFormatterRegistrar feignFormatterRegistrar = (FeignFormatterRegistrar)var2.next();
  56. feignFormatterRegistrar.registerFormatters(conversionService);
  57. }
  58.  
  59. return conversionService;
  60. }
  61.  
  62. @Bean
  63. @ConditionalOnMissingBean
  64. public Retryer feignRetryer() {
  65. return Retryer.NEVER_RETRY;
  66. }
  67.  
  68. @Bean
  69. @Scope("prototype")
  70. @ConditionalOnMissingBean
  71. public Builder feignBuilder(Retryer retryer) {
  72. return Feign.builder().retryer(retryer);
  73. }
  74.  
  75. @Bean
  76. @ConditionalOnMissingBean({FeignLoggerFactory.class})
  77. public FeignLoggerFactory feignLoggerFactory() {
  78. return new DefaultFeignLoggerFactory(this.logger);
  79. }
  80.  
  81. @Bean
  82. @ConditionalOnClass(
  83. name = {"org.springframework.data.domain.Page"}
  84. )
  85. public Module pageJacksonModule() {
  86. return new PageJacksonModule();
  87. }
  88.  
  89. @Configuration
  90. @ConditionalOnClass({HystrixCommand.class, HystrixFeign.class})
  91. protected static class HystrixFeignConfiguration {
  92. protected HystrixFeignConfiguration() {
  93. }
  94.  
  95. @Bean
  96. @Scope("prototype")
  97. @ConditionalOnMissingBean
  98. @ConditionalOnProperty(
  99. name = {"feign.hystrix.enabled"}
  100. )
  101. public Builder feignHystrixBuilder() {
  102. return HystrixFeign.builder();
  103. }
  104. }
  105. }
重写 FeignClientsConfiguration 类中的 Bean
覆盖掉默认的配置 Bean 
从而达到自定义配置的目的
 
Feign 默认的配置在请求失败后 重试次数为 ,即不重试Retryer.NEVER_RETRY
现在希望在请求失败后能够重试
这时需要写 个配置 FeignConfig 类
在该类中注入Retryer的Bean会覆盖掉默认的 Retryer的Bean
  1. @Configuration
  2. public class feignconfig {
  3. @Bean
  4. public Retryer feignRetryer(){
  5. return new Retryer.Default(, TimeUnit.SECONDS.toMillis(1L),);
  6. }
  7. }

5、Spring-Cloud-声明式调用 Feign(上)的更多相关文章

  1. spring cloud服务间调用feign

    参考文章:Spring Cloud Feign设计原理 1.feign是spring cloud服务间相互调用的组件,声明式.模板化的HTTP客户端.类似的HttpURLConnection.Apac ...

  2. Spring Cloud声明式调用Feign负载均衡FeignClient详解

    为了深入理解Feign,下面将从源码的角度来讲解Feign.首先来看看FeignClient注解@FeignClient的源码,代码如下: FeignClient注解被@Target(ElementT ...

  3. spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  4. Spring Cloud Config整合Spring Cloud Kubernetes,在k8s上管理配置

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Kubernetes有专门的ConfigMap和Secret来管理配置,但它也有一些局限性,所以还是希望通过Spring C ...

  5. 【spring cloud】【IDEA】【maven】spring cloud多模块在idea上使用maven插件打包报错:程序包XXX不存在

    >>>>spring cloud 多模块 >>>>在idea上使用maven插件打包,欲打包成jar包后 进行部署 >>>> 报 ...

  6. Spring Cloud项目中通过Feign进行内部服务调用发生401\407错误无返回信息的问题

    问题描述 最近在使用Spring Cloud改造现有服务的工作中,在内部服务的调用方式上选择了Feign组件,由于服务与服务之间有权限控制,发现通过Feign来进行调用时如果发生了401.407错误时 ...

  7. 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】

    环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...

  8. Spring-Cloud之Feign声明式调用-4

    一.Feign受Retrofit.JAXRS-2.0和WebSocket影响,采用了声明式API 接口的风格,将Java Http 客户端绑定到它的内部. Feign 首要目的是将 Java Http ...

  9. Spring Cloud系列之使用Feign进行服务调用

    在上一章的学习中,我们知道了微服务的基本概念,知道怎么基于Ribbon+restTemplate的方式实现服务调用,接着上篇博客,我们学习怎么基于Feign实现服务调用,请先学习上篇博客,然后再学习本 ...

  10. Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现

    作者 | Spring Cloud Alibaba 高级开发工程师洛夜 来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发...Spring Cloud ...

随机推荐

  1. 一、hive安装(内置数据库derby)

    hive是一个数据仓库工具,建立在hadoop之上,它的存在是为了让大数据的查询和分析更加的方便.hive提供简单的sql查询功能,并最终转换为mapreduce任务执行. 一.环境 JDK1.8+官 ...

  2. java自学-编程入门

    java语言写的代码需要先编译为可执行文件,才能被jvm执行.在下载的jdk安装目录下的bin目录,有两个可执行程序java.exe和javac.exe,javac就是用来编译的,java是执行编译后 ...

  3. HDU 1757 矩阵求第n的递推式

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. spss C# 二次开发 学习笔记(一)——配置数据源

    由于项目的需要,使用Spss进行数据统计分析. Spss对于数据统计分析的功能有多强主要是客户关注的事情,我所主要关注的是,Spss的二次开发有多复杂. 学习的基本思路是: (1)首先了解统计基本知识 ...

  5. Jquery获取radio选中的值

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 使用PuTTy在CentOS下安装web.py与简单的文件传输

    两周前,出于帮朋友忙的目的,尝试了一下微信公众号的菜单自定义与自动回复功能的实现,成了. 两周后,需要将代码转移至朋友新购的服务器上,发现基本操作全忘记了,麻瓜!所以记一笔,希望也能对大家也有帮助. ...

  7. Raspberry Pi - Huawei HiLink E3256 3G modem to ethernet adapter

    Raspberry Pi - Huawei HiLink E3256 3G modem to ethernet adapter This page documents how to configure ...

  8. win7 远程连接服务器出现身份验证错误,且找不到加密Oracle修正

    用远程桌面连接登录服务器,结果,弹出一个错误的提示框:发生身份验证错误,要求的函数不受支持. 然后在网上找了相关的教程,基本上所有的方法都是如下所示: 策略路径:"计算机配置"-& ...

  9. ListView中Item与Checkable子类控件抢焦点问题

    Android开发中,经常需要为ListView定制Adapter,绑定各种子类控件.如果Item包含Button等Checkable的控件,那么就会发生点击Item无法响应的问题.原因是自己定义的I ...

  10. [学习] nofollow

    [来源:百度百科 http://baike.baidu.com/view/1584081.htm] 简介 nofollow[1]是一个HTML标签的属性值.它的出现为网站管理员提供了一种方式,即告诉搜 ...