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 ...
随机推荐
- 2009年4月,Twitter宣布他们已经把大部分后端程序从Ruby迁移到Scala
w Scala 简介 | 菜鸟教程 http://www.runoob.com/scala/scala-intro.html
- principal components analysis 主成份分析
w http://deeplearning.stanford.edu/wiki/index.php/主成份分析 主成分分析(PCA)及其在R里的实现 - jicf的日志 - 网易博客 http:// ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_4_transient关键字_瞬态关键字
不想被序列化的成员变量用 transient修饰 age加上static关键字 反序列化age就读取不到正确的值了
- delphi开发实例:保存字体设置的方法
http://blog.csdn.net/delphi308/article/details/9906147 delphi开发实例:保存字体设置的方法 2013-08-11 22:37 446人阅读 ...
- mybatis 如何关闭connection
1.前言 最开始操作数据库是使用jdbc操作数据库,每次创建一个连接都需要关闭连接,避免占用资源.比如 Class.forName("com.jdbc.mysql.Driver") ...
- jmeter逻辑控制详解(1)
逻辑控制器 Jmeter提供了多种逻辑控制器,下面进行讲解说明: 1.Simple Controller 简单控制器是最基本的控制器,对jmeter测试运行没有任何影响,可以将某些请求归集在一个简单控 ...
- Windows7 系统安装
转载请标明本文链接:(https://www.cnblogs.com/softwarecb/p/11773811.html) 目前微软已经停止支持Windows 7,而且由于芯片组更新的原因,新的硬件 ...
- python基础-6.2正则表达式,计算器练习
content = "1-2*((60-30+(1-40/5*5+3-2*5/3)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))&q ...
- Java第三周总结&实验报告(1)
总结:不知不觉,到了第三周,回顾这一周,我更加深入了解了main方法,除此之外,学习了两个关键字,一个this,一个static,this在强调属性时,只能放在句首且不能循环调用,static声明用于 ...
- C语言如何操作内存
1.用变量名来访问内存(c语言对内存地址的封装.数据类型.函数名)--直接访问内存(使用地址) 如 int a; 编译器将申请32bit的内存(4个内存单元),同时将内存地址和变量名a绑定,操作a时, ...