当前位置: 首页 > news >正文

Feign的调用demo 和 EnableFeignClients的包名

在你的场景下,如果刷题微服务通过 Maven 引入了 auth-api 模块,并且 auth-api 中定义了 Feign 接口(例如获取用户名的方法),你需要在 刷题微服务 中的启动类上配置 @EnableFeignClients 注解。配置中 basePackages 参数应该填写 Feign 接口所在包的 全限定包名(即 auth-api 模块中定义接口的包路径)。


项目结构如下

 

Feign 接口定义(在 auth-api 模块中)

  • name: 必须和权限微服务 auth-servicespring.application.name 保持一致。
  • path: 对应权限服务中的接口路径。

刷题微服务中的配置(QuestionApplication

在刷题微服务中引入 auth-api 模块,并在启动类上配置 @EnableFeignClients,指向 Feign 接口所在的包 com.provider.auth.api

Maven 引入 auth-api

question-servicepom.xml 中添加依赖:

<dependency>
    <groupId>com.provider</groupId>
    <artifactId>auth-api</artifactId>
    <version>1.0.0</version>
</dependency>

启动类配置 @EnableFeignClients

启动类的完整配置:

package com.provider.question;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@EnableFeignClients(basePackages = "com.provider.auth.api") // 指向 auth-api 中的接口包
@SpringBootApplication
public class QuestionApplication {public static void main(String[] args) {SpringApplication.run(QuestionApplication.class, args);}
}


权限服务的配置(auth-service

在权限微服务中,确保接口路径 /auth/getUsername 存在并正确实现:

package com.provider.auth.service;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/auth")
public class UserService {@GetMapping("/getUsername")public String getUsername(@RequestParam("userId") Long userId) {// 返回用户名逻辑return "User-" + userId;}
}
  • 确保 auth-servicespring.application.name 配置为 auth-service

spring:
  application:
    name: auth-service
server:
  port: 8081


调用接口示例

在刷题微服务中,注入 UserFeignClient 并调用权限微服务的接口:

package com.provider.question.service;import com.provider.auth.api.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class QuestionService {@Autowiredprivate UserFeignClient userFeignClient;public String getUsername(Long userId) {return userFeignClient.getUsername(userId);}
}

关键点总结

  1. @EnableFeignClients 的配置

    • 配置 basePackages 为 Feign 接口所在的包路径,例如:com.provider.auth.api
    • 这个包路径是 auth-api 模块中定义的 Feign 接口所在的包名。
  2. Maven 依赖

    • 确保刷题微服务正确引入了 auth-api 模块的依赖。
  3. 服务名匹配

    • 确保 @FeignClient(name = "auth-service") 中的 name 与权限微服务的 spring.application.name 一致。
  4. 注册中心

    • 如果使用注册中心(如 Eureka、Nacos),确保权限服务正常注册,并能被刷题微服务发现。
http://www.lryc.cn/news/509359.html

相关文章:

  • 简化开发流程:如何通过 JDBC 自动生成符合 Java 命名规范的实体类
  • W25Q128存储器详解
  • Vite系列课程 | 11. Vite 配置文件中 CSS 配置(Modules 模块化篇)
  • Everspin代理MR25H10CDFR存储MRAM
  • cesium小知识:使用 EntityCollection的方法
  • Java 日志类库
  • 【Unity3D】Particle粒子特效或3D物体显示在UGUI上的方案
  • 有没有检测吸烟的软件 ai视频检测分析厂区抽烟报警#Python
  • 《鸣潮》游戏运行时弹出“xinput1_3.dll文件缺失”错误的处理方法,“xinput1_3.dll文件缺失”详解!
  • 大模型应用—HivisionIDPhotos 证件照在线制作!支持离线、换装、美颜等
  • 解决Ubuntu下无法装载 Windows D盘的问题
  • 一体成型电感
  • Reed-Muller(RM)码之编码
  • 【蓝桥杯——物联网设计与开发】基础模块8 - RTC
  • 聚类算法DBSCAN 改进总结
  • uniapp开发微信小程序实现获取“我的位置”
  • java中两个系统进行非对称加密,两个系统的公私钥可以用一套吗?
  • 无人设备遥控器之定向天线篇
  • 【电路笔记 信号】Metastability 平均故障间隔时间(MTBF)公式推导:进入亚稳态+退出亚稳态+同步器的可靠性计算
  • 计算机视觉:原理、分类与应用
  • Vue.js组件开发-使用watch进行深度观察
  • 明厨亮灶系统
  • 虚幻引擎结构之AActor
  • 基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
  • JAVA HTTP压缩数据
  • VSCode 配置远程连接免密登录 插件
  • VIVO C++开发面试题及参考答案
  • Unity3D用正则判断身份证号或邮箱
  • 【终端工具】FinalShell v4.5.12 官方版
  • 【阅读记录-章节6】Build a Large Language Model (From Scratch)