目录
温馨提示:全套教程请查看 教程总览
一、简介
Feign 是一个声明式的 Web Service 客户端。它使编写 Web Service 客户端更容易。要使用 Feign,需要创建一个接口并对其进行注解。它有可插拔的注解支持,包括 Feign 注解和 JAX-RS 注解。Feign 还支持可插拔的编码器和解码器。Spring Cloud 增加了对 Spring MVC 注解的支持,并支持使用 Spring Web 中默认使用的 HttpMessageConverters
。Spring Cloud 集成了 Eureka、Spring Cloud CircuitBreaker以及Spring Cloud LoadBalancer,以便在使用Feign时提供一个负载均衡的http客户端。
1.如何引入Feign
如果要在我们的项目中使用 Feign,请使用 group ID 为 org.springframework.cloud
和 artifact ID 为 spring-cloud-starter-openfeign
的 starter。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
二、搭建Consumer-feign应用
1.创建项目
1.1 新建Module
1.2 引入依赖
pom.xml
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.ash</groupId>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<version>${revision}</version>
</parent>
<artifactId>cloud-consumer-feign</artifactId>
<description>服务消费者-feign</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Nacos注册中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- loadbalancer负载均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!-- 将源码中的xml文件打包到jar中 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
2.添加配置文件
resources目录下新建 application.yml
配置文件,内容如下:
server:
port: 9003
spring:
application:
# 服务名称
name: cloud-consumer-feign
cloud:
nacos:
# nacos注册中心
discovery:
# 服务ip:port
server-addr: 127.0.0.1:8848
management:
endpoints:
web:
exposure:
include: '*'
3.添加启动类
通过在启动类上加注解 @EnableFeignClients
开启Feign功能
新建启动类 ConsumerFeignApplication.java
内容如下:
package org.ash.consumer.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignApplication.class, args);
}
}
4.添加Feign接口
新建接口 FeignService.java
内容如下:
package org.ash.consumer.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "cloud-provider")
public interface FeignService {
@GetMapping("/provider/test/{message}")
public String getProviderTest(@PathVariable("message") String message);
}
在
@FeignClient
注解中,value
值(上面的 “cloud-provider”)是一个客户端服务名称,它被用来创建一个 Spring Cloud LoadBalancer client。你也可以使用url
属性指定一个URL
(绝对值或只是一个hostname)。
5.添加API
新建 ConsumerFeignController.java
提供一个API,内容如下:
package org.ash.consumer.feign.controller;
import jakarta.annotation.Resource;
import org.ash.consumer.feign.service.FeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerFeignController {
@Resource
private FeignService feignService;
@GetMapping("consumer/feign/test/{message}")
public String test(@PathVariable String message) {
return feignService.getProviderTest(message);
}
}
三、运行测试
1.启动项目
1.1 启动服务提供者
1.2 启动服务消费者
注意:如果启动控制台报错,请检查 Nacos server 是否已启动
2.查看Nacos控制台
打开浏览器访问:http://localhost:8848/nacos
,并打开控制台 “服务管理” 下的 “服务列表” 页面,看到 cloud-provider
服务和 cloud-consumer-feign
已经注册进Nacos,表示服务注册成功。
3.调用测试接口
打开浏览器调用我们之前写的测试接口 http://localhost:9003/consumer/feign/test/open-feign
,成功返回消息,表示调用成功。
至此,OpenFeign服务接口调用搭建完成!!!
温馨提示:全套教程请查看 教程总览