SpringCloud(二):服务调用OpenFeign(Ribbon)


Springboot版本为2.2.6.RELEASE,SpringCloud版本为Hoxton.SR3

一、OpenFeign相关概念

  • OpenFeign:OpenFeign是Netflix开发的声明式、模板化的HTTP请求客户端。可以更加便捷、优雅地通过HTTP请求调用API
  • Feign和OpenFeign:Feign本身不支持Spring MVC的注解,它有一套自己的注解。OpenFeign是SpringCloud 在Feign的基础上支持了SpringMVC注解。OpenFeign的@FeignClient可以解析成SpringMVC的@RequestMapping注解下的接口, 并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

二、消费者进行服务调用

1、引入依赖
<!-- 服务调用-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、启动类上添加注解

@EnableFeignClients

3、添加服务调用接口
  • 被调用服务接口及其配置文件

Controller

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author IT00ZYQ
 * @date 2021/5/5 14:59
 **/
@RestController
public class MyController {

    @Value("${server.port}")
    private String port;

    @GetMapping("test")
    public String hello() {
        return "port: " + port + " === Hello, Eureka!";
    }
}

配置文件

eureka:
  client:
    service-url:
      # 向一个Eureka Server节点进行服务注册
      defaultZone: http://euk2.com:7001/eureka/
spring:
  application:
    name: code-service
  profiles:
    active: 8001
  1. 方式一:不依赖注册中心,固定前置URL
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 不依赖注册中心
 * @author IT00ZYQ
 * @date 2021/5/8 18:25
 **/
@FeignClient(name = "userServiceFeign2", url = "http://localhost:8001")
public interface UserServiceFeign2 {
    /**
     * 测试Feign调用
     * @return String
     */
    @GetMapping("test")
    String hello();
}
  1. 方式二:依赖注册中心,方便进行负载均衡调用
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 依赖于注册中心Eureka
 * @author IT00ZYQ
 * @date 2021/5/8 18:25
 **/
@FeignClient(name = "code-service")
public interface UserServiceFeign1 {
    /**
     * 测试Feign调用
     * @return String
     */
    @GetMapping("test")
    String hello();
}
4、注入Bean调用服务
import com.it00zyq.user_service.service.UserServiceFeign1;
import com.it00zyq.user_service.service.UserServiceFeign2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author IT00ZYQ
 * @date 2021/5/5 15:08
 **/
@RestController
public class TestController {
    @Autowired
    private UserServiceFeign1 userServiceFeign1;

    @Autowired
    private UserServiceFeign2 userServiceFeign2;

    @GetMapping("test1")
    public String test6() {
        return "test1: " + userServiceFeign1.hello();
    }

    @GetMapping("test2")
    public String test2() {
        return "test2: " + userServiceFeign2.hello();
    }
}

三、超时机制

  • Feign默认支持Ribbon,Feign和Ribbon都有重试机制,都启用的话,会产生冲突,所以默认关闭Feign的重试机制,使用Ribbon的重试机制
  • 使用ribbon重试机制,请求失败后,每个6秒会重新尝试
  • 相关配置
    在调用方进行配置,提供服务方只提供服务
ribbon:
  ConnectTimeout: 1000 #连接超时时间ms
  ReadTimeout: 3000 #被调用服务业务处理超时时间ms
  #同一台实例最大重试次数,不包括首次调用
  MaxAutoRetries: 2
  #重试负载均衡其他的实例最大重试次数,不包括首次调用
  MaxAutoRetriesNextServer: 2
  #是否所有操作都重试 post、put等添加修改操作多次重试可能导致重复添加/修改
  OkToRetryOnAllOperations: false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

it00zyq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值