【SpringCloud】11 OpenFeign 服务调用
1、新建模块,模块名称: Consumer-Order-OpenFeign-Port-80
2、配置POM
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId </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>
OpenFeign也整合了Ribbon

2、Application.yml配置信息
server: port: 80 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/,http://eureka7002.cn:7002/eureka/ register-with-eureka: false
3、启动类:
package cn.dzz.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; /** * @author DaiZhiZhou * @file SpringCloud-ATGG-2020 * @create 2020-08-26 23:40 */ @SpringBootApplication @EnableFeignClients public class ConsumerOrderOpenFeignPort80Application { public static void main(String[] args) { SpringApplication.run(ConsumerOrderOpenFeignPort80Application.class, args); } }
4、编写OpenFeign接口:
package cn.dzz.springcloud.service; import cn.dzz.springcloud.component.JsonResult; import cn.dzz.springcloud.entity.Payment; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @author DaiZhiZhou * @file SpringCloud-ATGG-2020 * @create 2020-08-26 23:43 */ @Component @FeignClient(value = "PROVIDER-PAYMENT-SERVICE") // 我要调用的服务名是? public interface FeignPaymentService { // 将8001服务的接口注册到OpenFeign中 但是注解在类上的@RequestMapping是不是无法获取到? // 所以这里需要手写接口信息 @GetMapping("/get/{id}") JsonResult<Payment> getPaymentById(@PathVariable("id") Long id); }
注意这个请求映射配置好:

5、控制器,服务调用方法:
package cn.dzz.springcloud.controller; import cn.dzz.springcloud.component.JsonResult; import cn.dzz.springcloud.entity.Payment; import cn.dzz.springcloud.service.FeignPaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author DaiZhiZhou * @file SpringCloud-ATGG-2020 * @create 2020-08-26 23:58 */ @RestController @Slf4j public class OpenFeignOrderController { @Resource private FeignPaymentService feignPaymentService; @GetMapping("consumer/payment/get/{id}") public JsonResult<Payment> getPaymentById(@PathVariable("id") Long id) { return feignPaymentService.getPaymentById(id); } }
6、运行测试,Eureka集群启动7001 + 7002 启动,然后是服务8001 + 8002 启动,再启动OpenFeign80
访问接口是可以的:
http://localhost/consumer/payment/get/1 {"code":200,"message":"查询数据成功 端口:8002","data":{"id":1,"serial":"aaabbb111"}}
OpenFeign超时控制:
所谓超时,就是被调用的服务接口
返回的数据需要一定时间完成,而调用的服务接口马上就能完成。
如果在调用的服务接口所规定的时间内,被调用服务没有返回数据,则被认定为超时(Time-out)
所以解决方案是一致化,也就是调用的服务接口 + 被调用的服务接口 一致规定时间长度
OpenFeign默认的时间长度是1秒钟,超过则报错
如何配置?
Consumer-Order-OpenFeign-Port-80端口
server: port: 80 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/,http://eureka7002.cn:7002/eureka/ register-with-eureka: false ribbon: ReadTimeout: 3000 # 调用的服务接口 与 被调用的服务接口 相连接需要的时间 ConnectTimeout: 3000 # 连接成功后,返回数据的时间长度的最长规定,超过则报错
OpenFeign日志增强:
日志级别:
None 默认不打印日志
Basic 仅记录请求方法、服务接口地址、响应状态码 & 执行时间
Headers 包括Basic级别的所有信息, 加上请求与响应头的信息
FULL 包括Headers的所有信息,加上请求和响应的正文 + 元数据

配置OpenFeign日志信息Bean:
在Consumer-Order-OpenFeign-Port-80服务模块下面
package cn.dzz.springcloud.configuration; import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author DaiZhiZhou * @file SpringCloud-ATGG-2020 * @create 2020-08-27 9:01 */ @Configuration public class OpenFeignConfiguration { @Bean public Logger.Level getOpenFeignLoggerLevel(){ return Logger.Level.FULL; } }
然后是在YML的配置:
server: port: 80 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/,http://eureka7002.cn:7002/eureka/ register-with-eureka: false ribbon: ReadTimeout: 3000 # 调用的服务接口 与 被调用的服务接口 相连接需要的时间 ConnectTimeout: 3000 # 连接成功后,返回数据的时间长度的最长规定,超过则报错 logging: level: cn.dzz.springcloud.configuration.OpenFeignConfiguration: debug

浙公网安备 33010602011771号