**
OpenFeign
**
springcloud官网地址:
https://spring.io/projects/spring-cloud-openfeign#overview
下面是官网对OpenFeign的一段描述
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Eureka, Spring Cloud CircuitBreaker, as well as Spring Cloud LoadBalancer to provide a load-balanced http client when using Feign.
feign本质是一个集成项目的接口调用工具,同时也支持线上注册任务的接口调用方式,比如:nacos,eureka。
依赖
<!--Spring Cloud OpenFeign Starter -->
<dependency><groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
示例:
@FeignClient(value = ServiceAppName.LOGIN_SERVICE_NAME,url = ServiceAppName.LOGIN_SERVICE_URL)
public interface LoginMasterFeign {
@GetMapping("/getUser")
public ReturnObj<Object> userLogin(@RequestParam("username") String username,@RequestParam("password") String password) throws ClassNotFoundException;
}
@FeignClient重要标签,value属性feign接口的名称,url属性对应接口地址:协议://ip:端口/地址。
启动类包扫描:
@EnableFeignClients(basePackages = {
"com.byun.xxx.feign"
})
basePackages属性feign接口包地址,可以传入多值。
具体详情可以参照官网提供的api文档。
https://docs.spring.io/spring-cloud-openfeign/reference/spring-cloud-openfeign.html
遇到的线上问题:在使用feign,跨module进行http调用时,HttpServletRequest requst不能传输,否则报错。