springcloud中使用openfeign来优化接口调用

简单介绍在springcloud中使用openfeign来优化接口调用

目录

一、引入依赖

java 复制代码
        <!--添加openFeign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

二、为服务提供者编写openfeign接口

如需要调用的远程Controller接口如下(服务提供者接口)

java 复制代码
@RestController
@RequestMapping("/stock")
public class StockController {

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

    @GetMapping("/reduce")
    public String reduce(){
        System.out.println("扣减库存:"+port);
        return "扣减库存:"+port;
    }
}

需要编写的openfeign接口如下(服务消费者openfeign接口)

java 复制代码
/**
 * name 指定调用rest接口所对应的服务名
 * path 指定调用rest接口所在的RequestMapping路径
 */
@FeignClient(name = "stock-service",path = "/stock")
public interface StockFeignService {
    // 声明需要调用rest接口对应的方法
    @GetMapping("/reduce")
    String reduce();
}

三、服务消费者调用定义的openfeign接口

随后在服务消费者Controller接口中调用定义的openfeign接口,达到像调用本地接口一样调用远程接口

java 复制代码
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private StockFeignService stockFeignService;

    @GetMapping("/addOrder")
    public String addOrder(){
        System.out.println("下单成功");
// String msg = restTemplate.getForObject("http://stock-service/stock/reduce", String.class);
        String msg = stockFeignService.reduce();
        return "hello feign !"+msg;
    }
}

四、项目结构

五、日志级别配置

在服务消费者端往往需要查看调用服务提供者接口的日志信息,包括接口地址,接口返回参数等等,因此需要对openfeign日志进行配置,以下提供三种配置方式

1、通过配置类进行全局配置

定义一个配置类

java 复制代码
@Configuration
public class FeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

需要在yml配置文件中开启日志

yaml 复制代码
# springboot默认的日志级别为info,feign的debug日志级别就不会输入,所以需要修改feign目录下对应的日志级别为debug
logging:
  level:
    com.tuling.order.feign: debug

该方式会作用于所有的服务提供者,即调用所有的服务提供者都会打印相应级别的日志

2、通过配置类进行局部配置

将配置类FeignConfig中的@Configuration注解去掉

java 复制代码
public class FeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

随后修改对应的FeignService中@FeignClient注解,加上configuration = FeignConfig.class

java 复制代码
@FeignClient(name = "stock-service",path = "/stock",configuration = FeignConfig.class)
public interface StockFeignService {
    // 声明需要调用rest接口对应的方法
    @GetMapping("/reduce")
    String reduce();
}

最后注意同样需要在yml配置文件中开启日志

这样的方式可以通过编辑服务提供者对应的FeignService来自定义开启哪些服务提供者的日志

3、通过配置文件配置

yaml 复制代码
# springboot默认的日志级别为info,feign的debug日志级别就不会输入,所以需要修改feign目录下对应的日志级别为debug
logging:
  level:
    com.tuling.order.feign: debug

feign:
  client:
    config:
      stock-service:
        logger-level: BASIC

其中stock-service为服务提供者对应的服务名

相关推荐
地图工程师12 小时前
Nacos + Dubbo3 实现微服务的Rpc调用
spring cloud·rpc·dubbo3
瑞金彭于晏21 小时前
微服务架构与传统的单体架构有什么区别?微服务架构(Spring Cloud + Maven)强在哪?
spring cloud·微服务·架构
蝴蝶不愿意1 天前
Docker基础-常见命令
java·笔记·学习·spring cloud·docker
别惹CC2 天前
Spring Boot 3 整合 Spring Cloud Gateway 工程实践
java·spring boot·后端·spring cloud·gateway
zzyh1234563 天前
springcloud负载均衡策略有哪些
java·spring cloud·负载均衡
希忘auto3 天前
Spring Cloud之注册中心之Nacos的安装
java·后端·spring·spring cloud
Gy-1-__4 天前
【springcloud】快速搭建一套分布式服务springcloudalibaba(一)
后端·spring·spring cloud
茶本无香5 天前
kafka+spring cloud stream 发送接收消息
spring cloud·kafka·java-zookeeper
陌殇殇5 天前
002 SpringCloudAlibaba整合 - Feign远程调用、Loadbalancer负载均衡
java·spring cloud·微服务
m0_675447086 天前
Java版企电子招标采购系统源业码Spring Cloud + Spring Boot +二次开发+ MybatisPlus + Redis
java·spring cloud·企业电子招投标系统源码·招投标系统源码