引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类添加注解:
@EnableFeignClients
命名接口:
/**
* name:指定rest接口所对应的服务名
* path: 指定调用rest接口所在的StockController指定的RequestMapping,如果没有就不指定
*/
@FeignClient(name = "stock-service", path = "/stock")
public interface StockFeignService {
//声明需调用的rest接口对应的方法
@RequestMapping("reduct")
String reduct();
}
调用:
@RequestMapping("/addWithFeign")
public String addWithFeign() {
System.out.println("下单成功!");
String msg = stockFeignService.reduct(); //在类中注入接口
return "Hello Feign!" + msg;
}
修改feign日志:
代码方式:
@Configuration //这样加入配置就会全局生效,不加配置类注解,加入进入@EnableFeignClients
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
配置文件方式:
feign优化:
底层实现:
URLConnection:默认实现,不支持连接池;Apache HttpClient:支持连接池;OKHttp:支持连接池
使用连接池代替默认的URLConnection:
引入依赖:
配置连接池: