总体调用:
1. 配置
1.1 新建module:cloud-consumer-openfeign-order808
1.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.pyh.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-openfeign-order808</artifactId>
<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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</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>
</dependency>
<dependency>
<groupId>com.pyh.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
1.3 application.yaml
server:
port: 808
spring:
application:
name: cloud-consumeropenfeign-order
eureka:
client:
register-with-eureka: false # false:不把 eureka 自身注册在注册中心里面, 不作高可用的情况下使用这个
fetch-registry: true # false:不从 eureka 上获取服务的注册信息
service-url:
# defaultZone: http://127.0.0.1:7001/eureka/ # 单例版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
1.4 主启动类 @EnableFeignClients
package com.pyh.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class OrderOpenfeignMain808 {
public static void main(String[] args) {
SpringApplication.run(OrderOpenfeignMain808.class,args);
}
}
1.5 添加service接口,调用payment端
package com.pyh.springcloud.service;
import com.pyh.springcloud.entities.CommonResult;
import com.pyh.springcloud.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(value = "cloud-payment-service")
public interface PaymentFeignService {
@GetMapping("/getpay/{id}")
public CommonResult getPayment(@PathVariable("id") Long id);
@PostMapping("/addpay")
public CommonResult addPayment(@RequestBody Payment payment);
}
1.6 controller
没有数据库调用需求,所以exclude了数据库的自动配置类
package com.pyh.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableFeignClients
public class OrderOpenfeignMain808 {
public static void main(String[] args) {
SpringApplication.run(OrderOpenfeignMain808.class,args);
}
}
1.7 配置超时控制
在 application.yaml 添加:
ribbon:
# 建立连接所允许的最大时间,适用于网络正常情况下,两端连接所用时间
ReadTimeout: 5000
# 指的是建立连接后, 从服务端读取到可用资源所用的时间
ConnectionTimeout: 5000
至此,即使服务端 Thread.sleep(3000),也可得到结果,
不配置则返回超时
@GetMapping("/consumer/getpay/timeout")
public String getFeignPaymentTimeout(){
// 客户端一般默认等一秒钟
return paymentFeignService.paymentFeignTimeout();
}
1.8 Openfeign 日志打印功能
对Feign接口的调用情况进行监控和输出
参考:设置feign配置日志级别
指定具体order service 的日志级别
logging:
level:
# com.pyh.springcloud.service.PaymentFeignService: debug
com.pyh.springcloud.service: debug
feign:
client:
config:
default: # feign 全局日志
# cloud-payment-service: #也可以指定某个 feign 服务名称
connectTimeout: 5000
readTimeout: 5000
loggerLevel: FULL # 日志级别 FULL / headers / basic / none 默认
# fiegnName: # fiegnName服务请求的配置,优先defalut配置。
# loggerLevel: FULL # 日志级别 FULL / headers / basic / none 默认
# connectTimeout: 5000 # 链接超时时间
# readTimeout: 5000 # 请求
# errorDecoder: com.example.SimpleErrorDecoder #异常处理
# retryer: com.example.SimpleRetryer # 重试策略
# defaultQueryParameters: # 默认参数条件
# query: queryValue
# defaultRequestHeaders: # 默认默认header
# header: headerValue
# requestInterceptors: # 默认拦截器
# - com.example.FooRequestInterceptor
# - com.example.BarRequestInterceptor
# decode404: false #404响应 true-直接返回,false-抛出异常
# encoder: com.example.SimpleEncoder #传输编码
# decoder: com.example.SimpleDecoder #传输解码
# contract: com.example.SimpleContract #传输协议