18.SpringCloud实战项目-整合OpenFeign远程调用
SpringCloud实战项目 PassJava 全套学习教程连载中,
关注公众号
第一时间获取。
文档在线地址:www.jayh.club
连载中...
1.Feign 概述
Feign声明式客的HTTP客户端,让远程调用更简单。 提供了HTTP请求的模板,编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息 整合了Ribbon(负载均衡组件)和Hystix(服务熔断组件),不需要显示使用这两个组件 Spring Cloud Feign 在Netflix Feign的基础上扩展了对SpringMVC注解的支持
2. 远程调用示例
示例:查询用户的学习时长
用户微服务passjava-member调用学习微服务passjava-study的方法
1.引入openfeign依赖
passjava-member和passjava-study项目的pom文件引入openfeign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.StudyTimeController定义远程调用测试方法
返回某个用户学习题目的总时长
@RequestMapping("/member/list/test")
public R memberStudyTimeTest() {
StudyTimeEntity studyTimeEntity = new StudyTimeEntity();
studyTimeEntity.setTotalTime(100); // 学习时长:100分钟
studyTimeEntity.setQuesTypeId(1L); // 题目类型:1 (javaBasic)
return R.ok().put("studyTime", Arrays.asList(studyTimeEntity));
}
3.member目录下创建feign service
创建package: com.jackson0714.passjava.member.feign
创建StudyTimeFeignService接口
添加注解
@FeignClient
。显示声明这个接口用来远程调用study
服务。@FeignClient("passjava-study")
publicinterface StudyTimeFeignService {}添加远程调用方法
public R memberStudyTime();
给方法添加要远程调用的方法的路径
study/studytime/member/list/test
@RequestMapping("study/studytime/member/list/test")
public R getMemberStudyTimeListTest();添加注解
@EnableFeignClients
开启远程调用服务。给类PassjavaStudyApplication.java添加注解
@EnableFeignClients
。basePackages代表自动扫码指定路径下所有带有@FeignClient注解的接口。
@EnableFeignClients(basePackages = "com.jackson0714.passjava.member.feign")
@EnableDiscoveryClient
@MapperScan("com.jackson0714.passjava.member.dao")
@SpringBootApplication
publicclass PassjavaMemberApplication {
public static void main(String[] args) {
SpringApplication.run(PassjavaMemberApplication.class, args);
}
}测试接口
启动passjava-member和passjava-study服务
用postman工具或浏览器输入请求地址
http://localhost:10000/member/member/studytime/list/test
返回结果如下图
studytime和member都有数据。
学习时长:100分钟,昵称:悟空聊架构
4.测试OpenFeign传参
示例:用户id作为参数在服务间传递
MemberController
@RequestMapping("/studytime/list/test/{id}")
public R getMemberStudyTimeListTest(@PathVariable("id") Long id) {
//mock数据库查到的会员信息
MemberEntity memberEntity = new MemberEntity();
memberEntity.setId(id); // 学习时长:100分钟
memberEntity.setNickname("悟空聊架构");
//远程调用拿到该用户的学习时长(学习时长是mock数据)
R memberStudyTimeList = studyTimeFeignService.getMemberStudyTimeListTest(id);
return R.ok().put("member", memberEntity).put("studytime", memberStudyTimeList.get("studytime"));
}
StudyTimeFeignService
@FeignClient("passjava-study")
publicinterface StudyTimeFeignService {
@RequestMapping("study/studytime/member/list/test/{id}")
public R getMemberStudyTimeListTest(@PathVariable("id") Long id);
}
StudyTimeController
@RequestMapping("/member/list/test/{id}")
public R memberStudyTimeTest(@PathVariable("id") Long id) {
StudyTimeEntity studyTimeEntity = new StudyTimeEntity();
studyTimeEntity.setTotalTime(100); // 学习时长:100分钟
studyTimeEntity.setQuesTypeId(1L); // 题目类型:1 (javaBasic)
return R.ok().put("studytime", Arrays.asList(studyTimeEntity));
}
3.总结FeignClient使用方法
引入OpenFeign依赖 定义FeignClient接口类(注解 @FeignClient
),声明这个接口类是用来远程调用其他服务的接口类中定义要远程调用的接口方法,指定远程服务方法的路径 Controller类中调用接口方法 开启远程调用(注解 @EnableFeignClients
)
更多内容
长按二维码关注
领取架构师资料
点击“阅读原文”,查看在线文档。
帅的人都点了在看!