一、搭建项目
1、创建feign-consumer模块
pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springCloud-demo</artifactId> <groupId>com.badcat</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../../pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.badcat</groupId> <artifactId>feign-consumer</artifactId> <packaging>jar</packaging> <name>feign-consumer</name> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
application.properties
spring.application.name=feign-consumer server.port=40001 eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/ # 每隔5秒钟,向服务注册中心发送一条续约指令 eureka.instance.lease-renewal-interval-in-seconds=5 # 如果30秒依然没有收到续约请求,判定服务过期 eureka.instance.lease-expiration-duration-in-seconds=30
启动类(注意@EnableFeignClients)
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; /** * @author badcat * @date 2020-04-12 11:58 */ @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
创建IService接口(注意@FeignClient("eureka-client")),这个用来对接要调用的服务(集群),所以我调用IService接口的某个方法,就是在调用eureka-client服务的对应接口。
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; /** * @author badcat * @date 2020-04-12 12:00 */ @FeignClient("eureka-client") public interface IService { /** * 此处指定调用eureka-client的哪个接口 * @return */ @GetMapping("/sayHi") String sayHi(); }
controller
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author badcat * @date 2020-04-12 12:03 */ @RestController public class Controller { @Autowired private IService iService; @GetMapping("/sayHi") public String sayHi(){ return this.iService.sayHi(); } }
启动eureka-server、eureka-client、eureka-client2、eureka-client3、feign-consumer,尝试通过feign-consumer调用eureka-client的接口。注册中心页面如下
调用结果如下,会发现feign的负载均衡默认时使用的ribbon的轮询策略。
因为feign是依赖ribbon的,我们修改一下ribbon的负载均衡策略,试一下改成随机,配置文件添加
#针对eureka-client服务集群的负载均衡策略的配置 eureka-client.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
启动相应模块,调用接口,发现已经是随机的了,不上截图了。
2、重试和超时策略
在feign-consumer的配置文件中添加如下配置,所以它的最大超时时间时(1000+2000)*(1+2)*(1+2)等于 2.7秒
# 每台机器最大的重试次数 feign-client.ribbon.MaxAutoRetries=2 # 可以再重试几台机器 feign-client.ribbon.MaxAutoRetriesNextServer=2 # 连接超时时间,单位毫秒 feign-client.ribbon.ConnectTimeout=1000 # 业务处理超时,单位毫秒 feign-client.ribbon.ReadTimeout=2000 # 在所有HTTP Method进行重试,默认false feign-client.ribbon.OkToRetryOnAllOperations=true
原文地址:https://www.cnblogs.com/hmxs/p/12685789.html
时间: 2024-10-08 22:33:16