[Alibaba微服务技术入门]_整合OpenFeign实现远程调度_第4讲

什么是Feign

Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。

Feign是Netflix开发的声明式、模板化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。Feign可帮助我们更加便捷、优雅地调用HTTP API。在Spring Cloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。Spring Cloud对Feign进行了增强,使Feign支持了Spring MVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便。

项目主页:https://github.com/OpenFeign/feign

在pom.xml文件,加入OpenFeign依赖

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
      <version>2.2.5.RELEASE</version>
</dependency>

在启动类中,加入@EnableFeignClients的注解

package com.liuyangjava;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumer8001 {
    public static void main(String[] args) {
        SpringApplication.run(NacosConsumer8001.class, args);
    }
}

在3-nacos-consumer8001项目中新增提供者的API接口

package com.liuyangjava.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "nacos-provider")
public interface NacosConsumerService {

    @GetMapping("/nacos/provider")
    String getInfoWithFeign();
}

在NacosConsumerController接口中调用Feign

package com.liuyangjava.controller;

import com.liuyangjava.service.NacosConsumerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class NacosConsumerController {

    @Value("${service-url.nacos-provider}")
    private String serverUrl;

    @Resource
    private NacosConsumerService nacosConsumerService;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/nacos/consumer")
    public String getInfo() {
        return restTemplate.getForObject(serverUrl + "/nacos/provider", String.class);
    }
    
    @GetMapping("/nacos/feign/consumer")
    public String getInfo2() {
        return nacosConsumerService.getInfoWithFeign();
    }

}

如果通过Feign我们成功调用到提供者的接口信息,但是在网络请求出现异常请求,如果还想再异常情况下使系统可用,那么就需要容错处理,比如:网络请求超时时给用户提示“稍后重试”。Spring Cloud Feign就是通过Fallback实现的,其中一种较为比较简单方式:@FeignClient.fallback = UserFeignFallback.class 指定一个实现Feign接口的实现类

第一步:在前面的 NacosConsumerService 接口 @FeignClient 上追加 fallback 属性

package com.liuyangjava.service;

import com.liuyangjava.service.fallback.NacosConsumerServiceFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "nacos-provider", fallback = NacosConsumerServiceFallBack.class)
public interface NacosConsumerService {

    @GetMapping("/nacos/provider")
    String getInfoWithFeign();
}

第二步:创建 NacosConsumerServiceFallBack 类,且实现 NacosConsumerService 接口

package com.liuyangjava.service.fallback;

import com.liuyangjava.service.NacosConsumerService;
import org.springframework.stereotype.Component;

@Component
public class NacosConsumerServiceFallBack implements NacosConsumerService {
    @Override
    public String getInfoWithFeign() {
        return "接口调用失败...";
    }
}
原文地址:https://www.cnblogs.com/liuyangjava/p/15418599.html