OpenFeign服务接口调用
一、概述
1、OpenFeign是什么
官网
Feign是一个声明式WebSerrvice客户端。使用Feign能让编写Web Serrvice客户端更加简单。
它的使用方法是定义一个服务接口然后再上面添加注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
总结:Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需创建接口并在接口上添加注解即可
GitHub官网地址
2、OpenFeign能干什么
Feign旨在使编写Java Http客户端变得更加容易
3、Feign和OpenFeign两者的区别
Feign | OpenFeign |
---|---|
Feign是Spring Cloud 组件中的一个轻量级RESTful的HTTP服务客户端Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务 | OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC 的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。 |
二、OpenFeign使·用步骤
1、接口+注解
微服务调用接口+@FeignClient
2、新建cloud-consumer-feign-order80
3、pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
4、yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002:7002/eureka/
5、主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springfra