Feign Dynamic URL
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11811932.html
Project Directory

Maven Dependency
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.fool.feign</groupId>
<artifactId>hello-feign</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-server-impl</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.3.RELEASE</version>
</dependency> <dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8188 feign.hystrix.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
hystrix.threadpool.default.coreSize=10
Source Code
Application.java
package org.fool.feign; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Note: 使用Feign需要加上 @EnableFeignClients 注解
AbstractFallbackFactory.java
package org.fool.feign.client; import feign.hystrix.FallbackFactory;
import org.fool.feign.common.LogDomain;
import org.fool.feign.common.ResultCode;
import org.fool.feign.contract.response.ApplicationResponse;
import org.fool.feign.logger.ApplicationLogger;
import org.fool.feign.logger.ApplicationLoggerFactory;
import org.fool.feign.util.JsonUtils; public abstract class AbstractFallbackFactory<T> implements FallbackFactory<T> {
protected final ApplicationLogger LOGGER = ApplicationLoggerFactory.getLogger(getClass());
private static final String DEFAULT_MESSAGE = "isolation by hystrix"; String handleExceptions(Throwable cause, String message) {
LOGGER.error(LogDomain.CLIENT, message, cause); ApplicationResponse response = ApplicationResponse.builder()
.resultCode(ResultCode.INTERNAL_ERROR)
.message(ResultCode.INTERNAL_ERROR + ":" + DEFAULT_MESSAGE)
.build(); return JsonUtils.objectToJson(response);
}
}
OrderClient.java
package org.fool.feign.client; import org.fool.feign.config.FeignConfiguration;
import org.fool.feign.contract.request.DemoRequest;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import java.net.URI; @FeignClient(name = "ORDER-SERVICE", url = "url-placeholder",
fallbackFactory = OrderClient.OrderClientFallbackFactory.class, configuration = FeignConfiguration.class)
public interface OrderClient { @PostMapping(value = "/demo")
String queryOrder(URI uri, @RequestBody DemoRequest orderRequest); @Component
class OrderClientFallbackFactory extends AbstractFallbackFactory<OrderClient> {
@Override
public OrderClient create(Throwable throwable) {
return new OrderClient() {
@Override
public String queryOrder(URI uri, DemoRequest orderRequest) {
String message = "failed to query_order with " + orderRequest + " url: " + uri.toString();
return handleExceptions(throwable, message);
}
};
}
}
}
Note:
通常第一个红框输入的是远程服务的URL,第二个红框则没有URI uri 参数,这里为了实现远程调用模版接口相同,请求URL不同,采用如下图所示实现

ApplicationTest.java
package org.fool.feign.test; import org.fool.feign.Application;
import org.fool.feign.client.OrderClient;
import org.fool.feign.contract.request.DemoRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.net.URI; @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTest { @Autowired
private OrderClient orderClient; @Test
public void test() throws Exception {
String url = "http://localhost:8188/order/v1/resource"; DemoRequest request = new DemoRequest();
request.setOrderTime("2019-11-11");
request.setBizTag("order"); String result = orderClient.queryOrder(new URI(url), request);
System.out.println(result);
}
}
Note: 实现动态调用如下图红框所示,动态传如一个URL

Console Output

Feign Dynamic URL的更多相关文章
- WebServic dynamic url
How to make your Web Reference proxy URL dynamic 开发环境和部署环境,Webservice 的URL不同 需将url 配置到 web.config文件中 ...
- Feign 动态URL 解决记录
Feign中使用动态URL请求 (应当是spring-cloud-starter-openfeign,不知道和一般的feign有何差别) 在spring项目下,假设有这样个Feign的消费接口,原来写 ...
- Feign从配置文件中读取url
Feign的url和name都是可配置的,就是从配置文件中读取的属性值,然后用占位符引用就可以了: ${rpc.url} @FeignClient(name = "me", url ...
- Feign实现动态URL
需求描述 动态URL的需求场景: 有一个异步服务S,它为其他业务(业务A,业务B...)提供异步服务接口,在这些异步接口中执行完指定逻辑之后需要回调相应业务方的接口. 这在诸如风控审核,支付回调等场景 ...
- 分享一个 SpringCloud Feign 中所埋藏的坑
背景 前段时间同事碰到一个问题,需要在 SpringCloud 的 Feign 调用中使用自定义的 URL:通常情况下是没有这个需求的:毕竟都用了 SpringCloud 的了,那服务之间的调用都是走 ...
- Spring Cloud中关于Feign的常见问题总结
一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...
- Ionic 动态配置url路由的设置
随着Ionic App功能的不断增加,需要路由的url设置就越来越多,不喜欢在config函数中写一堆硬代码,一则不美,二则维护起来也麻烦,能不能把这些数据独立出来呢? 经过查找资料与各种实验,最终找 ...
- Spring Cloud系列之Feign的常见问题总结
一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...
- Spring Cloud官方文档中文版-声明式Rest客户端:Feign
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...
随机推荐
- nf_conntrack: table full, dropping packet解决方法
https://blog.csdn.net/yanggd1987/article/details/45886913
- 构建嵌入式Linux交叉编译工具链
开源交叉编译工具链制作方法汇总: 1) 使用crosstool/crosstool-ng生成 2) 使用buildroot生成 ARM交叉编译工具链说明: 1) arm-linux-gcc是一个集合命 ...
- 2013 AAAI: Uncorrelated Lasso
Si-Bao Chen, Chris Ding, Bin Luo and Ying Xie. Uncorrelated Lasso. AAAI, 2013. 第一作者是安徽大学陈思宝副教授. 第二作者 ...
- flex 数字上标
以A的3次方为例,我们输入以下代码: /** * 部分代码参考Adobe文档: * http://help.adobe.com/zh_CN/AS3LCR/Flash_10.0/flash/text/e ...
- VMware 虚拟化编程(11) — VMware 虚拟机的全量备份与增量备份方案
目录 目录 前文列表 全量备份数据的获取方式 增量备份数据的获取过程 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VMware 虚拟化编 ...
- Openstack_通用模块_Oslo_vmware 创建/删除 vCenter 虚拟机
目录 目录 oslovmware Connect to vCenter Server Create VirtualMachine for vCenter 常用的虚拟机配置项 删除虚拟机 oslo.vm ...
- vue组件间通信子与父
二.组件间通信(子组件传值给父组件) 通过事件的方式来完成数据的传输. ①在父组件中 定义一个方法,用来接收子组件所通过事件传来的值 methods:{ recvMsg:function(msg){ ...
- JavaSE编码试题强化练习3
1.给20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐. public class TestCirculation { public static void ma ...
- [Web 前端] 016 css 元素的转换
三种元素之间的转换 display 属性是用来设置元素的类型及隐藏的 常用的属性有 none 元素隐藏且不占位置 block 元素以块元素显示 inline 元素以内联元素显示 inline-bloc ...
- [Python3] 008 列表内涵,“满腹经纶”
目录 简述 少废话,上例子 例1 用 for 创建列表 例2 看看乘法"向"着谁 例3 给列表加一张"滤纸" 例4 列表生成式可以嵌套 例5 列表生式还能嵌入条 ...