扩大
缩小

18.SpringCloud实战项目-SpringCloud整合OpenFeign远程调用

SpringCloud实战项目全套学习教程连载中

PassJava 学习教程

简介

  • PassJava-Learning项目是PassJava(佳必过)项目的学习教程。对架构、业务、技术要点进行讲解。
  • PassJava 是一款Java面试刷题的开源系统,可以用零碎时间利用小程序查看常见面试题,夯实Java基础。
  • PassJava 项目可以教会你如何搭建SpringBoot项目,Spring Cloud项目
  • 采用流行的技术,如 SpringBoot、MyBatis、Redis、 MySql、 MongoDB、 RabbitMQ、Elasticsearch,采用Docker容器化部署。

更好的阅读体验

文档连载目录

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")
    public interface 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
    public class PassjavaMemberApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(PassjavaMemberApplication.class, args);
        }
    
    }
    
  • 测试接口

    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")
public interface 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));
}

请求地址和参数:http://localhost:10000/member/member/studytime/list/test/1

执行结果:

执行结果

3.总结FeignClient使用方法

  • 引入OpenFeign依赖
  • 定义FeignClient接口类(注解@FeignClient),声明这个接口类是用来远程调用其他服务的
  • 接口类中定义要远程调用的接口方法,指定远程服务方法的路径
  • Controller类中调用接口方法
  • 开启远程调用(注解@EnableFeignClients

代码地址

https://github.com/Jackson0714/PassJava-Platform

公众号

公众号

posted @ 2020-04-22 16:14  悟空聊架构  阅读(501)  评论(0编辑  收藏  举报
Copyright ©2019 悟空聊架构