现在我们来复写Feign的默认配置7.2 Overriding Feign Defaultshttps://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.htmlSpring Cloud的Feign支持的一个中心是命名客户端,也就是我们可以为Client进行命名,每个FeignClient是整体的一部分,他们一起工作,按需联系远程服务器,并且该整体有一个名称,开发人员可以使用FeignClient进行命名A central concept in Spring Cloud’s Feign support is that of the named client. Each feign client is part of an ensemble of components that work together to contact a remote server on demand,and the ensemble has a name that you give it as an application developer using the @FeignClient annotation. @FeignClient("microservice-simple-provider-user")
public interface UserFeignClient {我们可以用这个进行命名org.springframework.cloud.netflix.feign.FeignClient/*** The service id with optional protocol prefix. Synonym for {@link #value() value}.*/
@AliasFor("value")
String name() default "";name其实他是value的别名,Spring Cloud按照需要,使用FeignClientsConfiguration为每个命名的客户端创建一个新的整体,FeignClientsConfiguration这个是FeignClient的默认配置类,/*** A custom <code>@Configuration</code> for the feign client. Can contain override* <code>@Bean</code> definition for the pieces that make up the client, for instance* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.** @see FeignClientsConfiguration for the defaults*/
Class<?>[] configuration() default {};他默认就是FeignClientsConfiguration,为每一个命名的客户端建立一个新的整体作为ApplicationContext,我们知道Spring他是一个父子容器,我们通过FeignClient其实创建了一个子容器,他包含了feign.Decoder,feign.Encoder,feign.Contract,包含了解码器,编码器,和契约,Spring Cloud creates a new ensemble as an ApplicationContext on demand for each named client using FeignClientsConfiguration. This contains (amongst other things) an feign.Decoder, a feign.Encoder,and a feign.Contract.你可以用额外的声明,额外的配置,去完全控制Feign的配置Spring Cloud lets you take full control of the feign client by declaring additional configuration(on top of the FeignClientsConfiguration) using @FeignClient. Example:@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {//..
}FooConfiguration does not need to be annotated with @Configuration. However, if it is, then take care to exclude it from any @ComponentScan that would otherwise include this configuration as it will become the default source for feign.Decoder, feign.Encoder, feign.Contract, etc., when specified. This can be avoided by putting it in a separate, non-overlapping package from any @ComponentScan or @SpringBootApplication, or it can be explicitly excluded in @ComponentScan.The serviceId attribute is now deprecated in favor of the name attribute.@FeignClient("microservice-simple-provider-user")你这里用了name就不要用id了,/*** The service id with optional protocol prefix. Synonym for {@link #value() value}.** @deprecated use {@link #name() name} instead*/
@Deprecated
String serviceId() default "";以前你使用URL属性,name不是必须的,但是现在他是必须的了,Previously, using the url attribute, did not require the name attribute. Using name is now required./*** An absolute URL or resolvable hostname (the protocol is optional).*/
String url() default "";Placeholders are supported in the name and url attributes.@FeignClient(name = "${feign.name}", url = "${feign.url}")
public interface StoreClient {//..
}我这边只写url,没有写name,他还是不报错的,启动的时候就启动不来Spring Cloud Netflix provides the following beans by default for feign (BeanType beanName: ClassName):Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)Encoder feignEncoder: SpringEncoderLogger feignLogger: Slf4jLoggerContract feignContract: SpringMvcContractFeign.Builder feignBuilder: HystrixFeign.BuilderClient feignClient: if Ribbon is enabled it is a LoadBalancerFeignClient, otherwise the default feign client is used.Spring Cloud Netflix项目呢,使用这些作为默认配置,解码器用的是ResponseEntityDecoder这个解码器,编码器用的是SpringEncoder这个编码器,Logger用的是Slf4jLogger这个Log,Contract用的是SpringMvcContract契约,所以他才可以使用SpringMVC的注解,如果你使用Ribbon的话,然后你还可以使用OkHttpClient,这也是一个非常火的一个项目,The OkHttpClient and ApacheHttpClient feign clients can be used by setting feign.okhttp.enabled or feign.httpclient.enabled to true, 下面是Spring Cloud没有进行默认配置的Spring Cloud Netflix does not provide the following beans by default for feign, but still looks up beans of these types from the application context to create the feign client:Logger.LevelRetryerErrorDecoderRequest.OptionsCollection<RequestInterceptor>SetterFactoryhttps://github.com/OpenFeign/feigninterface GitHub {@RequestLine("GET /repos/{owner}/{repo}/contributors")List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);@RequestLine("POST /repos/{owner}/{repo}/issues")void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);}localhost:8010/movie/1
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>microservice-consumer-movie-feign</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>microservice-simple-consumer-movie</name><description>Demo project for Spring Boot</description><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
#debug=true
server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-consumer-movie-ribbon
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.healthcheck.enabled=true
spring.redis.host=10.40.8.152
spring.redis.password=1234
spring.redis.port=6379#stores.ribbon.listOfServers=10.40.8.144:7900
package com.learn.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import feign.Contract;
import feign.Logger;@Configuration
public class Configuration1 {@Beanpublic Contract feignContract() {return new feign.Contract.Default();}@BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL;}
}
package com.learn.cloud.feign;import org.springframework.cloud.netflix.feign.FeignClient;import com.learn.cloud.entity.User;
import com.learn.config.Configuration1;import feign.Param;
import feign.RequestLine;@FeignClient(name = "microservice-simple-provider-user", configuration = Configuration1.class)
public interface UserFeignClient {@RequestLine("GET /simple/{id}")public User findById(@Param("id") Long id);}
package com.learn.cloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import com.learn.cloud.entity.User;
import com.learn.cloud.feign.UserFeignClient;@RestController
public class MovieController {@Autowiredprivate UserFeignClient userFeignClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {return this.userFeignClient.findById(id);}}
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerMovieFeignApplication {public static void main(String[] args) {SpringApplication.run(ConsumerMovieFeignApplication.class, args);}
}

