SpringCloud——Openfeign 服务接口调用

目录

1. Feign 

1.1 Feign 的作用

1.2 Feign 和 OpenFeign 的区别

2. SpringCloud 整合 Openfeign

2.1 添加 pom

2.2 写 YML

2.3 启动类

2.4 业务类

2.5 测试

3. openFeign 超时控制

4. Openfeign 打印日志

4.1 日志级别

4.2 开启日志


1. Feign 

1.1 Feign 的作用

Feign 旨在使编写 Java Http 客户端变得更容易。

使用 Ribbon+ RestTemplate 时,利用 RestTemplate 对 http 请求的封装处理,形成了一套模版化的调用方法。

但是在实际开发中,于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign 在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。

在 Feign 的实现下只需创建一个接口并使用注解的方式来配置它,即可完成对服务提供方的接口绑定,简化了使用 Spring cloud Ribbon时,自动封装服务调用客户端的开发量。

Feign 集成了 Ribbon ,利用 Ribbon 维护了 Payment 的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与 Ribbon 不同的是,通过 feign 只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。

1.2 Feign 和 OpenFeign 的区别

Feign

 OPenFeign

Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。

Feign使用方式:使用Feign的注解定义接口,调用这个接口就可以调用服务注册中心的服务

OpenFeign是Spring Cloud在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。

QpenFeign的@FeignClient可以解析SpringMVC的RequestMapping 注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。


    org.springframework.cloud
    spring-cloud-starter-feign


    org.springframework.cloud
    spring-cloud-starter-openfeign

 

 

2. SpringCloud 整合 Openfeign

新建服务消费者 model:cloud-consumer-feign-order80

2.1 添加 pom


    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
        com.zth.springcloud
        cloud-api-commons
        ${project.version}
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-actuator
    

2.2 写 YML

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

 

2.3 启动类

@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
    public static void main(String[] args){
        SpringApplication.run(OrderFeignMain80.class,args);
    }
}

【注】需添加:@EnableFeignClients

2.4 业务类

a. 业务逻辑接口 +@FeignClient 配置调用 provider 服务

@Service
@FeignClient(value = "cloud-payment-service")
public interface PaymentFeignService {
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") long id);
}

SpringCloud——Openfeign 服务接口调用_第1张图片

b. 控制层 controller

@RestController
public class OrderFeignController {
    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        return paymentFeignService.getPaymentById(id);
    }
}

2.5 测试

SpringCloud——Openfeign 服务接口调用_第2张图片

3. openFeign 超时控制

openFeign 默认等待时间是 1 秒。

服务提供者 controller:

@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout(){
    try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}
    return serverPort;
}

服务消费者 service 接口:

@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout();

服务消费者 controller:

@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout(){
   return paymentFeignService.paymentFeignTimeout();
}

测试: 

SpringCloud——Openfeign 服务接口调用_第3张图片

在 服务消费者 YML 添加:

ribbon:
  ReadTimeout:  5000
  ConnectTimeout: 5000

 

SpringCloud——Openfeign 服务接口调用_第4张图片

4. Openfeign 打印日志

Feign 提供了日志打印功能,可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节。

4.1 日志级别

  1. NONE:默认的,不显示任何日志;
  2. BASIC:仅记录请求方法、URL、 响应状态码及执行时间;
  3. HEADERS:除了 BASIC 中定义的信息之外,还有请求和响应的头信息;
  4. FULL:除了 HEADERS 中定义的信息之外,还有请求和响应的正文及元数据。

 

4.2 开启日志

a. 配置日志 bean

@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

 

b. YML 配置需要开启日志的客户端

logging:
  level:
    com.zth.springcloud.service.PaymentFeignService: debug

 

开启结果:

SpringCloud——Openfeign 服务接口调用_第5张图片

 

 

 

你可能感兴趣的:(SpringCloud,分布式,ribbon,spring)