Feign之@feignclient用法,通过url方式跨Eureka走网关调用服务,以及fallbackFactory熔断机制控制业务

目录

背景

业务需求

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)
调用成功

Feign之@feignclient用法,通过url方式跨Eureka走网关调用服务,以及fallbackFactory熔断机制控制业务_第1张图片

 

 

你可能感兴趣的:(springcloud,项目开发)