OpenFeign入门

Feign是一个声明式WebService客户端,使用Feign能让编写的Web Service客户端更加简单。它的使用方法是定义一个服务接口然后在上面添加注解,Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持Spring MVC标准注解和HttpMessageConverters。Feign和Eureka和Ribbon组合使用以支持负载均衡。

Feign集成了Ribbon

Feign和Ribbon不同的是,通过Fegin只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现服务调用。

Fegin OpenFegin
Fegin是Spring Cloud组件中一个轻量级RESTful的HTTP服务客户端 Fegin内置了Ribbon,用来做客户端的负载均衡,去调用服务注册中心的服务,Fegin的使用方式:使用Fegin的注解定义接口,调用这个接口,就可以调用服务注册中心服务。 OpenFegin是Spring Cloud在Fegin的基础上支持了SpringMVC的注解,如@RequestMapping等等,OpenFegin的@FeginClien可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方法产生实现类,实现类中做负载均衡并调用其他服务。
<dependency>
 <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

接下来看看OpenFegin如何使用

首先创建一个新的项目 cloud-consumer-fegin-order80

添加openfegin的依赖

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

application.yml配置文件

server:
  port: 80


eureka:
  client:
    register-with-eureka: false  #false 表示不向注册中心注册自己
    service-url:
      #设置与Eureka Server交互地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

主启动类 添加@EnableFeignClients注解

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

接下来创建一个service 添加@FeignClient(value = "CLOUD-PAYMENT-SERVICE") 用来指明这个一个Fegin接口 ,并且指向

CLOUD-PAYMENT-SERVICE服务。

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface Paymentservice {

    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);
}

添加一个Controller

@RestController
@RequestMapping("/fegin/order")
public class FeginOrderController {

   @Autowired
   private Paymentservice paymentservice;

    @GetMapping("/get/{id}")
    public CommonResult create(@PathVariable("id") Long id){
        return paymentservice.getPaymentById(id);
    }

}

查看访问结果

OpenFegin的基本使用是不是很便捷很简单呢?

 接下来看一下OpenFegin的超时控制,由于OpenFegin里面自带了Ribbon,所以超时控制可由Ribbon实现。

首先在两个服务提供方添加超时测试方法,刻意等待3秒。

@GetMapping("/timeout")
    public String getFeignTimeOut(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverport;
    }

在客户端的Paymentservice中添加方法

@GetMapping("/payment/timeout")
    public String getFeignTimeOut();

在客户端Controller中添加

  @GetMapping("/timeout")
    public String timeout(){
        return paymentservice.getFeignTimeOut();
    }

测试方法会报超时

 由于Feign客户端默认只等待1秒,所以会出现超时错误。

在客户端配置文件中添加配置 设置ribbon的超时时间。

ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1
  eureka:
    enabled: true

猜你喜欢

转载自blog.csdn.net/JIY743761374/article/details/104914499
今日推荐