首先,加入两个服务提供者一个服务消费者用于测试。
服务消费者openfeign配置:
依赖
<!--Open feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.1.4</version>
</dependency>
消除缓存警告依赖
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.12</version>
</dependency>
启动类添加注解
@EnableFeignClients
服务消费者创建
注意是接口
ParOpenFeignService代码
@FeignClient("QIESIYV-ZHUTI-TIGONG")//所要调用的服务名
@Component
public interface ParOpenFeignService {
@GetMapping(value = "/ceshi/ceshi")//指向服务提供者方法地址
String ceshi();
}
CuserController代码
@Resource
private ParOpenFeignService parOpenFeignService;
@GetMapping(value = "ceshi")
public String ceshi(){
return parOpenFeignService.ceshi();
}
服务提供者创建CeshiController用于测试,方法为返回端口号
postman测试结果可见实现轮询
再测试接口执行5秒,发现未提示超时,可见新版openfeign的默认超时时间未再设置为1s
注意:
- 在Service接口部分,若要请求的方法请求方式为get且有参数,则需要添加@SpringQueryMap如,不添加的话服务提供方会报Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘POST’ not supported]的错误,即使你用的是@GetMapping。
日志:
NONE:默认级别,不显示日志
BASIC:仅记录请求方法、URL、响应状态及执行时间
HEADERS:除了BASIC中定义的信息之外,还有请求和响应头信息
FULL:除了HEADERS中定义的信息之外,还有请求和响应正文及元数据信息
配置类
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLogLevel(){
return Logger.Level.FULL;//级别
}
}
yml
logging:
level:
qiesiyv.mainbody.service.*: debug #包名.类名: debug 以debug的形式打印日志
效果