原创转载请注明出处: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. navigation ObtacleCostFunction源码分析

    ObtacleCostFunction 定义了一个ObstacleCostFunction类,继承自Trajectory类,Trajectory类有8个类参 总共有8个类参 double xv_,yv ...

  2. 职位-CEO:CEO

    ylbtech-职位-CEO:CEO 首席执行官(Chief Executive Officer,缩写CEO),职位名称,是在一个企业中负责日常事务的最高行政官员,主司企业行政事务,又称作司政.行政总 ...

  3. KindEditor上传图片一直提示undefined

    图片已经上传成功了,但是就是不在文本编辑器里显示图片,一直弹出undefined 返回的JSON都对呀!这是官网说的返回值: //成功时 { "error" : 0, " ...

  4. Windows Server 2008 R2 为用户“IIS APPPOOL\DefaultAppPool”授予的权限不足,无法执行此操作

    报表开发与部署好后,也嵌入到aspx页面中了,使用VS自带的Web服务器组件,一切正常,当部署到IIS中的时,出现了如下错误: 为用户“IIS APPPOOL\DefaultAppPool”授予的权限 ...

  5. docker进阶——数据管理与网络

    一.数据卷管理 用户在使用 Docker 的过程中,势必需要查看容器内应用产生的数据,或者 需要将容器内数据进行备份,甚至多个容器之间进行数据共享,这必然会涉及 到容器的数据管理 (1)Data Vo ...

  6. Selfishness is not living as one wishes to live. it is asking others to live as wishes to live.

    regin: n. 统治; 任期 lap:n. 大腿部. procession: n. 行列,游行 lessen: n. 减少 wade: v. 跋涉 patriotic: adj. 爱国的 Medi ...

  7. SpringBoot中FreeMarker创建

    1.创建SpringBoot项目时,勾选freemarker依赖(web,dev) 2.Controller中向model中存放数据 package com.ziv.springbootbygrald ...

  8. log4net 配置文件配置方法

    转自:http://www.dozer.cc/2013/06/log4net-config-file-order/ 最近把项目中所有的日志都改成了 log4net ,同事也蠢蠢欲动,用起了 log4n ...

  9. [2019杭电多校第八场][hdu6667]Roundgod and Milk Tea

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题目大意是说n个班级,每个班级有ai人和bi杯茶,每个人只能喝其他班的茶并且只能喝一杯.问最多有 ...

  10. C# 共享文件读取(转)

    using System;using System.Runtime.InteropServices;using BOOL = System.Boolean;using DWORD = System.U ...