【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. ssm 不能将查询的结果返回到界面的一个原因

    * 在controller类中,应先定义一个ArrayList的集合即就是: List<Product> defaultProductList = new ArrayList(); // ...

  2. RHEL7.2 安装Eclipse-oxygen Hadoop开发环境

    1 Eclipse-oxygen下载地址 http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/re ...

  3. [Part 3] 在Ubuntu 16.04源码编译PCL 1.8.1支持VTK和QT

    本文首发于个人博客https://kezunlin.me/post/137aa5fc/,欢迎阅读! Part-3: Install and Configure PCL 1.8.1 with vtk q ...

  4. cglib测试例子和源码详解

    目录 简介 为什么会有动态代理? 常见的动态代理有哪些? 什么是cglib 使用例子 需求 工程环境 主要步骤 创建项目 引入依赖 编写被代理类 编写MethodInterceptor接口实现类 编写 ...

  5. AE10.0在Visual Studio 2012下安装没有模板(转)

    转自百度经验: VS2012中丢失ArcGIS模板的解决方法 由于ArcGIS10.0(for .NET)默认是用VS2010作为开发工具的,所以在先安装VS2012后装ArcGIS10.0 桌面版及 ...

  6. Ubuntu中使用Nginx+rtmp模块搭建流媒体视频点播服务

    1. 背景 不知不觉笔者来到流媒体部门已经一年半多了,积攒了不少的流媒体知识,但平时工作也比较忙,很少进行总结性的梳理,最近准备花几个周末时间写一个流媒体系列的实践文章,也算是给自己做总结的同时帮助有 ...

  7. Python 0基础开发游戏:打地鼠(详细教程)VS code版本

    如果你没有任何编程经验,而且想尝试一下学习编程开发,这个系列教程一定适合你,它将带你学习最基本的Python语法,并让你掌握小游戏的开发技巧.你所需要的,就是付出一些时间和耐心来尝试这些代码和操作. ...

  8. urllib练习

    # coding = utf-8 """ 解析https://www.kuaidaili.com/free/网站里的代理地址, 并测试是否可用 ""& ...

  9. 【数据结构】之顺序表(Java语言描述)

    之前总结过使用C语言描述的顺序表数据结构.在C语言类库中没有为我们提供顺序表的数据结构,因此我们需要自己手写,详细的有关顺序表的数据结构描述和C语言代码请见[我的这篇文章]. 在Java语言的JDK中 ...

  10. 详解在Linux系统中安装Tomcat

    本文以在CentOS 7.6中安装Tomcat8.5为例进行安装,其他系统和版本都是大同小异的. 安装JDK 安装Tomcat之前,需要先安装JDK,可以参看之前的文章详解在Linux系统中安装JDK ...