一、引言
什么是Feign
Feign远程调用,核心就是通过一系列的封装和处理,以JAVA注解的方式定义的远程调用API接口,最终转换成HTTP的请求形式。然后将HTTP的请求的响应结果,解析成JAVA Bean,返回给调用方,实现远程接口调用。
Feign个人理解实际上是一种对RPC远程过程调用的一种实现,通过注解可以很方便的去进行RPC调用,减少配置。
二、Feign调用实例
我们需要创建2个工程:order-server、gds-server,然后分别在其中定义出controller来模拟订单系统调用商品系统的业务流程。
gds-server
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.zhbf</groupId> 6 <artifactId>gds-server</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>gds-server</name> 9 <description>商品服务【有梦想的肥宅】</description> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>1.5.6.RELEASE</version> 15 <relativePath/> 16 </parent> 17 18 <properties> 19 <java.version>1.8</java.version> 20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 21 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 22 <spring-boot.version>1.5.6.RELEASE</spring-boot.version><!--Spring Boot版本--> 23 <spring-cloud.version>1.4.6.RELEASE</spring-cloud.version><!--Spring Cloud版本--> 24 </properties> 25 26 <dependencyManagement> 27 <dependencies> 28 <!--指定下载源和使用Spring Boot的版本--> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-dependencies</artifactId> 32 <version>${spring-boot.version}</version> 33 <type>pom</type> 34 <scope>import</scope> 35 </dependency> 36 <!--指定下载源和使用Spring Cloud的版本--> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-dependencies</artifactId> 40 <version>Edgware.SR5</version> 41 <type>pom</type> 42 <scope>import</scope> 43 </dependency> 44 </dependencies> 45 </dependencyManagement> 46 47 48 <dependencies> 49 <!--引入Spring Boot依赖--> 50 <dependency> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-starter</artifactId> 53 </dependency> 54 <!--引入lombok--> 55 <dependency> 56 <groupId>org.projectlombok</groupId> 57 <artifactId>lombok</artifactId> 58 <optional>true</optional> 59 </dependency> 60 <!--引入Spring Cloud的Eureka依赖--> 61 <dependency> 62 <groupId>org.springframework.cloud</groupId> 63 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 64 <version>${spring-cloud.version}</version> 65 </dependency> 66 <!--引入fastjson依赖--> 67 <dependency> 68 <groupId>com.alibaba</groupId> 69 <artifactId>fastjson</artifactId> 70 <version>1.2.76</version> 71 </dependency> 72 <!--服务调用--> 73 <dependency> 74 <groupId>org.springframework.cloud</groupId> 75 <artifactId>spring-cloud-starter-openfeign</artifactId> 76 </dependency> 77 </dependencies> 78 79 </project>
controller
/** * @author 有梦想的肥宅 * @date 2021/9/6 */ @RestController public class GdsController { @RequestMapping("/getGdsInfo") public Map getGdsInfo(@RequestParam String gdsId) { Map gdsInfo = new HashMap(); gdsInfo.put("gdsId", gdsId); gdsInfo.put("gdsName", "测试商品名称"); gdsInfo.put("shopName", "【有梦想的肥宅】小店"); return gdsInfo; } }
application.yml
#应用名称
spring:
application:
name: GDS-SERVER
#服务端口
server:
port: 9092
#Eureka配置
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:9090/eureka
instance:
hostname: 127.0.0.1
GdsServerApplication.java【启动类】
@SpringBootApplication @EnableDiscoveryClient //服务注册发现 @EnableFeignClients //服务调用 public class GdsServerApplication { public static void main(String[] args) { SpringApplication.run(GdsServerApplication.class, args); } }
order-server
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.zhbf</groupId> 6 <artifactId>order-server</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>order-server</name> 9 <description>订单服务【有梦想的肥宅】</description> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>1.5.6.RELEASE</version> 15 <relativePath/> 16 </parent> 17 18 <properties> 19 <java.version>1.8</java.version> 20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 21 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 22 <spring-boot.version>1.5.6.RELEASE</spring-boot.version><!--Spring Boot版本--> 23 <spring-cloud.version>1.4.6.RELEASE</spring-cloud.version><!--Spring Cloud版本--> 24 </properties> 25 26 <dependencyManagement> 27 <dependencies> 28 <!--指定下载源和使用Spring Boot的版本--> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-dependencies</artifactId> 32 <version>${spring-boot.version}</version> 33 <type>pom</type> 34 <scope>import</scope> 35 </dependency> 36 <!--指定下载源和使用Spring Cloud的版本--> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-dependencies</artifactId> 40 <version>Edgware.SR5</version> 41 <type>pom</type> 42 <scope>import</scope> 43 </dependency> 44 </dependencies> 45 </dependencyManagement> 46 47 48 <dependencies> 49 <!--引入Spring Boot依赖--> 50 <dependency> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-starter</artifactId> 53 </dependency> 54 <!--引入lombok--> 55 <dependency> 56 <groupId>org.projectlombok</groupId> 57 <artifactId>lombok</artifactId> 58 <optional>true</optional> 59 </dependency> 60 <!--引入Spring Cloud的Eureka依赖--> 61 <dependency> 62 <groupId>org.springframework.cloud</groupId> 63 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 64 <version>${spring-cloud.version}</version> 65 </dependency> 66 <!--引入fastjson依赖--> 67 <dependency> 68 <groupId>com.alibaba</groupId> 69 <artifactId>fastjson</artifactId> 70 <version>1.2.76</version> 71 </dependency> 72 <!--服务调用--> 73 <dependency> 74 <groupId>org.springframework.cloud</groupId> 75 <artifactId>spring-cloud-starter-openfeign</artifactId> 76 </dependency> 77 </dependencies> 78 </project>
调用商品服务的fegin接口
/** * 调用商品服务的fegin接口 * * @author 有梦想的肥宅 */ @FeignClient(value = "GDS-SERVER", path = "/")//这里表示使用feign去调用应用名为GDS-SERVER的内部服务 public interface OutGdsApi { /** * 查询商品信息 */ @RequestMapping("/getGdsInfo")//表示通过feign发送http请求到GDS-SERVER的应用,地址映射为"/getGdsInfo" Map getGdsInfo(@RequestParam("gdsId") String gdsId); }
controller
/** * @author 有梦想的肥宅 * @date 2021/9/6 */ @RestController public class OrderController { @Autowired private OutGdsApi outGdsApi;//自动注入调用商品服务的fegin接口 @RequestMapping("/showOrderInfo") public String showOrderInfo() { Map orderInfo = new HashMap(); orderInfo.put("orderId", 1); orderInfo.put("gdsInfo", outGdsApi.getGdsInfo("1")); return JSON.toJSONString(orderInfo); } }
application.yml
#应用名称 spring: application: name: OREDER-SERVER #服务端口 server: port: 9091 #Eureka配置 eureka: client: service-url: defaultZone: http://${eureka.instance.hostname}:9090/eureka instance: hostname: 127.0.0.1
OrderServerApplication.java【启动类】
@SpringBootApplication @EnableDiscoveryClient //服务注册发现 @EnableFeignClients //服务调用 public class OrderServerApplication { public static void main(String[] args) { SpringApplication.run(OrderServerApplication.class, args); } }
分别启动gds-server、order-server工程,查看效果