OpenFegin基础使用方式

本文介绍了OpenFeign,一种用于简化微服务架构中服务间调用的工具,它通过声明式接口和SpringCloud集成提供了高级功能,如负载均衡和请求重试。文章详细说明了如何在SpringBoot项目中集成OpenFeign并展示了其使用方法和配置选项。
摘要由CSDN通过智能技术生成

介绍:
OpenFeign是一种基于接口的声明式Web服务客户端,它在微服务架构中起到了简化服务间调用的作用。它的核心思想是通过定义接口来实现服务间通信,将服务调用的过程封装起来,使得开发者可以像调用本地方法一样来调用远程服务,极大地降低了使用RESTful服务的复杂度和工作量

具体来说,OpenFeign的主要作用包括:

  • 简化服务调用:在微服务架构中,服务间的调用可能非常频繁,并且可能需要调用多个不同的服务才能完成一个业务流程。OpenFeign通过声明式的方式,简化了这些调用的过程,使得开发者可以更加专注于业务逻辑的实现,而不是底层的通信细节。
  • 提供高级功能:OpenFeign还提供了一些高级功能,如负载均衡、服务发现、请求重试等。这些功能可以帮助开发者更好地管理和优化微服务架构中的服务调用,提高系统的可用性和稳定性。
  • 整合Spring Cloud:作为Spring Cloud的服务调用中间件,OpenFeign可以解析Spring MVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。这使得在Spring Cloud环境下使用OpenFeign变得非常方便。

使用方式

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--nacos-服务注册发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--nacos-配置文件-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <!--1. 添加openfeign依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

在启动类开启OpenFeign
在这里插入图片描述

@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
    public static void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(OrderApplication.class, args);
          String property = applicationContext.getEnvironment().getProperty("user.name");
          System.out.println(property);

    }

}

定义Feign接口


// 局部配置
//@FeignClient(name = "stock-server",path = "/stock", configuration = FeignConfig.class)
//name  对应的是被调用的服务名, path是controller的根路径
@FeignClient(name = "stock-server",path = "/stock")
public interface StockServer {
	
	//value对应被请求的接口路径,method是请求方式,在openfegin中格式比较严谨,如果需要在路径中传递参数
	//需要在定义好 例如 public String reduct( @PathVariable(id) int id);
    @RequestMapping(value = "/reduct",method = RequestMethod.GET)
    public String reduct( @PathVariable(id));
}

具体使用方式


	// 引入需要使用的openfeign
    @Autowired
    StockServer StockServer;

    @RequestMapping("/add")
    public String add(){
		// 使用StockServer直接调用指定的方法
        String reduct = StockServer.reduct();
        System.out.println("下单成功!");
      return reduct;
    }

OpenFeign配置文件

  • 日志配置
  • 契约配置
  • 超时时间配置
  • 自定义拦截器配置

日志配置

  • 使用Configuration配置为全局配置文件,所有的openfeign都会使用当前配置信息
  • 不使用Configuration则不会生效
  • @FeignClient(name = “stock-server”,path = “/stock”, configuration = FeignConfig.class),在接口定义的地方使用configuration则表示当前的接口使用此配置信息

文件方式配置

@Configuration
public class FeignConfig {


    // openfeign全局配置
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    /**
     * 修改契约配置,支持Feign原生的注解
     *
     * @return
     */
    @Bean
    public Contract feignContract() {
        return new Contract.Default();
    }


    //全局配置超时时间
    @Bean
    public Request.Options options() {
        return new Request.Options(5000, 10000);
    }

YML类型配置

feign:
  client:
    config:
      stock-server:
        # 连接超时时间,默认2s
        connect-timeout: 1000
        # 请求处理超时时间,默认5s
        readTimeout: 10000
        logger-level: BASIC # 日志登记
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值