SpringCloud OpenFeign服务接口调用

介绍

OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。

Feign和OpenFeign区别

  • Feign

Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端
Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。

  • OpenFeign

OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

实战一

1.新建模块

新建模块:cloud-consumer-feign-order80

2.修改pom

<dependencies>
    <!--openfeign-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!--eureka client-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
    <dependency>
        <groupId>com.sgtech.springcloud</groupId>
        <artifactId>cloud-api-common</artifactId>
        <version>${project.version}</version>
    </dependency>
    <!--web-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--一般基础通用配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3.修改配置

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

4.添加启动类

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

5.添加业务类

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService
{
    
    
    @GetMapping(value = "/payment/get/{id}")
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
@RestController
public class OrderFeignController
{
    
    
    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
    {
    
    
        return paymentFeignService.getPaymentById(id);
    }
}

测试

  • 启动服务注册中心:7001和7002
  • 启动支付服务:8001和8002
  • 启动订单服务:80
  • 浏览器访问:http://localhost/consumer/payment/get/22

在这里插入图片描述

实战二

默认Feign客户端只等待一秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了,直接返回报错。
为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制。

1.支付模块添加超时接口

@GetMapping(value = "/feign/timeout")
public String paymentFeignTimeOut() {
    
    
    System.out.println("*****paymentFeignTimeOut from port: " + serverPort);
    //暂停几秒钟线程
    try {
    
    
        TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
    
    
        e.printStackTrace();
    }
    return serverPort;
}

2.订单模块添加超时接口

  • PaymentFeignService添加方法
@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeOut();
  • OrderFeignController添加方法:
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeOut()
{
    
    
    return paymentFeignService.paymentFeignTimeOut();
}

3.测试

浏览器访问: http://localhost/consumer/payment/feign/timeout

发现超时了,因为我们在支付模块中设置了睡眠3秒钟后返回!!

在这里插入图片描述

4.修改订单模块超时控制

添加以下配置,表示把超时时间拉长到5秒:

#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

再次访问接口,等待几秒后正常返回了结果。

在这里插入图片描述

项目代码

https://gitee.com/indexman/cloudstudy

猜你喜欢

转载自blog.csdn.net/IndexMan/article/details/119351976