Feign-2覆写Feign的默认配置相关推荐

  1. Fegion-3覆写Fegion的默认配置及Fegion的日志

    http://10.40.8.152:8761/eureka/apps后面跟上服务的名称http://10.40.8.152:8761/eureka/apps/MICROSERVICE-CONSUME ...

  2. 第8章 Drupal 主题系统( Drupal theme)(4) 高级特性--1,覆写主题函数

    Drupal主题高级特性  译者:老葛 在前面的部分,你学到了Drupal使用的各种不同模板文件,当Drupal要将你的主题合并到一起时就会查找这些模板.你学到了如何创建页面模板文件,如何创建特定节点 ...

  3. Spring Cloud 覆写远端的配置属性

    覆写远端的配置属性 应用的配置源通常都是远端的Config Server服务器,默认情况下,本地的配置优先级低于远端配置仓库.如果想实现本地应用的系统变量和config文件覆盖远端仓库中的属性值,可以 ...

  4. SpringCloud:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)

    现在为止所进行的所有的 Rest 服务调用实际上都会出现一个非常尴尬的局面,例如:以如下代码为例:Dept dept = this.restTemplate.exchange(DEPT_GET_URL ...

  5. feign返回null_109、Feign的服务降级和Turbine

    一.信号量隔离 创建项目 修改pom文件添加hystrix坐标 <dependency> 修改配置文件 spring.application.name=eureka-consumer-ri ...

  6. feign 序列化_自定义 feign 反序列化时间字符格式

    feign client 默认配置类:默认的配置类为FeignClientsConfiguration 配置了解码和编码. 当请求Feign Client的方法执行时会被 SynchronousMet ...

  7. hadoop配置文件默认配置

    原文地址:http://www.linuxqq.net/archives/964.html 获取默认配置 配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapre ...

  8. STM32F2系列系统时钟默认配置

    新到一家公司后,有个项目要用到STM32F207Vx单片机,找到网上的例子照猫画虎的写了几个例子,比如ADC,可是到了ADC多通道转换的时候就有点傻眼了,这里面的时钟跑的到底是多少M呢?单片机外挂的时 ...

  9. Vue-CLI@4——html-webpack-plugin默认配置的获取与修改

    官方文档 https://cli.vuejs.org/zh/guide/webpack.html#简单的配置方式 获取默认配置 配置vue.config.js 在项目根目录下创建vue.config. ...

最新文章

  1. php 腾讯云实时音视频,腾讯云视频 -实时音视频学习日志
  2. 如何让Jupyter Notebook支持pytorch、tensorflow
  3. VB无所不能之三:VB截获Windows消息的钩子
  4. Ajax学习(6)---服务器端脚本和程序中用 JSON 进行响应和回复
  5. linux comm 12,Linux comm命令
  6. Struts 2初体验
  7. 西门子ddc_铁门关西门子两通电动阀VVF42.25-10C+SKD60西
  8. mysql权限日志_mysql权限管理、日志管理及常用工具
  9. 中国在线度假旅游市场专题分析2020
  10. 解决pytorch DataLoader 加载数据报错UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe5 in position 1023
  11. java count 在哪一类里_count 是java关键字吗
  12. java的text函数,excel text函数以及相关的函数使用方法
  13. LeetCode383. Ransom Note
  14. iphone编程,使用代码结束程序
  15. 金融计量学第一次实验:eviews做多元线性回归分析
  16. python信用卡管理源码_Python随机生成信用卡卡号的实现方法
  17. 化工厂定位系统健全企业安全体系
  18. 多颗微粒的阵列光镊系统设计
  19. 请假工资扣费总额计算机公式,病假扣款计算公式excel
  20. springboot+vue社区维修平台(源码+文档)

热门文章

  1. C# OOP 重要部分全解
  2. [詹兴致矩阵论习题参考解答]习题1.3
  3. 关于mysql的error-based injection payload
  4. delphi 中配置文件的使用(*.ini)
  5. java解析json转Map
  6. java常用API之DateFormat
  7. mysql 用户已设置密码,但登录可以不填密码
  8. ABP(现代ASP.NET样板开发框架)系列之2、ABP入门教程
  9. 一种简单定义FourCC常量的方法 (C/C++)
  10. JS 对select动态添加options操作[IEFireFox兼容]