简介:
Feign是声明性Web服务客户端。它使编写Web服务客户端更加容易。要使用Feign,请创建一个接口并对其进行注释。它具有可插入的注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud添加了对Spring MVC注释的支持,并支持使用HttpMessageConvertersSpring Web中默认使用的注释。Spring Cloud集成了Ribbon和Eureka以及Spring Cloud LoadBalancer,以在使用Feign时提供负载平衡的http客户端。
- 使编写Java Http客户端更加容易 使用 RestTemplate+Ribbon 时,利用 RestTemplate 对http 请求的封装处理,形成一套模板化的调用方法,但是在实际中,由于对服务的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以Feign在此基础上做了进一步封装,由他来帮助我们定义和实现服务接口的定义。在Feign的实现下我们只需要创建一个接口并使用注解来配置它(以前是Dao接口上标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可)。自动封装服务调用客户端的开发量。
- Feign集成了Ribbon 利用Ribbon维护了Payment的服务列表信息,并且实现了轮询实现客户端的负载均衡。而与Ribbon不同的是,feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现服务调用。
POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
YML
使用的是consul注册中心
- ribbon.ReadTimeout:建立起连接后从服务器读取资源所用时间
- ribbon.ConnectTimeout:建立连接所用时间;OpenFeign默认1s
- logging.level.com.live.service.OpenFeignService:开启OpenFeign日志
server:
port: 80
spring:
application:
name: cloud-comsumer-consul-order
cloud:
consul:
discovery:
instance-id: order80
prefer-ip-address: true
service-name: ${spring.application.name}
host: localhost
port: 8500
ribbon:
ReadTimeout: 6000
ConnectTimeout: 6000
logging:
level:
com.live.service.OpenFeignService: debug
启动类
@EnableFeignClients
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class OpenFeignConsul80 {
public static void main(String[] args) {
SpringApplication.run(OpenFeignConsul80.class,args);
}
}
Service
@FeignClient(value=“微服务名”)
@RequestMapping(value=“和微服务提供者的路径一致”)
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
@Component
@FeignClient(value = "cloud-provider-consul-payment")
public interface OpenFeignService {
@GetMapping(value = "/consul/provider/test")
String consulTest();
@GetMapping(value = "/consul/provider/openfeign/test")
String openfeignTest();
@GetMapping(value = "/consul/provider/openfeign/timeout/test")
String openfeignTimeOutTest();
}
Controller
import com.live.service.OpenFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class OpenFeignController {
@Resource
private OpenFeignService openFeignService;
@GetMapping("/consumer/openfeign/test")
public String openfeignTest() {
return openFeignService.openfeignTest();
}
@GetMapping(value = "/consul/consumer/openfeign/timeout/test")
public String openfeignTimeOutTest() {
return openFeignService.openfeignTimeOutTest();
}
}
Config-日志
- 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 OpenFeignLogConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
-
还需再application.yml或properties中配置
com.live.service.OpenFeignService
:自定
logging: level: com.live.service.OpenFeignService: debug