【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 ...
随机推荐
- 使用不同的C++支持库的模块混合开发时,引发异常展开不正常,抛异常竟引出一个SIGSEGV
如果你使用gcc对一部分模块进行了GNUMake的编译,这些编译出动态库使用在Gradle编译框架下的项目.那么就有可能出现题目中的情况,使用不同的C++支持库的模块混合开发时,引发异常展开不正常. ...
- Docker从入门到掉坑(三):容器太多,操作好麻烦
前边的两篇文章里面,我们讲解了基于docker来部署基础的SpringBoot容器,如果阅读本文之前没有相关基础的话,可以回看之前的教程. Docker 从入门到掉坑 Docker从入门到掉坑(二): ...
- java.lang.NoSuchMethodError: org.apache.tomcat.JarScanner.scan(Ljavax/servlet/ServletContext;Ljava/lang/ClassLoader;Lorg/apache/tomcat/JarScannerCallback;Ljava/util/Set;)V
java.lang.NoSuchMethodError: org.apache.tomcat.JarScanner.scan(Ljavax/servlet/ServletContext;Ljava/l ...
- 1142 CREATE VIEW command denied to user 'blog'@'XXX.XXX.XXX.XXX' for table 'Articles'
创建视图时,报如上的1142错误,是数据库权限设置的问题. 进入mysql的root用户,赋予所有权限即可: mysql>grant all privileges on blogDB.* to ...
- HTML的标签认识
<!-- html标签 h1~h6 标题标签(只有1~6,依次减小) p 段落标签 span 无意义的行标签 div 无意义的块标签 b 加粗 ...
- mac软件推荐及chrome插件推荐
通用软件 Alfred (超级好用的效率工具) 用mac这个软件一定要装,用习惯之后加上电脑本身的快捷键.效率提升的飞起. Alfred我常使用的功能有: 搜索chrome的书签 我搜索的书签大概分为 ...
- 百度杯 black_hole复现
在这次复现中,经历了太多挫折. 刚刚开始的时候本地调试 get不到shell,就很疑问,而且不会爆破,想学下怎么爆破出那个0x05, 后来问了位师傅 ,他说用retdl_solve 试试,我就跑去学了 ...
- Spring Cloud Alibaba基础教程:Nacos服务发现与配置管理
随着微服务概念的流行,越来越多的公司采用`Spring Cloud`全家桶构建微服务系统,实现业务的快速迭代.`Spring Cloud`提供了快速构建分布式微服务常用组件,包括`Spring Clo ...
- mvc 学习笔记
1.routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); MVC中的路由忽略,只要访问的地址中带有 .axd , 该请求都将排除在mv ...
- day20191012笔记
课程默写笔记: 1.程序架构 C/S 客户端/服务器端 B/S 浏览器/服务器端 2.Tomcat应用服务器 tomcat默认端口号是80:tomcat配置文件中通常端口的定义是8080: 3.使用开 ...