原创转载请注明出处: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的更多相关文章

  1. WebServic dynamic url

    How to make your Web Reference proxy URL dynamic 开发环境和部署环境,Webservice 的URL不同 需将url 配置到 web.config文件中 ...

  2. Feign 动态URL 解决记录

    Feign中使用动态URL请求 (应当是spring-cloud-starter-openfeign,不知道和一般的feign有何差别) 在spring项目下,假设有这样个Feign的消费接口,原来写 ...

  3. Feign从配置文件中读取url

    Feign的url和name都是可配置的,就是从配置文件中读取的属性值,然后用占位符引用就可以了: ${rpc.url} @FeignClient(name = "me", url ...

  4. Feign实现动态URL

    需求描述 动态URL的需求场景: 有一个异步服务S,它为其他业务(业务A,业务B...)提供异步服务接口,在这些异步接口中执行完指定逻辑之后需要回调相应业务方的接口. 这在诸如风控审核,支付回调等场景 ...

  5. 分享一个 SpringCloud Feign 中所埋藏的坑

    背景 前段时间同事碰到一个问题,需要在 SpringCloud 的 Feign 调用中使用自定义的 URL:通常情况下是没有这个需求的:毕竟都用了 SpringCloud 的了,那服务之间的调用都是走 ...

  6. Spring Cloud中关于Feign的常见问题总结

    一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...

  7. Ionic 动态配置url路由的设置

    随着Ionic App功能的不断增加,需要路由的url设置就越来越多,不喜欢在config函数中写一堆硬代码,一则不美,二则维护起来也麻烦,能不能把这些数据独立出来呢? 经过查找资料与各种实验,最终找 ...

  8. Spring Cloud系列之Feign的常见问题总结

    一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...

  9. Spring Cloud官方文档中文版-声明式Rest客户端:Feign

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

随机推荐

  1. vue +ts 在router的路由中import报错的解决方案

    在router.ts中引入.vue文件,会提示打不到module,但是编译可能成功,运行也不报错 找了好久,发现了这个答案 https://segmentfault.com/a/11900000167 ...

  2. centos输入正确的账号和密码登陆不进去

    vm下启动centos,输入正确的账号和密码,依然登陆不进去,一直处于这个界面: 暂时的解决方法是:先等待一段时间.重启,然后再输入密码,然后,ctrl+c 不停地ctrl+c,然后就登陆进去了.什么 ...

  3. VMware vMotion 配置要求

    目录 目录 vCenter 支持 vMotion 的前提 条件 vMotion 的主机配置 vMotion 共享存储器要求 vMotion 网络要求 最后 vCenter 支持 vMotion 的前提 ...

  4. SQLServer中的top、MySql中的limit、Oracle中的rownum

    (1)在SQL Server中,我们使用 select top N * from tablename来查询tablename表中前N条记录. (2)在MySQL中,我们使用select * from ...

  5. 关于一段有趣代码引出的String创建对象的解释

    通常来说,我们认为hashCode不相同就为不同的对象.就这样由一段代码引发了一场讨论,代码如下: @Test public void stringCompare() { String s1 = &q ...

  6. js面向对象程序设计之继承

    在面向对象语言中继承分成两种:接口继承和实现继承.解释一下,接口继承只继承方法的签名,而实现继承则是继承实际的方法.但是ECMAScript中的函数没有签名所以无法进行接口继承,只能是实现实现继承.而 ...

  7. 升级到mysql5.7无法启动问题解决

    漏洞扫描,老项目升级到5.7位成功,启动发现报错:unknown option log_error 线备份my.cnf配置文件, 猜测应该是写法有问题,先把log_error  改成log #log_ ...

  8. promise 封装 axios

    /*axios({ method:"get", url:"./data.json", data:{ id:10 } }).then((res)=>{ co ...

  9. Spring Boot系列(三) Spring Boot 之 JDBC

    数据源 类型 javax.sql.DataSource javax.sql.XADataSource org.springframework.jdbc.datasource.embedded,Enbe ...

  10. c#工厂模式

    创建一个抽象类: public abstract class Test {  public abstract void Print();//输出信息} 创建输出123的测试类 public class ...