5.1、写一个 Feign 害户端

新建项目:

依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

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

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

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

@EnableFeignClients:开启Feign Client功能

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

EurekaClientFeign.java

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

feignconfig.java

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

FeignService.java

@Service
public class FeignService { @Autowired
EurekaClientFeign eurekaClientFeign; public String port(){
return eurekaClientFeign.port();
}
}
在Service层FeignService注入EurekaClientFeign去调用port()方法

FeignController.java

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

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

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

spring-cloud-starter-openfeign 的porn 文件

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hystrix</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-java8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-archaius</artifactId>
<optional>true</optional>
</dependency>

5.2、FeignClient详解

@FeignClient注解源码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.springframework.cloud.openfeign; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor; @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
@AliasFor("name")
String value() default ""; /** @deprecated */
@Deprecated
String serviceId() default ""; String contextId() default ""; @AliasFor("value")
String name() default ""; String qualifier() default ""; String url() default ""; boolean decode404() default false; Class<?>[] configuration() default {}; Class<?> fallback() default void.class; Class<?> fallbackFactory() default void.class; String path() default ""; boolean primary() default true;
}
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

@Configuration
public class FeignClientsConfiguration {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Autowired(
required = false
)
private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList();
@Autowired(
required = false
)
private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList();
@Autowired(
required = false
)
private Logger logger; public FeignClientsConfiguration() {
} @Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
} @Bean
@ConditionalOnMissingBean
@ConditionalOnMissingClass({"org.springframework.data.domain.Pageable"})
public Encoder feignEncoder() {
return new SpringEncoder(this.messageConverters);
} @Bean
@ConditionalOnClass(
name = {"org.springframework.data.domain.Pageable"}
)
@ConditionalOnMissingBean
public Encoder feignEncoderPageable() {
return new PageableSpringEncoder(new SpringEncoder(this.messageConverters));
} @Bean
@ConditionalOnMissingBean
public Contract feignContract(ConversionService feignConversionService) {
return new SpringMvcContract(this.parameterProcessors, feignConversionService);
} @Bean
public FormattingConversionService feignConversionService() {
FormattingConversionService conversionService = new DefaultFormattingConversionService();
Iterator var2 = this.feignFormatterRegistrars.iterator(); while(var2.hasNext()) {
FeignFormatterRegistrar feignFormatterRegistrar = (FeignFormatterRegistrar)var2.next();
feignFormatterRegistrar.registerFormatters(conversionService);
} return conversionService;
} @Bean
@ConditionalOnMissingBean
public Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
} @Bean
@Scope("prototype")
@ConditionalOnMissingBean
public Builder feignBuilder(Retryer retryer) {
return Feign.builder().retryer(retryer);
} @Bean
@ConditionalOnMissingBean({FeignLoggerFactory.class})
public FeignLoggerFactory feignLoggerFactory() {
return new DefaultFeignLoggerFactory(this.logger);
} @Bean
@ConditionalOnClass(
name = {"org.springframework.data.domain.Page"}
)
public Module pageJacksonModule() {
return new PageJacksonModule();
} @Configuration
@ConditionalOnClass({HystrixCommand.class, HystrixFeign.class})
protected static class HystrixFeignConfiguration {
protected HystrixFeignConfiguration() {
} @Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(
name = {"feign.hystrix.enabled"}
)
public Builder feignHystrixBuilder() {
return HystrixFeign.builder();
}
}
}
重写 FeignClientsConfiguration 类中的 Bean
覆盖掉默认的配置 Bean 
从而达到自定义配置的目的
 
Feign 默认的配置在请求失败后 重试次数为 ,即不重试Retryer.NEVER_RETRY
现在希望在请求失败后能够重试
这时需要写 个配置 FeignConfig 类
在该类中注入Retryer的Bean会覆盖掉默认的 Retryer的Bean
@Configuration
public class feignconfig {
@Bean
public Retryer feignRetryer(){
return new Retryer.Default(, TimeUnit.SECONDS.toMillis(1L),);
}
}

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. java 并发(七)--- ThreadLocal

         文章部分图片来自参考资料 问题 : ThreadLocal 底层原理 ThreadLocal 需要注意什么问题,造成问题的原因是什么,防护措施是什么 ThreadLocal 概述 Threa ...

  2. Oracle数据库基本操作(三) —— DQL相关内容说明及应用

    本文所使用的查询表来源于oracle数据中scott用户中的emp员工表和dept部门表. 一.基本语法 SQL语句的编写顺序: select 输出的列 from 表名 where 条件 group ...

  3. 3d图片点击切换

    效果图: 代码块: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  4. Java设计模式—桥梁模式

    终于又碰到了一个简单点的模式了. 桥梁模式也叫做桥接模式,定义如下:                将抽象和实现解耦,使得两者可以独立地变化. 这句话也太难理解了,桥梁模式是为了解决类继承的缺点而设计 ...

  5. Servlet - Reasource loading

    1. Load db3.properties String path = this.getServletContext().getRealPath("/WEB-INF/classes/db/ ...

  6. javascript的 热点图怎么写

    在gis中,你如果用js来写热点图 不借助后台怎么搞,as的话比较容易有相应的类库甚至官方都有.而且用js不借助arcgis发布rest服务,(注:热点图可以借助服务的形式发布,arcgis for ...

  7. MUI框架-09-MUI 与后台数据交互

    MUI框架-09-MUI 与后台数据交互 本篇介绍使用 art-template 和原生 MUI 的数据交互 mui.ajax 来实现 我们大家都知道,想要数据交互就要有数据,每次当我们发送请求,我们 ...

  8. IEC62304软件维护框架

    软件维护计划的任务 建立接收.记录.评估.解决和追踪医疗器械软件发行后的反馈 制定确认反馈是否是问题的标准 使用风险管理过程 使用配置管理过程 制定升级.补丁以及遗留问题修正计划 问题和修改分析的任务 ...

  9. 微信小程序 - 弹出键盘遮挡住输入框

    在开发微信小程序的时候遇到,输入用户名或者手机号以及地址,手机键盘调起来,会把输入框遮挡. 如图: 以上两张图是自己工作中遇到的,此处不要着急,一个属性帮你搞定. cursor-spacing:指定光 ...

  10. spring cloud Eureka server配置

    参考:http://www.ityouknow.com/springcloud/2017/05/10/springcloud-eureka.html spring boot版本:2.0.3.RELEA ...