【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 ...
随机推荐
- 0MQ讲述多线程魔法
为什么你想过的所有你所知道关于并发的事情,不是完全神经病的(Insane),就是假的(Bogus). 并发的定律,e=mc*c.这里并不是爱因斯坦的质能方程.而是 努力 = 代码规模 * (线程冲突碰 ...
- python: __future__的介绍
__future__ 给旧版本python提供新版本python的特性例如: 在python2.X中可以使用print"" 也可以使用print() 但是加载这个print的新特性 ...
- Elasticsearch从入门到放弃:文档CRUD要牢记
在Elasticsearch中,文档(document)是所有可搜索数据的最小单位.它被序列化成JSON存储在Elasticsearch中.每个文档都会有一个唯一ID,这个ID你可以自己指定或者交给E ...
- 插槽在父组件和子组件间的使用(vue3.0推荐)
子组件: 父组件: 插槽在父组件和子组件间的使用(vue3.0推荐):在外面加一个template模板
- tensorflow多层CNN代码分析
tf,reshape(tensor,shape,name=None) #其中shape为一个列表形式,特殊的一点是列表中可以存在-1.-1代表的含义是不用我们自己#指定这一维的大小,函数会自动计算,但 ...
- NTP服务编译安装报错:ntpd.c:124:29: 致命错误:sys/capability.h:没有那个文件或目录
缺少libcap-devel [root@localhost libcap]# cd /mnt/ [root@localhost mnt]# rpm -Uvh libcap*
- 处理器CPU天梯图,显卡天梯图(性能排名图)
自己网上找的几个图,仅供参考,买电脑可以看看了~
- Kafka 0.10.0.1 consumer get earliest partition offset from Kafka broker cluster - scala code
Return: Map[TopicPartition, Long] Code: val props = new Properties() props.put(ConsumerConfig.BOOTST ...
- 教程 Redis+ flask+vue 在线聊天
知识点 基于 Server-Sent Event 工作方式,Web 即时通信 Redis 包 发布订阅功能的使用 flask 快速入门,常用对象实例方法函数 Vuejs 列表页面自动渲染 效果图 代码 ...
- Netty学习——Google Protobuf的初步了解
学习参考的官网: https://developers.google.com/protocol-buffers/docs/javatutorial 简单指南详解:这个文档写的简直是太详细了. 本篇从下 ...