1.概述
1.OpenFeign是什么
https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/#spring-cloud-openfeign
Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需 创建一个接口并在接口上添加注解即可。
2.OpenFeign使用步骤
1.接口+注解
微服务调用接口+@FeignClient
2.新建cloud-consumer-order80-feign
Feign在消费端使用
3.POM
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
4.YML
server:
port: 80
#服务名称
spring:
application:
name: cloud-consumer-order80-openFeign #服务名
#服务注册Eureka配置
eureka:
client:
register-with-eureka: true #表示向注册中心注册自己 默认为true
fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
service-url:
# defaultZone: http://localhost:7001/eureka/ # 入驻单机版Eureka地址
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 入驻集群版Eureka地址
instance:
instance-id: cloud-consumer-order80-openFeign #服务名称
prefer-ip-address: true #访问路径可以显示IP地址
5.主启动类
增加注解:@EnableFeignClients
package com.dt.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;
import org.springframework.cloud.openfeign.FeignClient;
/**
* 主启动类,要开启OpenFeign,需要增加@EnableFeignClients 注解。
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableFeignClients
public class Order80Main_openFeign {
public static void main(String[] args) {
SpringApplication.run(Order80Main_openFeign.class, args);
}
}
6.业务类
1.业务逻辑接口+@FeignClient配置调用provider服务
新建PaymentFeignService接口并新增注解@FeignClient
FeighClient接口不需要实现,因为要进行远程调用。
package com.dt.springcloud.OpenFeignClient;
import com.dt.springcloud.entities.CommonResult;
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;
/**
* 支付OpenFeign客户端, 用于通过OpenClient中的服务名称进行远程接口调用。
*
* @FeignClient中Value值为Eureka中的服务名称。
*/
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentOpenFeignClient {
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id);
}
2.控制层Controller
调用FeignClient接口, 实现远程调用及负载。
package com.dt.springcloud.controller;
import com.dt.springcloud.OpenFeignClient.PaymentOpenFeignClient;
import com.dt.springcloud.entities.CommonResult;
import com.dt.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
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;
@RestController
@Slf4j
public class OrderController {
@Resource
private PaymentOpenFeignClient paymentOpenFeignClient;
/**
* 查询, 调用PaymentOpenFeignClient接口通过OpenFeign调用远程接口。
* @param id
* @return
*/ @GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
return this.paymentOpenFeignClient.getPaymentById(id);
}
}
7.测试
Feign自带负载均衡配置项
先启动2个eureka集群7001/7002
再启动2个微服务8001/8002
启动OpenFeign
http://localhost/consumer/payment/get/1
3.OpenFeign超时控制
1.OpenFeign默认等待1秒,超时后报错。
2.OpenFeign默认支持Ribbon
3.YML中开启OpenFeign客户端超时控制。
#设置feign客户端超时时间
ribbon:
ReadTimeout: 5000 #指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000 #指的是建立连接后从服务器读取到可用资源所用的时间
4.超时设置,故意设置超时演示出错情况
1.服务提供方8001/8002故意写暂停程序
@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeOut()
{
try{
TimeUnit.SECONDS.sleep(3);
}catch (Exception e)
{
e.printStackTrace();
}
return serverPort;
}
2.服务消费方80添加超时方法PaymentFeignService
@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeOut();
3.服务消费方80添加超时方法OrderFeignController
/**
* 测试OpenFeign超时
* @return
*/@GetMapping("/consumer/payment/feign/timeout")
public String paymentFeignTimeOut() {
return this.paymentOpenFeignClient.paymentFeignTimeOut();
}
4.测试
http://localhost/consumer/payment/feign/timeout
4.OpenFeign日志打印功能
openfeign提供了日志打印功能。
Logger有四种类型:NONE(默认)
、BASIC
、HEADERS
、FULL
,通过注册Bean来设置日志记录级别
1.配置类
package com.dt.springcloud.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* OpenFeign日志配置类
*/
@Configuration
public class OpenFeignConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
2.yml配置文件添加
##OpenFeign日志配置
logging:
level:
# feign日志以什么级别监控哪个接口
com.dt.springcloud.service.PaymentFeignService: debug
com.dt.springcloud.OpenFeignClient.PaymentOpenFeignClient: debug