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
- 方式一:不依赖注册中心,固定前置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();
}
- 方式二:依赖注册中心,方便进行负载均衡调用
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
2764

被折叠的 条评论
为什么被折叠?



