Feign 的简单介绍
Feign 框架进行RPC调用,会按照http协议来封装请求数据,数据格式都是按照http协议里的请求来做的,底层通过tcp连接发送。其基本原理是服务B向注册中心注册自己,服务A通过Ribbon发现服务。然后Feign通过封装好的代码发送http请求给服务B,从而进行两服务之间的通讯。简而言之Feign是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求。
Nacos 整合 OpenFeign
- 前提条件准备好nacos 环境
1、创建一个maven父工程
<modules>
<module>common</module>
<module>provider</module>
<module>consumer</module>
</modules>
<packaging>pom</packaging>
<properties>
<jdk-version>11</jdk-version>
<spring-boot-version>2.6.3</spring-boot-version>
<spring-cloud-version>2021.0.1</spring-cloud-version>
<spring-cloud-alibaba-version>2021.0.1.0</spring-cloud-alibaba-version>
<lombok-version>1.16.10</lombok-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba-version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot-version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok-version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
2、创建三个子工程模块
2.1、创建provider 模块,自己暴露写出一个接口到时提供给consumer模块调用(和写普通接口一样)。
2.1.1、引入依赖
<dependencies>
<dependency>
<groupId>com.xia</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--Spring Cloud alibaba nacoos 服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监听信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.1.2、修改配置文件
server:
port: 9001
spring:
application:
name: Provider9001
cloud:
nacos:
discovery:
# nacos 服务地址
server-addr: 192.168.98.30:8848,192.168.98.31:8848,192.168.98.32:8848
# 所属组
group: myDemo
2.1.3、配置启动类
@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
public class Provider9001 {
public static void main(String[] args) {
SpringApplication.run(Provider9001.class,args);
log.info("-------------服务启动成功-------------");
}
}
3.1、创建consumer 模块
3.1.1、引入依赖
<dependencies>
<!--Open Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--LoadBalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--实现服务降级-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.xia</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--Spring Cloud alibaba nacoos 服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监听信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
3.1.2、修改配置文件
server:
port: 9002
spring:
application:
name: Provider9001
cloud:
nacos:
discovery:
# nacos 服务地址
server-addr: 192.168.98.30:8848,192.168.98.31:8848,192.168.98.32:8848
# 所属组
group: myDemo
feign:
# 开启服务降级
sentinel:
enabled: true
client:
config:
default:
# 网络连接超时时间
connectTimeout: 1000
# 远程调用响应超时时间
readTime: 2000
3.1.3、配置启动类
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class Consumer9002 {
public static void main(String[] args) {
SpringApplication.run(Consumer9002.class,args);
log.info("-------------服务启动成功-------------");
}
}
3.1.4、创建调用provider提供的接口
@FeignClient(value = "Provider9001",fallbackFactory = ApiFailBack.class)
public interface ProviderApi {
@RequestMapping(value = "/provider/demo", method = RequestMethod.POST)
public BusinessResult myDemo();
}
创建服务降级方法
@Component
@Slf4j
public class ApiFailBack implements FallbackFactory<ProviderApi> {
private static final Logger LOGGER = LoggerFactory.getLogger(ApiFailBack.class);
@Override
public ProviderApi create(Throwable cause) {
return () -> {
return BusinessResult.error("服务超时");
};
}
}
编写调用服务的controller
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
public ProviderApi providerApi;
@RequestMapping(value = "/demo", method = RequestMethod.POST)
public BusinessResult myDemo() {
return providerApi.myDemo();
}
}
4、运行结果
4.1 、 正常情况
4.2、 超时情况
5、代码地址