【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"); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南