SpringBoot集成OpenFeign

SpringBoot集成OpenFeign简单使用


一. 简介

1. 什么是OpenFeign?

Feign是一个声明性web服务客户端。它使编写web服务客户端更容易。要使用Feign,请创建一个接口并对其进行注释。它具有可插入的注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并支持使用Spring Web中默认使用的相同HttpMessageConverters。SpringCloud集成了Ribbon和Eureka,以及SpringCloundLoadBalancer,以在使用Feign时提供负载平衡的http客户端。

2. OpenFeign能干什么?

在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可);即可完成对服务提供方的接口绑定,简化了使用SpringCloud Ribbon时,自动封装服务调用客户端的开发量。

3. OpenFeign和Feign的区别
OpenFeignFeign
OpenFeign是SpringCloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。Feign是SpringCloud组件中的一个轻量级RESTful的Http服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。

二. Openfeign的使用

本次只演示OpenFeign远程调用接口的基本使用,不涉及负载均衡和服务熔断等其他问题。

1. 引入依赖

springboor和springcloud版本号均为 <version>2.2.1.RELEASE</version>

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
      <version>2.2.1.RELEASE</version>
</dependency>
2. 添加启动注解

在项目的启动类上添加Open Feign的启动注解@EnableFeignClients

@SpringBootApplication
@EnableFeignClients
public class StudentServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudentServerApplication.class, args);
    }

}

注:基础的使用我们只需要引入依赖和添加注解即可,不需要在yml中做其他配置,后期可根据需要引入其他依赖或添加其他注解即可(如Ribbon和Hystrix)。

三. Openfeign的调用

为了演示Openfeign,我在本地开启了两台工程,TeacherServerApplication作为服务提供帮者,StudentServerApplication作为服务调用者,模拟场景为老师发布通知,学生接收通知。

1. 服务提供者

TeacherServerApplication作为服务提供者,地址为localhost:8085,服务名为teacher-server

1.1 服务启动类
@SpringBootApplication
@EnableFeignClients
public class TeacherServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TeacherServerApplication.class, args);
    }
}

1.2 创建提供者Service
@Service
public class TeacherService {
    public String getNotice(){
        return "疫情原因,明天不来学校,上午9:00 在家上网课";
    }
}
1.3 创建提供者Controler
@RestController
@RequestMapping(path = "/api/v1/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/getNotice")
    public String getNotice(){
        String notice = teacherService.getNotice();
        System.out.println("获取通知"+notice);
        return teacherService.getNotice();
    }
}

2. 服务调用者

StudentServerApplication 作为服务调用者,地址为localhost:8080

2.1 服务启动类
@SpringBootApplication
@EnableFeignClients
public class StudentServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudentServerApplication.class, args);
    }
}
2.2 服务调用者Service

这里的name和url由服务提供者提供,方法名可以自定义,但 @GetMapping("/getNotice")必须一致。

@FeignClient(name = "teacher-server", url = "localhost:8085/api/v1/teacher")
@Component
public interface StudentService {
    @GetMapping("/getNotice")
    public String getNotice();
}
2.3 服务调用者Controller
/**
 * @author gf
 * @date 2022/11/3
 */
@RestController
@RequestMapping(path = "/api/v1/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/notice")
    public String getTeacherNotice(){
        String notice = studentService.getNotice();
        return "学生收到通知了,通知内容为:"+notice;
    }
}

2.4 结果预览

启动两个工程,浏览器输入服务调用者的请求url。

在这里插入图片描述
从上图可以看,我们已经获取到了服务提供者返回的数据。

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值