这篇文章将为大家详细讲解有关怎么整合OpenFeign远程调用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站制作、做网站、河曲网络推广、小程序开发、河曲网络营销、河曲企业策划、河曲品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供河曲建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
示例:查询用户的学习时长
用户微服务passjava-member调用学习微服务passjava-study的方法
passjava-member和passjava-study项目的pom文件引入openfeign依赖
org.springframework.cloud
spring-cloud-starter-openfeign
返回某个用户学习题目的总时长
@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));
}
创建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分钟,昵称:悟空聊架构
示例:用户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));
}
@FeignClient
),声明这个接口类是用来远程调用其他服务的@EnableFeignClients
)关于怎么整合OpenFeign远程调用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。