@FeignClient常用属性
@FeignClient(name = "gateway-test", value = "gateway-test", url = "localhost:9997", fallbackFactory = FallbackFactory.class, contextId = "GatewayClient")
name/value: 服务名称
//查看源码可知: 两个属性等价
public @interface FeignClient {
@AliasFor("value")
String name() default "";
@AliasFor("value")
String name() default "";
}
//查看源码可知: 两个属性至少要配置一个
FeignClientsRegistrar {
private String getClientName(Map<String, Object> client) {
if (client == null) {
return null;
}
String value = (String) client.get("contextId");
if (!StringUtils.hasText(value)) {
value = (String) client.get("value");
}
if (!StringUtils.hasText(value)) {
value = (String) client.get("name");
}
if (!StringUtils.hasText(value)) {
value = (String) client.get("serviceId");
}
if (StringUtils.hasText(value)) {
return value;
}
throw new IllegalStateException("Either 'name' or 'value' must be provided in @"
+ FeignClient.class.getSimpleName());
}
}
url: 请求地址, 没配置的话, 会把name/value的属性值当成服务名进行调用, 配置的话则使用url的值
示例:

Product服务: 服提供者, 服务名product-client, 存在一个接口/product/test, 返回"test"字符串
User服务: 调用方, 服务名user-client, 存在一个接口/user/test, 调用product服务的test接口
//配置url方式,请求地址为url的地址
@FeignClient(name = "product-client", url = "localhost:9997")
public interface ProductClient {
/**
* ..
* @return .
*/
@GetMapping("/product/test")
String test();
}
@RequestMapping("/user")
public class UserController {
@Resource
private ProductClient client;
@GetMapping("/test")
public String test() {
return client.test();
}
}
//访问http://localhost:8200/user/test报错:
//Connection refused: connect executing GET http://localhost:9997/product/test with root cause
//说明配置了url属性后,确实是根据url的值作为请求地址进行调用
//使用服务名调用方式
@FeignClient(name = "product-client")
public interface ProductClient {
/**
* ..
* @return .
*/
@GetMapping("/product/test")
String test();
}
//调用成功,后台能看到获取注册中心(nacos)的数据解析获取真实的url地址的日志

//用一个不存在的服务名再验证一下
@FeignClient(name = "product-client123")
public interface ProductClient {
/**
* ..
* @return .
*/
@GetMapping("/product/test")
String test();
}
//报错信息:
com.netflix.client.ClientException: Load balancer does not have available server for client: product-client123
fallbackFactory: 获取异常信息
示例:
user服务开启hystrix
feign:
hystrix:
enabled: true
//user服务添加fallback,用于在调用服务出现错误时,返回自定义信息
@Component
public class ProductClientFallback implements ProductClient{
@Override
public String test() {
return "error";
}
}
//user服务添加fallbackFactory,用户在调用服务出现错误时,获取错误信息
@Component
public class ProductClientFallbackFactory implements FallbackFactory<ProductClient> {
@Resource
ProductClientFallback fallback;
@Override
public ProductClient create(Throwable cause) {
System.out.println("cause.getMessage() = " + cause.getMessage());
return fallback;
}
}
//product服务修改/product/test接口,模拟出现异常
@RestController
@RequestMapping("/product")
public class ProductController {
@GetMapping("/test")
public String test() throws Exception {
throw new Exception("test Exception");
}
}
Postman访问 http://localhost:8200/user/test ,成功返回自定义的异常信息

查看服务调用方User的日志, 成功打印出错误日志

