Springcloud项目整合openfeigin
前言
springcloud整合openfeigin实现第三方效验接口
提示:以下是本篇文章正文内容,下面案例可供参考
一、openfeign是什么?
OpenFeign 是一个基于 Java 的声明式 HTTP 客户端框架,用于简化调用 RESTful 服务的过程。它是 Netflix 开发的,旨在简化基于 HTTP 的 API 调用,特别是微服务架构中的服务间通信
二、使用步骤
代码如下(示例):
- 添加依赖:首先,在你的项目中添加 OpenFeign 的依赖。如果你是使用 Maven 来管理项目,可以在 pom.xml 文件中添加以下依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
- 启用 OpenFeign:在 Spring Boot 应用程序的主类上添加
@EnableFeignClients
注解,以启用 OpenFeign。//标记启动类 @SpringBootApplication (scanBasePackages = OpenPowerConstants.BASE_SCAN_PACKAGE) //启用服务发现客户端功能 @EnableDiscoveryClient //这个注解用于启用 Feign 客户端功能,参数指定了需要扫描的 Feign 客户端接口所在的包路径 @EnableFeignClients (basePackages = OpenPowerConstants.BASE_SCAN_PACKAGE) //扫描mapper @MapperScan (OpenPowerConstants.MAPPER_SCAN_PACKAGE) //这个注解用于启用 Spring 的缓存支持,可以通过在方法上标注 @Cacheable、@CachePut、@CacheEvict 等注解来实现缓存功能。 @EnableCaching //这个注解用于启用 Spring 的异步方法调用支持,可以通过在方法上标注 @Async 注解来实现方法的异步执行。 @EnableAsync public class OpenPowerApi { public static void main (String[] args) { SpringApplication.run (OpenPowerApi.class, args); } }
- 定义 Feign 接口:创建一个接口,用于声明要调用的远程服务的 API。你可以使用注解来定义接口,包括请求的 URL、请求方法、请求头等信息。
java @FeignClient (name = "RemoteCaptchaService", url = "${cph-baseline-config.user-service.host}", path = "${cph-baseline-config.user-service.path}", configuration = BaseLineAdminReqInterceptor.class, fallbackFactory = RemoteCaptchaServiceFallbackFactory.class) public interface RemoteCaptchaService { /** * 生成图片验证码 * 用于管理门户获取图片验证码 * * @param getCaptchaReq 请求对象 * @return 响应对象 */ @PostMapping ("/admin/v1/vcc/captcha") CaptchaResp getCaptcha (@RequestBody GetCaptchaReq getCaptchaReq); /** * 生成图片验证码图片 * 用于管理门户获取图片验证码图片 * * @param getCaptchaImgReq 请求对象 * @return 响应对象 */ @PostMapping ("/admin/v1/vcc/captcha/image") CaptchaResp getCaptchaImg (@RequestBody GetCaptchaImgReq getCaptchaImgReq); /** * 刷新图片验证码图片 * 此接口用于管理门户刷新图片验证码图片 * * @param refreshCaptchaImgReq 请求对象 * @return 响应对象 */ @PutMapping ("/admin/v1/vcc/captcha/image") CaptchaResp refreshCaptchaImg (@RequestBody RefreshCaptchaImgReq refreshCaptchaImgReq); }
在这个例子中,
RemoteServiceClient
是一个 Feign 接口,用于调用名为remote-service
的远程服务的/api/resource
路径 - 使用 Feign 接口:在你的代码中注入 Feign 接口,并调用其中定义的方法。
@RestController @RequestMapping ("/user") @Slf4j @RequiredArgsConstructor public class CaptchaController { private final PCaptchaService pCaptchaService; /** * 获取生成验证码的key * @return */ @PostMapping("/captcha/key") public ApiResult<PCaptchaKeyResp> key(){ R<PCaptchaKeyResp> resp=pCaptchaService.getkey(); return ApiResultUtil.build (resp.getResultCode (), resp.getResultMsg (), resp.getData ()); } /** * 根据Key来获取验证码图片 * @return */ @PostMapping ("/captcha/image") public ApiResult<PCaptchaImageResp> captchaImage (@RequestBody @Validated CaptchaImageReq captchaImageReq) { R<PCaptchaImageResp> resp = pCaptchaService.getCaptchaImg (captchaImageReq); return ApiResultUtil.build (resp.getResultCode (), resp.getResultMsg (), resp.getData ()); } /*** * 刷新验证码 * @param pRefreshCaptchaImgReq * @return */ @PostMapping ("/captcha/refresh") public ApiResult<PCaptchaImageResp> refreshCaptcha (@Validated @RequestBody PRefreshCaptchaImgReq pRefreshCaptchaImgReq) { R<PCaptchaImageResp> resp = pCaptchaService.refreshCaptchaImg (pRefreshCaptchaImgReq); return ApiResultUtil.build (resp.getResultCode (), resp.getResultMsg (), resp.getData ()); } }
这样,你就可以在 CaptchaController类中注入PCaptchaService ,然后调用它的方法来从远程服务获取资源。
- 配置属性(可选):你可以在
application.properties
或application.yml
文件中配置 Feign 相关的属性,例如超时时间、重试策略等。feign: client: config: default: connectTimeout: 5000 readTimeout: 5000
总结
以上就是使用 OpenFeign 实现远程服务调用的基本步骤。通过定义 Feign 接口,并使用注解来声明服务调用的细节,可以使得代码更加清晰和简洁。