feign一个接口多个方法_spring cloud分布式项目中,提供fegin接口多个@FeignClient里面的name相同的问题...

如图,一个微服务中, 有多个FeignClient时

/**

* @Author Ezio

* @Date 2020/5/21 11:36

*/

@Component

@FeignClient(name = "hauto-merchant", contextId = "ClearClient", path = "clear", fallback = ClearHystrix.class)

public interface ClearClient {

/**

* 查询结算规则信息

* @param commodityIds

* @return

*/

@PostMapping("queryRuleInfo")

ResponseMessage queryClearRule(@RequestParam(value = "commodityIds",required = false) ListcommodityIds);

}

/**

* @author miluo

* @date 2020-05-26

*/

@Component

@FeignClient(name = "hauto-merchant", contextId = "CommodityClient", path = "commodity", fallback = CommodityHystrix.class)

public interface CommodityClient {

/**

* 获取商品公对公支付配置

* @param param 查询参数

* @return 查询结果

*/

@RequestMapping("/noAuth/payConfig")

ResponseMessage getCommodityPayConfig(@RequestBody CommodityNoAuthParam param);

}

/**

* 自核信息操作feign接口

* @Author Ezio

* @Date 2020/5/19 15:42

*/

@Component

@FeignClient(name = "hauto-merchant", contextId = "ExamineClient", path = "examine", fallback = ExamineHystrix.class)

public interface ExamineClient {

/**

* 通过商品id获取自核账户

* @param commodityId

* @return

*/

@PostMapping("getAccountByCommodityId")

ResponseMessage getAccountByCommodityId(String commodityId);

}

需要用contextId来区分;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud 是一个基于 Spring Boot 的框架,用于快速构建分布式系统的通用工具集。Nacos 是阿里巴巴开源的注册心和配置心,而 Feign 是一个声明式的 HTTP 客户端,使得编写 Web 服务客户端变得更加简单。 下面是 Spring Cloud 项目搭建,并整合 Nacos 和 Feign 组件的步骤: 1. 创建父工程,添加以下依赖: ``` <dependencyManagement> <dependencies> <!-- Spring Cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.1</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 2. 创建子工程,添加以下依赖: ``` <!-- Nacos 注册心和配置心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> </dependency> <!-- Feign 声明式 HTTP 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.0.2</version> </dependency> ``` 3. 在子工程的启动类上添加注解 `@EnableDiscoveryClient` 和 `@EnableFeignClients`,分别启用服务注册与发现和 Feign 组件。 ``` @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 配置 Nacos 注册心和配置心的地址,可以在 `application.properties` 文件添加以下配置: ``` # Nacos 注册心和配置心地址 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.server-addr=127.0.0.1:8848 ``` 5. 创建 Feign 接口,使用 `@FeignClient` 注解指定要调用的服务名称和请求路径。 ``` @FeignClient(name = "service-provider") public interface HelloFeignClient { @GetMapping("/hello") String hello(); } ``` 6. 在需要使用 Feign 的地方注入 `HelloFeignClient` 对象,即可调用服务提供者的 `/hello` 接口。 ``` @RestController public class HelloController { @Autowired private HelloFeignClient helloFeignClient; @GetMapping("/hello") public String hello() { return helloFeignClient.hello(); } } ``` 至此,Spring Cloud 项目已经搭建完成,并整合了 Nacos 和 Feign 组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值