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 ...
随机推荐
- 源码编译apache设置系统启动失败
文章为转载,亲试成功. Apache无法自动启动,1.将apachectl文件拷贝到/etc/rc.d/init.d 中,然后在/etc/rc.d/rc5.d/下加入链接即可.命令如下:cp /usr ...
- PHP执行外部程序
备份/恢复数据库 exec - 执行一个外部程序(在php文件所在目录进行执行) 很久以前写的,很多方法是项目中的直接复制粘体用不了,只能提供下思路. 用到执行外部程序的就这一句: exec(&quo ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_1_字符输入流_Reader类&FileRead
- Python工具库(感谢backlion整理)
漏洞及渗透练习平台: WebGoat漏洞练习平台: https://github.com/WebGoat/WebGoat webgoat-legacy漏洞练习平台: https://github.co ...
- 前端 CSS的选择器 属性选择器
属性选择器,字面意思就是根据标签中的属性,选中当前的标签. 属性选择器 通常在表单控件中 使用比较多 根据属性查找 /*用于选取带有指定属性的元素.*/ <!DOCTYPE html> & ...
- Java类和对象的内存分配
类的加载时机: 1.创建对象 2.调用类的静态成员 3.加载子类 类在实例化后的内存分配 1.每次创建对象时,都需要进行加载和创建2个操作: ① 先去判断需要的类是否已经加载,如果已经加载了,则无需再 ...
- SQL Server增量备份数据[转]
服务器中一个数据库DB,其中表A有几个地方要写入的,程序设定在网络不通的时候也可以用本机的数据库运行,在网络通达后,再将本机的数据写回到服务器中,经研究,增量备份有以下几种可能性: 1. ...
- ES6 new Set实现数组去重
使用new Set实现数组去重必须结合for of, 如果使用for循环就实现不了 var arr = new Set([1, 2, 1, 1, 2, 3, 3, 4, 4]); for (var e ...
- BZOJ 4552(二分+线段树+思维)
题面 传送门 分析 此题是道好题! 首先要跳出思维定势,不是去想如何用数据结构去直接维护排序过程,而是尝试二分a[p]的值 设二分a[p]的值为x 我们将大于x的数标记为1,小于等于x的数标记为0 则 ...
- 简单易用的leetcode开发测试工具(npm)
描述 最近在用es6解leetcode,当问题比较复杂时,有可能修正了新的错误,却影响了前面的流程.要用通用的测试工具,却又有杀鸡用牛刀的感觉,所以就写了个简单易用的leetcode开发测试工具,分享 ...