message信息为空的话,配置一下Product服务:
server:
error:
include-message: ALWAYS
具体原因请查看 博客, 也可以通过使用@ControllerAdvice来解决
contextId: 别名
假设一个User服务有两个FeignClient,都需要调用Product服务, 因为name属性值一样,所以需要通过配置contextId来区分,否则启动项目时会报错
@FeignClient(name = "product-client", fallbackFactory = ProductClientFallbackFactory.class, contextId = "ProductClientCopy")
public interface ProductClientCopy {
/**
* ..
* @return .
*/
@GetMapping("/product/testCopy")
String test();
}
@FeignClient(name = "product-client", fallbackFactory = ProductClientFallbackFactory.class, contextId = "ProductClient")
public interface ProductClient {
/**
* ..
* @return .
*/
@GetMapping("/product/testCopy")
String test();
}
@FeignClient常用属性的更多相关文章
- 【Android自学日记】五大布局常用属性
线性布局(LinearLayout)常用属性: android:orientation="vertical"--决定子类控件的排布方式(vertical垂直:horizontal水 ...
- DataGrid中的常用属性
DataGrid中的常用属性 $('#dg').datagrid({ url:'datagrid_data.json', columns:[[ {field:'code',title:'Code',w ...
- Node.js process 模块常用属性和方法
Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...
- ImageView的常用属性
ImageView的一些常用属性,并且这些属性都有与之对应的getter.setter方法: android:adjustViewBounds:设置ImageView是否调整自己的边界来保持所显示图片 ...
- HTML a标签、4个伪类、常用属性(下载)、锚链接(待扩展:邮件、电话、短信、GPS)
HTML 超链接<a> 1.超链接可以是一个字.一个词.一组词.一幅图像,您可以点击这些内容来跳转到新的文档或者当前文档中的某个部分. 2.当您把鼠标指针移动到网页中的某个链接上时,箭头会 ...
- iOS导航控制器常用函数与navigationBar常用属性
导航控制器常用函数触发时机 当视图控制器的View将要出现时触发 - (void)viewWillAppear:(BOOL)animated 当视图控制器的View已经出现时触发 - (void)vi ...
- CSS样式常用属性整理
web工程师是最近5年刚刚兴起的一门高薪职业,人们的专注度越来越高. 那么前端除了学习html标签之外还需要掌握什么知识点呢? 为大家整理了一个和HTML标签密不可分的知识要点--<CSS样式常 ...
- WPF DataGrid常用属性记录
WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...
- Android开发中XML布局的常用属性说明
<!-- 常用属性说明: android:id="@+id/button" 为控件指定Id android:text="NNNNNNNNNN" 指定控件的 ...
随机推荐
- 5.23考试总结(NOIP模拟2)
5.23考试总结(NOIP模拟2) 洛谷题单 看第一题第一眼,不好打呀;看第一题样例又一眼,诶,我直接一手小阶乘走人 然后就急忙去干T2T3了 后来考完一看,只有\(T1\)骗到了\(15pts\)[ ...
- Mysql权限管理以及sql数据备份
权限管理和备份 用户管理 可视化管理 SQL命令操作 用户表:msql.user 同样就是对表的操作,就是对这张表的增删改查 -- 创建用户 create user kuangshen identfi ...
- Qt实现基于多线程的文件传输(服务端,客户端)
1. 效果 先看看效果图 这是传输文件完成的界面 客户端 服务端 2. 知识准备 其实文件传输和聊天室十分相似,只不过一个传输的是文字,一个传输的是文件,而这方面的知识,我已经在前面的博客写过了,不了 ...
- Java中对象调用方法的顺序
Java虚拟机会预先为加载到内存中的每个类维护一个方法表(Method Table),其中列出了所有类中所有方法的签名. 现在有2个类A和B,其中,B是A的子类,和一个B类型的对象x,当调用x.f(a ...
- 36、网卡绑定bond
注意:虚拟机需要网卡模式为同一模式,否则无法进行通信: 36.1.mode0(平衡负载模式): 平时两块网卡均工作,且自动备援,但需要在与服务器本地网卡相连的交换机设备上进行端口聚合来支持绑定技术. ...
- 7、resync实时备份
sersync+rsync(增量,无差异备份),resync支持多线程,效果比inotify更好,配置思想和inotify很相似 7.1.在备份服务器上安装并配置rsync服务,实现nfs共享目录,可 ...
- Java并发之ReentrantReadWriteLock源码解析(二)
先前,笔者和大家一起了解了ReentrantReadWriteLock的写锁实现,其实写锁本身实现的逻辑很少,基本上还是复用AQS内部的等待队列思想.下面,我们来看看ReentrantReadWrit ...
- 《面试八股文》之kafka21卷
微信公众号:moon聊技术 关注选择" 星标 ", 重磅干货,第一 时间送达! [如果你觉得文章对你有帮助,欢迎关注,在看,点赞,转发] 大家好,我是moon,最新一篇面试八股文系 ...
- ROS2学习之旅(12)——创建工作空间
workspace(工作空间)是包含ROS2 packages(包)的文件夹.在使用ROS 2之前,有必要在终端中source一下ROS 2的安装工作区,这样就可以在该终端中使用ROS 2的软件包. ...
- ESP32-使用有刷直流电机笔记
基于ESP-IDF4.1 1 /* 2 * 刷直流电动机控制示例,代码通过L298电机芯片测试 3 */ 4 5 #include <stdio.h> 6 7 #include " ...