缅甸做菠菜网站,做游戏直播那个网站好,互动式网站开发,海兴县建设工程招标信息网站在你的场景下#xff0c;如果刷题微服务通过 Maven 引入了 auth-api 模块#xff0c;并且 auth-api 中定义了 Feign 接口#xff08;例如获取用户名的方法#xff09;#xff0c;你需要在 刷题微服务 中的启动类上配置 EnableFeignClients 注解。配置中 basePackages 参数…在你的场景下如果刷题微服务通过 Maven 引入了 auth-api 模块并且 auth-api 中定义了 Feign 接口例如获取用户名的方法你需要在 刷题微服务 中的启动类上配置 EnableFeignClients 注解。配置中 basePackages 参数应该填写 Feign 接口所在包的 全限定包名即 auth-api 模块中定义接口的包路径。 项目结构如下 Feign 接口定义在 auth-api 模块中 name: 必须和权限微服务 auth-service 的 spring.application.name 保持一致。path: 对应权限服务中的接口路径。 刷题微服务中的配置QuestionApplication
在刷题微服务中引入 auth-api 模块并在启动类上配置 EnableFeignClients指向 Feign 接口所在的包 com.provider.auth.api
Maven 引入 auth-api
在 question-service 的 pom.xml 中添加依赖 dependency groupIdcom.provider/groupId artifactIdauth-api/artifactId version1.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-service 的 spring.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);}
}关键点总结 EnableFeignClients 的配置 配置 basePackages 为 Feign 接口所在的包路径例如com.provider.auth.api。这个包路径是 auth-api 模块中定义的 Feign 接口所在的包名。 Maven 依赖 确保刷题微服务正确引入了 auth-api 模块的依赖。 服务名匹配 确保 FeignClient(name auth-service) 中的 name 与权限微服务的 spring.application.name 一致。 注册中心 如果使用注册中心如 Eureka、Nacos确保权限服务正常注册并能被刷题微服务发现。