栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

006-SpringCloud系列之OpenFeign服务接口调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

006-SpringCloud系列之OpenFeign服务接口调用

文章目录
    • Feign能干什么
    • Feign集成了Ribbon
    • Feign和OpenFeign的区别
    • OpenFeign使用步骤
      • 新建007-cloud-provider-order-openfeign80
      • pom.xml
      • application.yml
      • 主启动
      • 业务类
      • 控制层
      • 测试
    • OpenFeign超时控制
    • OpenFeign日志打印功能
      • 日志级别
      • 配置日志bean

涉及代码地址:https://gitee.com/codinginn/SpringCloudTurorial/tree/master/SpringCloudModules

OpenFeign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可。

GitHub地址:https://github.com/spring-cloud/spring-cloud-openfeign

Feign能干什么

Feign旨在使编写Java Http客户端变得更容易。前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定。

Feign集成了Ribbon

与Ribbon不同的是,通过Feign只需要定义服务绑定接口即可优雅而简单的进行服务调用。

Feign和OpenFeign的区别

OpenFeign使用步骤

微服务调用接口+@FeignClient

新建007-cloud-provider-order-openfeign80 pom.xml


    
        SpringCloudModules
        com.hashnode
        1.0-SNAPSHOT
    
    4.0.0

    007-cloud-provider-order-openfeign80

    
        8
        8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

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

        
        
        
        
        
        
        
        
        
        
        
        

        
        
        
        
        
        
        




        
            com.hashnode
            003-cloud-api-commons
            1.0-SNAPSHOT
        

        
            cn.hutool
            hutool-all
            5.1.0
        

        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
        
        




        
        
            junit
            junit
        
        
        
            log4j
            log4j
        
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
        
            org.webjars
            bootstrap
        

        
            org.projectlombok
            lombok
        



    


application.yml
server:
  port: 80

spring:
  application:
    name: cloud-order-service


eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka

#      设置feign客户端超时时间 Openfeign默认支持ribbon
ribbon:
#      指的是建立连接所用的时间,适用于网络情况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
#      指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimout: 5000


#日志相关
logging:
  level:
    com.hashnode.service.PaymentFeignService: debug
主启动
package com.hashnode;

import com.hashnode.myrule.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
//@EnableEurekaClient
@EnableFeignClients
public class OrderMainOpenfeign80 {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(OrderMainOpenfeign80.class, args);
    }
}
业务类

新建PaymentFeignService接口并新增注解@FeignClient

package com.hashnode.service;

import com.hashnode.model.CommonResult;
import com.hashnode.model.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;

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id")Long id);

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

控制层
package com.hashnode.controller;

import com.hashnode.lb.LoadBalancer;
import com.hashnode.model.CommonResult;
import com.hashnode.model.Payment;
import com.hashnode.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@RestController
@Slf4j
public class OrderController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @Resource
    private PaymentFeignService paymentFeignService;

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


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


}

测试

先启动2个eureka集群7001/7002

再启动2个微服务8001/8002

启动OpenFeign

访问http://localhost/consumer/payment/get/1

可以看到Feign自带负载均衡的功能。

OpenFeign超时控制

服务提供方8001故意写暂停程序演示出错情况

  • PaymentController.java
package com.hashnode.controller;

import com.hashnode.model.CommonResult;
import com.hashnode.model.Payment;
import com.hashnode.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;


    ...

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

   ...


}

服务消费方80添加超时方法

  • PaymentFeignService.java
package com.hashnode.service;

import com.hashnode.model.CommonResult;
import com.hashnode.model.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;

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    ...

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

测试

原因是OpenFeign默认等待1秒钟,如果响应超时会报错。为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制。OpenFeign默认支持Ribbon:

如果需要设置Feign客户端的超时控制,可以在yml中添加如下配置:

OpenFeign日志打印功能

Feign 提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节,说白了就是对Feign接口的调用情况进行监控和输出。

日志级别

NONE:默认的,不显示任何日志;

BASIC:仅记录请求方法、URL、响应状态码及执行时间;

HEADERS:除了 BASIC 中定义的信息之外,还有请求和响应的头信息;

FULL:除了 HEADERS 中定义的信息之外,还有请求和响应的正文及元数据。

配置日志bean
package com.hashnode.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//配置日志
@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

YML文件里需要开启日志的Feign客户端

后台日志查看:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838051.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号