OpenFeign的常规使用

架构:

一.新建module

引入依赖:

 <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

yml配置;

server:
  port: 80

spring:
  application:
    name: cloud-consumer-openfeign-order
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true #优先使用服务ip进行注册
        service-name: ${spring.application.name}

二.公共模块编写feign调用接口:

package com.wen.cloud.apis;

import com.wen.cloud.entities.Pay;
import com.wen.cloud.resp.ResultData;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value = "cloud-payment-service")//对应注册中心的模块命名
public interface PayFeignApi {
    @PostMapping(value = "/pay/add")
    public ResultData addPay(@RequestBody Pay pay);

    @GetMapping(value = "/pay/get/{id}")
    public ResultData getById(@PathVariable("id") Integer id);

    @GetMapping("/pay/getInfo")
    public String getInfoByConsul();
}

cloud-payment-service中的方法体与PayFeignApi的对应关系:

将cloud-consumer-feign-order模块启动类添加注释,用于开启@FeignClient(value = "cloud-payment-service")注解

三:项目架构:

consumer中的orderController会自动装配PayFeignApi接口,并调用接口中提供的方法。该接口会通过@FeignClient(value = "cloud-payment-service")中的value名取注册中心寻找对应的微服务模块,并匹配该模块方法的路径并调用。

4.测试:

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用OpenFeign,首先需要创建一个启动类,将其标记为@SpringBootApplication,并在注解中排除掉DataSource自动配置,同时添加@EnableFeignClients、@EnableEurekaClient和@EnableDiscoveryClient注解来启用Feign客户端和服务注册与发现功能。 接下来,需要定义一个接口,并使用@FeignClient注解来标识该接口是一个Feign客户端,并指定要调用的服务。在接口中,可以使用REST风格的方式来定义接口请求服务的方法。例如,可以使用@RequestMapping注解来指定请求路径和请求方法,然后在方法中返回要调用的服务的响应结果。 要在项目中使用OpenFeign,需要添加OpenFeign的依赖。可以在项目的pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.2.RELEASE</version> </dependency> ``` 最后,需要配置Feign接口的错误回滚配置。这可以通过在Feign接口上添加注解来实现。 综上所述,使用OpenFeign的步骤如下: 1. 创建一个启动类,并标记为@SpringBootApplication,同时排除DataSource自动配置,添加@EnableFeignClients、@EnableEurekaClient和@EnableDiscoveryClient注解。 2. 定义一个接口,并使用@FeignClient注解来标识该接口是一个Feign客户端,指定要调用的服务。 3. 在接口使用REST风格的方式定义接口请求服务的方法,并添加相应的注解来配置请求路径和请求方法。 4. 添加OpenFeign的依赖到项目的pom.xml文件中。 5. 配置Feign接口的错误回滚配置,可以通过在接口上添加注解来实现。 请注意,确保在项目中正确配置了服务注册中心和相应的服务提供者。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [OpenFeign的理解和使用](https://blog.csdn.net/m0_45016797/article/details/123633386)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [OpenFeign的简单使用](https://blog.csdn.net/qq_50626505/article/details/124060848)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值