Feign是一个声明式的WebService客户端, 使用Feign能让编写Web Service客户端更加简单
它使用的方法是定义个服务接口然后在上面添加注解,Feign也支持可插拔式的编码器和解码器,SpringCloud对feign进行了封装支持SpringMVC中标准注解和httpMessageConverteres。Feign还可以与EUREKA和Ribbon组合使用支持负载均衡
能干嘛
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GSbZR5YH-1601736701617)(C:\Users\74551\AppData\Roaming\Typora\typora-user-images\image-20200818161424095.png)]
Feign 和 OPenFeign的区别
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qf3WCKZt-1601736701622)(C:\Users\74551\AppData\Roaming\Typora\typora-user-images\image-20200818162227529.png)]
自带负载均衡的配置项
1.建Model cloud-consumer-feign-order80 消费端使用
2.写pom
<dependencies> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--监控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>3.改yml
server: port: 80 # 模拟不用加端口 spring: application: name: cloud-order-service eureka: client: # false 表示不向注册中心注册自己 register-with-eureka: true # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务 fetch-registry: true service-url: # 设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址 # defaultZone: http://localhost:7001/eureka/ 单机版 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 # 设置feign客户端超时时间(OpenFeign默认支持ribbon) ribbon: # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间 ReadTimeout: 5000 # 指的是建立连接后从服务器读取到可用资源所用的时间 ConnectTimeout: 5000 logging: level: # feign 日志以及什么级别监控哪个接口 com.atguigu.springcloud.service.PaymentFeignService: debug4.改启动类
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class OrderFeignMain80 { public static void main(String[] args) { SpringApplication.run(OrderFeignMain80.class, args); } }5.业务类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J0W9GNKf-1601736701623)(C:\Users\74551\AppData\Roaming\Typora\typora-user-images\image-20200818170826105.png)]
调用其他提供服务的模块
@Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentFeignService { @GetMapping(value="/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id); @GetMapping("/payment/feign/timeout") public String paymentFeignTimeout(); }6.控制层
@RestController @Slf4j public class OrderFeignController { @Resource private PaymentFeignService paymentFeignService; @GetMapping(value = "/consumer/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) { return paymentFeignService.getPaymentById(id); } @GetMapping("/consumer/payment/feign/timeout") public String paymentFeignTimeout() { //openfeign-ribbon 客户端默认等待1s return paymentFeignService.paymentFeignTimeout(); } }小总结
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XF7tVcuK-1601736701626)(C:\Users\74551\AppData\Roaming\Typora\typora-user-images\image-20200818171040614.png)]
超时设置 OPenFeign 默认等待1秒钟 超过后会报错
# 设置feign客户端超时时间(OpenFeign默认支持ribbon) ribbon: # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间 ReadTimeout: 5000 # 指的是建立连接后从服务器读取到可用资源所用的时间 ConnectTimeout: 5000日志打印功能
1.配置Bean
@Configuration public class FeignConfig { /** * feignClient配置日志级别 * * @return */ @Bean public Logger.Level feignLoggerLevel() { // 请求和响应的头信息,请求和响应的正文及元数据 return Logger.Level.FULL; } }2.yml增加配置
logging: level: # feign日志以什么级别监控哪个接口 com.atguigu.springcloud.service.PaymentFeignService: debug