feign调用走不走网关全局拦截_Feign之@feignclient用法,通过url方式跨Eureka走网关调用服务,以及fallbackFactory熔断机制控制业务...

本文介绍了如何使用Feign在不同Eureka注册中心间进行服务调用,通过设置url直接调用目标网关实现跨Eureka通信。同时,展示了如何结合Hystrix熔断机制,当调用失败时返回自定义错误信息。
摘要由CSDN通过智能技术生成

目录

背景

业务需求

Demo实现

背景

eureka1 中包含一堆服务:

aa1

aa1

vplm

vslm

aa-gateway(注册ip+端口  :10.10.xx.xx:9901/)

等等.............

eureka2 中包含一堆服务:

bb1

bb2

bb-gateway(注册ip+端口  :10.10.xx.xx:9902/)

customer-im

employee

等等.............

业务需求

vslm 模块需要调用customer-im 模块的接口,而vslm注册在eureka1中,customer-im注册在eureka2 中

现在需要vslm跨eureka调用customer-im服务接口

Demo实现

eureka相关配置及注解略

customer-im 模块暴露的接口

@ApiOperation("跨eureka联调测试")

@GetMapping("/test/{id}")

public String connTest(@PathVariable String id){

//方法用于测试

String out = "联调成功访问客户模块并返回数据:\t" + id;

System.out.println(out);

//模拟超时情况触发熔断

/*try {

Thread.sleep(60000L);

} catch (InterruptedException e) {

e.printStackTrace();

}*/

return out;

}

vslm消费服务

yml配置url

#跨eureka走网关

feign_api_url:

customerName: customer

customerUrl: 10.10.xx.xx:9902/customer-im/

feign:

hystrix:

enabled: true

feign接口编写:CustomerFeignApi

其中url给的是:eureka2的网关的服务地址(customerUrl: 10.10.xx.xx:9902/customer-im/)

即:调的是gateway2网关,后面加的是服务实力名,让网关自己去注册中心找需要调用的服务实例(实现负载均衡ribbon)

/**

* @Description

* @Author by mocar小师兄

* @Date 2020/6/29 11:45

**/

@FeignClient(name = "${feign_api_url.customerName}",url = "${feign_api_url.customerUrl}",fallbackFactory = CustomerFeignApiImpl.class)

public interface CustomerFeignApi {

/***

* 功能描述: 测试联调

* 〈〉

* @Param: []

* @Return: java.lang.String

* @Author: by

* @Date: 2020/6/29 11:51

*/

@GetMapping("/customer/test/{id}")

String connTest(@PathVariable String id);

}

熔断机制类:CustomerFeignApiImpl

@Component

public class CustomerFeignApiImpl implements FallbackFactory{

private static final Logger logger = LoggerFactory.getLogger(CustomerFeignApiImpl.class);

@Override

public CustomerFeignApi create(Throwable cause) {

return new CustomerFeignApi(){

@Override

public String connTest(String id) {

logger.error("fallback;arg was: {}, exception was: {}, reason was: {}",id ,cause.toString(),cause.getMessage());

return cause.getMessage();

}

};

}

}

controller层的接口测试:

@Autowired

private CustomerFeignApi customerFeignApi;

@GetMapping("/test/{id}")

public String test(@PathVariable("id") String id){

System.out.println("入参为" + id);

String connTest = customerFeignApi.connTest(id);

//如果feign调用出现网络等异常,会执行rollback实现类中重写的方法,并返回重写的方法的return值

//如果正常,则返回调用的接口的返回值

System.out.println(connTest);//

return connTest;

}

运行结果:(下面是模拟熔断的结果输出)

入参为nnnnnhhhhhhhh

2020-06-29 15:29:04 [hystrix-customer-1] ERROR c.h.v.s.f.f.CustomerFeignApiImpl.connTest - fallback;arg was: nnnnnhhhhhhhh, exception was: feign.FeignException: status 500 reading CustomerFeignApi#connTest(String), reason was: status 500 reading CustomerFeignApi#connTest(String)

status 500 reading CustomerFeignApi#connTest(String)

调用成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值