【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】自定义配置的更多相关文章

  1. Spring Cloud Feign 自定义配置(重试、拦截与错误码处理) 实践

    Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 目录 Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 引子 FeignClient的 ...

  2. Feign自定义编程配置

    介绍 在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feigh默认使用的编码器.解码器.所使用的契约等.Spring Cloud允 ...

  3. Feign 自定义编码器、解码器和客户端,Feign 转发请求头(header参数)、Feign输出Info级别日志

    Feign 的编码器.解码器和客户端都是支持自定义扩展,可以对请求以及结果和发起请求的过程进行自定义实现,Feign 默认支持 JSON 格式的编码器和解码器,如果希望支持其他的或者自定义格式就需要编 ...

  4. ASP.NET 5 入门 (2) – 自定义配置

    ASP.NET 5 入门 (2) – 自定义配置 ASP.NET 5 理解和入门 建立和开发ASP.NET 5 项目 初步理解ASP.NET5的配置 正如我的第一篇文章ASP.NET 5 (vNext ...

  5. 基于Spring的可扩展Schema进行开发自定义配置标签支持

    一.背景 最近和朋友一起想开发一个类似alibaba dubbo的功能的工具,其中就用到了基于Spring的可扩展Schema进行开发自定义配置标签支持,通过上网查资料自己写了一个demo.今天在这里 ...

  6. C#创建自定义配置节

    在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...

  7. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  8. VS2012 常用web.config配置解析之自定义配置节点

    在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...

  9. C#如何使用和开发自定义配置节

    在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择.这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉.程序中的配置信息一般放在应用程序的app.c ...

随机推荐

  1. React入门知识点清单

    做前端的一定都知道现在是三大框架--Vue.React.Angular三足鼎立的时代.Vue是公认的最容易入门的,因为它文件结构上有传统的HTML的影子,让刚接触它的前端人员刚到很"亲切&q ...

  2. gcc在x64体系中如何传递参数,linux,mac,iOS适用

    上一篇介绍了vc(windows)平台在x64体系当中,c函数的传参方式.本篇将要介绍gcc(类linux,mac)平台在x64中,c函数是如何传参的.为节约时间和篇幅,首先来定义一个有十个参数的函数 ...

  3. 二 linuk系统安装

    一 VM虚拟机安装与使用 安装: VM官网:www.vmware.com 不需要太高版本 安装较为简单,选典型安装,别放c盘即可 使用 1.创建虚拟机时,把每个处理器的内核数量调高(我调为8),注意别 ...

  4. Python自动化办公之操作Excel文件

    模块导入 import openpyxl 读取Excel文件 打开Excel文件 workbook = openpyxl.load_workbook("test.xlsx") 输出 ...

  5. [学习笔记] 在Windows 10上安装 WebLogic 12.2.1.3

    本文适用于学习目的而撰写.截止目前WebLogic已经有12.2.1.4.0了. 在安装WebLogic 12.2.1.3.0 首先要在Windows10之上Oracle JDK 1.8.  当前认证 ...

  6. 基于Vue的日历组件

    可以标注重要日子 自己写的,可能不是特别很好,大家多提意见!!! 地址:https://github.com/jsLWQ/calendar

  7. 优秀的github项目学习

    优秀的github项目学习 后期会陆续添加遇到的优秀项目 https://github.com/chaijunkun

  8. 微调(Fine-tune)原理

    在自己的数据集上训练一个新的深度学习模型时,一般采取在预训练好的模型上进行微调的方法.什么是微调?这里已VGG16为例进行讲解,下面贴出VGGNet结构示意图. 上面圈出来的是VGG16示意图,也可以 ...

  9. DexOpt相关的异常

    查找的资料 dvm探讨之odex绕过 DexClassLoader4.4.2动态加载分析(磁盘加载分析) - ::): DexOpt: incorrect opt magic number (0xff ...

  10. 构建之法组——“别吃错喽”微信小程序评价

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9860 基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论 ...