目录
一、OpenFeign实现服务调用
1.1、创建openfeign微服务
1、导入pom依赖
XML
<dependencies>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--SpringCloud consul discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.cjc.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--web + actuator-->
<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>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--hutool-all-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--fastjson2-->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<!-- swagger3 调用方式 http://你的主机IP地址:5555/swagger-ui/index.html -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、配置yml及主启动类
java
server:
port: 80
spring:
application:
name: cloud-consumer-openfeign-order
####Spring Cloud Consul for Service Discovery
cloud:
consul:
host: localhost
port: 8500
discovery:
prefer-ip-address: true #优先使用服务ip进行注册
service-name: ${spring.application.name}
java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启openfeign
public class MainOpenFeign80 {
public static void main(String[] args) {
SpringApplication.run(MainOpenFeign80.class,args);
}
}
3、相关代码及接口
java
@FeignClient(value = "cloud-payment-service")
public interface PayFeignApi {
//添加
@GetMapping("/pay/add")
public ResultData addOrder(PayDTO payDTO);
//查询
@GetMapping("/pay/getById/{id}")
public ResultData getByIdOrder(@PathVariable("id") Integer id);
//负载均衡
@GetMapping("/pay/info")
public ResultData getOrLoad();
}
java
@RestController
public class FeignController {
@Autowired
private PayFeignApi payFeignApi;
@PostMapping("/feign/pay/add")
public ResultData addPay(@RequestBody PayDTO payDTO){
System.out.println("添加调用成功");
return payFeignApi.addOrder(payDTO);
}
@GetMapping("/feign/pay/get/{id}")
public ResultData getByPayId(@PathVariable("id") Integer id){
System.out.println("查询调用成功");
return payFeignApi.getByIdOrder(id);
}
@GetMapping("/pay/get/info")
public ResultData getInfo(){
System.out.println("负载均衡调用成功");
return payFeignApi.getOrLoad();
}
}
二、Openfeign超时控制
客户端默认超时时间是60s
2.1、全局默认配置
在yml文件中配置
java
spring:
application:
name: cloud-consumer-openfeign-order
####Spring Cloud Consul for Service Discovery
cloud:
consul:
host: localhost
port: 8500
discovery:
prefer-ip-address: true #优先使用服务ip进行注册
service-name: ${spring.application.name}
openfeign:
client:
config:
default:
#链接超时时间
connect-timeout: 3000
#读取超时时间
read-timeout: 3000
2.2、单个微服务配置
java
spring:
application:
name: cloud-consumer-openfeign-order
####Spring Cloud Consul for Service Discovery
cloud:
consul:
host: localhost
port: 8500
discovery:
prefer-ip-address: true #优先使用服务ip进行注册
service-name: ${spring.application.name}
openfeign:
client:
config:
#单个微服务配置
cloud-payment-service:
#链接超时时间
connect-timeout: 3000
#读取超时时间
read-timeout: 3000
三、重试机制
使用配置类开启
java
@Configuration
public class FeignConfig {
@Bean
public Retryer retryer(){
//return Retryer.NEVER_RETRY;//默认不重试
//最大请求次数为3(1+2),初始间隔时间为100ms,重试间最大间隔时间为1s
return new Retryer.Default(100,1,3);
}
}
四、替换openfeign默认的HttpClient
导入pom
XML
<!-- httpclient5-->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3</version>
</dependency>
<!-- feign-hc5-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hc5</artifactId>
<version>13.1</version>
</dependency>
修改yml文件
java
application:
name: cloud-consumer-openfeign-order
####Spring Cloud Consul for Service Discovery
cloud:
consul:
host: localhost
port: 8500
discovery:
prefer-ip-address: true #优先使用服务ip进行注册
service-name: ${spring.application.name}
openfeign:
client:
config:
#单个微服务配置
cloud-payment-service:
#链接超时时间
connect-timeout: 3000
#读取超时时间
read-timeout: 3000
httpclient: #开启H5
hc5:
enabled: true
开启后:
五、请求响应压缩
修改yml配置文件
java
spring:
application:
name: cloud-consumer-openfeign-order
####Spring Cloud Consul for Service Discovery
cloud:
consul:
host: localhost
port: 8500
discovery:
prefer-ip-address: true #优先使用服务ip进行注册
service-name: ${spring.application.name}
openfeign:
client:
config:
#单个微服务配置
cloud-payment-service:
#链接超时时间
connect-timeout: 3000
#读取超时时间
read-timeout: 3000
httpclient: #开启H5
hc5:
enabled: true
compression:
request:
enabled: true
min-request-size: 2048 #最小触发压缩的大小
mime-types: text/xml,application/xml,application/json #触发压缩数据类型
response:
enabled: true
六、日志打印
修改配置类
java
//feign日志功能等级
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
修改yml配置文件
java
spring:
application:
name: cloud-consumer-openfeign-order
####Spring Cloud Consul for Service Discovery
cloud:
consul:
host: localhost
port: 8500
discovery:
prefer-ip-address: true #优先使用服务ip进行注册
service-name: ${spring.application.name}
openfeign:
client:
config:
#单个微服务配置
cloud-payment-service:
#链接超时时间
connect-timeout: 3000
#读取超时时间
read-timeout: 3000
httpclient: #开启H5
hc5:
enabled: true
compression:
request:
enabled: true
min-request-size: 2048 #最小触发压缩的大小
mime-types: text/xml,application/xml,application/json #触发压缩数据类型
response:
enabled: true
# feign日志以什么级别监控哪个接口
logging:
level:
com:
cjc:
cloud:
apis:
PayFeignApi: debug