OpenFeign的使用

OpenFeign是什么?

Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简单。使用Feign只需要创建一个接口加上对应的注解,比如:FeignClient注解。
OpenFeign貌似只能调用提供者的controller,即写一个提供者controller的接口,消费者模块来调用这个接口,就还是相当于提供者的controller,和restTemplate没有本质区别

OpenFeign可以做什么?

OpenFeign主要是用来使java Http客户端变得更容易。

怎么使用OpenFeign?

1.新建一个消费者模块,因为OpenFeign自带负载均衡,所以不用手动配置。
2.引入依赖

<dependencies>
    <!-- Open Feign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!-- eureka Client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
        <groupId>com.krisswen.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
	3.编写配置文件
server:
  port: 80
spring:
  application:
    name: cloud-consumer-feign-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

4.编写启动类

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

5.编写controller

@RestController
@SuppressWarnings(value = "all")
public class CustomerFeignController {

    @Resource
    PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/feign/payment/{id}")
    public CommonResult<Payment> getPaymentId(@PathVariable("id") Integer id){
        return paymentFeignService.getPaymentId(id);
    }

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

6.编写service

@Component
@FeignClient("cloud-service-payment")  //服务提供者
public interface PaymentFeignService {
    @GetMapping("/findById/{id}")   //这里的路径是调用服务提供者controller里的访问路径
    CommonResult<Payment> getPaymentId(@PathVariable("id") Integer id);


    @GetMapping("/payment/feign/timeout")
    String timeOut();
}

最后目录结构是这样

在这里插入图片描述
然后启动我们的两台Eureka,和两台服务提供方
然后启动新建的feign模块,
访问:localhost:/consumer/feign/payment/1即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值