【Feign】自定义配置
【Feign】自定义配置
转载:
自定义配置,如果在同一个工程,注意配置不要和@SpringBootApplication或@ComponentSacan放在用一个包下,就是不要被扫描上
package cn.example.config; import cn.example.interceptor.YcxFeignRequestInterceptor;
import org.springframework.context.annotation.Bean; public class YcxFeignConfiguration {
/**
* 将契约改为feign原生的默认契约。这样就可以使用feign自带的注解了。
* @return 默认的feign契约
*/
// @Bean
// public Contract getAFeignContract() {
// System.out.println(">>> create getAFeignContract");
// return new feign.Contract.Default();
// }
@Bean
YcxFeignRequestInterceptor ycxFeignRequestInterceptor() {
System.out.println(">>> create ycxFeignRequestInterceptor");
return new YcxFeignRequestInterceptor();
}
}
自定义拦截器
package cn.example.interceptor; import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j; @Slf4j
public class YcxFeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
log.info(">>> " + template.path());
log.info(">>> " + YcxFeignRequestInterceptor.class.getSimpleName());
}
}
feign接口
package com.example.feignservice.feign; import cn.example.config.YcxFeignConfiguration;
import com.example.commenservice.bean.YcxResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "feign-service", configuration = {YcxFeignConfiguration.class})
public interface YcxFeign {
@RequestMapping("/testFeignA")
// @RequestLine("GET /testFeignA") // 自定义契约
YcxResult testFeignA(@RequestParam("value") String value);
}
接口实现
package com.example.feignservice.feign; import com.example.commenservice.bean.YcxResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.time.LocalTime; @RestController
public class YcxFeignImpl implements YcxFeign {
public YcxResult testFeignA(String value) {
return YcxResult.ok(LocalTime.now().toString() + " " + value);
}
}
调用端
package com.example.helloservice; import com.example.commenservice.bean.YcxResult;
import com.example.feignservice.feign.YcxFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; @SpringBootApplication
@RestController
@Slf4j
@EnableFeignClients(basePackageClasses = {YcxFeign.class})
public class HelloServiceApplication { public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
} @Autowired
YcxFeign feignA; // @Autowired
// HelloServiceApplication(Decoder decoder, Encoder encoder, Client client, Contract contract) {
// this.feignA = Feign.builder().client(client)
// .encoder(encoder)
// .decoder(decoder)
// .contract(contract)
// .requestInterceptor(new YcxFeignRequestInterceptor())
// .target(YcxFeign.class, "http://feign-service");
// } @RequestMapping("/")
public YcxResult home(HttpServletRequest request) {
log.info(request.getServletPath());
return feignA.testFeignA("Hello A");
} }
有两种方式,一种是自动注入,一种是被注释掉的,
@Autowired
HelloServiceApplication(Decoder decoder, Encoder encoder, Client client, Contract contract) {
this.feignA = Feign.builder().client(client)
.encoder(encoder)
.decoder(decoder)
.contract(contract)
.requestInterceptor(new YcxFeignRequestInterceptor())
.target(YcxFeign.class, "http://feign-service");
}
【Feign】自定义配置的更多相关文章
- Spring Cloud Feign 自定义配置(重试、拦截与错误码处理) 实践
Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 目录 Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 引子 FeignClient的 ...
- Feign自定义编程配置
介绍 在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feigh默认使用的编码器.解码器.所使用的契约等.Spring Cloud允 ...
- Feign 自定义编码器、解码器和客户端,Feign 转发请求头(header参数)、Feign输出Info级别日志
Feign 的编码器.解码器和客户端都是支持自定义扩展,可以对请求以及结果和发起请求的过程进行自定义实现,Feign 默认支持 JSON 格式的编码器和解码器,如果希望支持其他的或者自定义格式就需要编 ...
- ASP.NET 5 入门 (2) – 自定义配置
ASP.NET 5 入门 (2) – 自定义配置 ASP.NET 5 理解和入门 建立和开发ASP.NET 5 项目 初步理解ASP.NET5的配置 正如我的第一篇文章ASP.NET 5 (vNext ...
- 基于Spring的可扩展Schema进行开发自定义配置标签支持
一.背景 最近和朋友一起想开发一个类似alibaba dubbo的功能的工具,其中就用到了基于Spring的可扩展Schema进行开发自定义配置标签支持,通过上网查资料自己写了一个demo.今天在这里 ...
- C#创建自定义配置节
在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...
- [转]通过继承ConfigurationSection,在web.config中增加自定义配置
本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...
- VS2012 常用web.config配置解析之自定义配置节点
在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...
- C#如何使用和开发自定义配置节
在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择.这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉.程序中的配置信息一般放在应用程序的app.c ...
随机推荐
- Docker基础与实战,看这一篇就够了
docker 基础 什么是Docker Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,namespace,以及 AUFS 类的 Uni ...
- C# 彻底搞懂async/await
前言 Talk is cheap, Show you the code first! private void button1_Click(object sender, EventArgs e) { ...
- Composer依赖管理 – PHP的利器
别再到处搜PHP类扩展包了,对于现代语言而言,包管理器基本上是标配.Java 有 Maven,Python 有 pip,Ruby 有 gem,Nodejs 有 npm.PHP 的则是 PEAR,不过 ...
- Mac下载ChromeDriver
ChromeDriver下载地址: https://npm.taobao.org/mirrors/chromedriver 如何查看chrome版本与ChromeDriver版本对应 查看chrome ...
- Running serveral https server on a single IP address
Nginx 在一个IP上配置多个https server时,默认只会发送默认server name的证书.这是由ssl 协议本身行为导致的:先建立ssl connection,后发送http请求.即n ...
- PostGIS 使用Mysql_fdw同步ArcGIS填坑记录
##实现Mysql_fdw数据同步过程中,出现过很多坑,开此贴记录一下 1.触发器记录 这里insert的时候,采用过insert into f_pressureline select new.*,出 ...
- 【Luogu P2515】软件安装
Luogu P2515 这道题的题面与P2146有点像.一些不同地方就是P2146是无环的,这题是有环的. 很显然,如果有几个软件的依赖关系形成环,那么这几个软件就可以被看成是一个大软件,其价值和空间 ...
- 【NOIP2017】【Luogu P3956】【SPFA】棋盘
Luogu P3956 本题是一道简单的SPFA 具体看程序 #include<iostream> #include<cstdio> using namespace std; ...
- Java基础面试题及答案(三)
多线程 35. 并行和并发有什么区别? 并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生. 并行是在不同实体上的多个事件,并发是在同一实体上的多个事件. 在一台处理器 ...
- 【数据结构】之队列(C语言描述)
队列(Queue)是编程中最常用的数据结构之一. 队列的特点是“先进先出”,就像食堂排队买饭一样,先来的人排在前面,后来的人排在后面:前面的人先买饭,买完饭后离开这个队列.这就是队列的原理,它可以进行 ...