openfeign的使用

在这里插入图片描述
首先,加入两个服务提供者一个服务消费者用于测试。

服务消费者openfeign配置:

依赖

<!--Open feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
            <version>3.1.4</version>
        </dependency>

消除缓存警告依赖

		<dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.3.12</version>
        </dependency>

启动类添加注解

@EnableFeignClients

服务消费者创建
在这里插入图片描述
注意是接口
ParOpenFeignService代码

@FeignClient("QIESIYV-ZHUTI-TIGONG")//所要调用的服务名
@Component
public interface ParOpenFeignService {

    @GetMapping(value = "/ceshi/ceshi")//指向服务提供者方法地址
    String ceshi();
}

在这里插入图片描述
CuserController代码

 	@Resource
    private ParOpenFeignService parOpenFeignService;

    @GetMapping(value = "ceshi")
    public String ceshi(){
        return parOpenFeignService.ceshi();
    }

服务提供者创建CeshiController用于测试,方法为返回端口号
在这里插入图片描述

postman测试结果可见实现轮询
在这里插入图片描述
在这里插入图片描述
再测试接口执行5秒,发现未提示超时,可见新版openfeign的默认超时时间未再设置为1s

注意:

  1. 在Service接口部分,若要请求的方法请求方式为get且有参数,则需要添加@SpringQueryMap如,不添加的话服务提供方会报Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘POST’ not supported]的错误,即使你用的是@GetMapping。
    在这里插入图片描述

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

配置类

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

@Configuration
public class FeignConfig {
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;//级别
    }
}

yml

logging:
  level:
    qiesiyv.mainbody.service.*: debug #包名.类名: debug 以debug的形式打印日志

效果
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
OpenFeign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加容易。通过使用OpenFeign,我们可以定义一个接口用于调用远程Web服务,然后在运行时生成一个实现该接口的代理类。 以下是使用OpenFeign的步骤: 1. 添加依赖 在Maven项目中,我们需要添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 创建Feign客户端接口 定义一个接口,用于调用远程Web服务。该接口通常会使用Spring MVC注解来定义请求路径、请求参数等信息。 ```java @FeignClient(name = "service-name") public interface MyFeignClient { @RequestMapping(value = "/api/resource", method = RequestMethod.GET) Resource getResource(@RequestParam("id") Long id); } ``` 其中,@FeignClient注解用于定义Feign客户端的名称,name属性指定了远程服务的名称。@RequestMapping注解则用于定义请求路径和请求方法。 3. 注入Feign客户端 在需要调用远程服务的地方,我们可以通过@Autowired注解来注入Feign客户端。 ```java @RestController public class MyController { @Autowired private MyFeignClient myFeignClient; @GetMapping("/resource/{id}") public Resource getResource(@PathVariable Long id) { return myFeignClient.getResource(id); } } ``` 这里,我们将Feign客户端注入到了控制器中,在控制器中调用了getResource方法。 4. 配置Feign客户端 我们可以通过配置文件来配置Feign客户端的行为。例如,我们可以配置连接超时时间、读取超时时间等。 ```yaml feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 ``` 这里,我们将连接超时时间和读取超时时间都设置为了5秒。 以上就是使用OpenFeign的基本步骤,它使得调用远程Web服务变得更加容易。同时,OpenFeign还提供了很多高级特性,例如请求拦截器、响应拦截器等,可以帮助我们更好地管理Web服务客户端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翎墨袅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值