简介
Feign
Feign是一个声明性web服务客户端。让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可。让http远程调用就像接口调用一样简单。(远程http调用的工具有很多,HttpClient、OKHttp、Spring Boot中的RestTemplate等 —— 我们在 Spring Cloud alibaba 使用Nacos服务发现 一文中就是使用的restTemplate的方式)
OpenFeign和Feign
Feign是由Netflix开源,但目前和ribbon一样停止更新了,所以spring 官方在feign的基础上推出了OpenFeign,目前spring Cloud基本都使用OpenFeign做http调用客户端。
入门示例
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>
<groupId>org.example</groupId>
<artifactId>CloudConsumer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<build.name>CloudConsumer</build.name>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.cloud.version>2021.0.4</spring.cloud.version>
<spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 使用Nacos服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 使用Nacos配置管理 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- 使用spring-cloud-starter-loadbalancer做负载均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 支持读取bootstrap配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<id>pre</id>
<properties>
<profileActive>pre</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>${build.name}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>mapper/**/*.xml</include>
<include>application.properties</include>
<include>application-${profileActive}.properties</include>
<include>bootstrap.properties</include>
<include>logback-spring.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
Application 启动类
package com.yyoo.cloud;
import com.yyoo.cloud.conf.MyNacosLoadBalancerConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication(scanBasePackages = {"com.yyoo"})
@EnableDiscoveryClient // 开启服务注册发现功能
@EnableFeignClients // 开启OpenFeign功能
// 更改默认的负载均衡策略
@LoadBalancerClients(defaultConfiguration = MyNacosLoadBalancerConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
MyNacosLoadBalancerConfiguration类的代码在我们的前文 Spring Cloud LoadBalancer(负载均衡) 中定义
OpenFeign客户端代码MyClient
package com.yyoo.cloud.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient("myCloud")
public interface MyClient {
@RequestMapping("/myCloud/conf/getName")
String getName();
@RequestMapping("/myCloud/conf/getCommonConf")
String getCommonConf();
}
该调用的是我们Spring Cloud alibaba 使用Nacos配置中心 一文中的Provider。所以本文中没有Provider的实现示例。
注:@FeignClient(“myCloud”)中的myCloud是我们Provider的ServiceID,@RequestMapping中的myCloud是Provider的应用上下文
注:@FeignClient注解的类上,不能使用@RequestMapping注解。(这是官网提示)
翻译过来
编写测试Controller
package com.yyoo.cloud.controller;
import com.yyoo.cloud.client.MyClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RequestMapping("openFeign")
@RestController
public class MyController1 {
@Resource
private MyClient myClient;
@RequestMapping("getName")
public String getName(){
String rs = myClient.getName();
System.out.println("OpenFeign:" + rs);
return rs;
}
@RequestMapping("getCommonConf")
public String getCommonConf(){
String rs = myClient.getCommonConf();
System.out.println("OpenFeign:" + rs);
return rs;
}
}
调用 http://127.0.0.1:8704/myCloud/openFeign/getCommonConf 接口,多测试几次
- 先判断OpenFeign调用是否成功,是否返回对应结果
- 启动多个Provider(相同ip可以使用不同端口启动),验证我们的负载均衡配置是否成功。
到此我们的OpenFeign入门就完成了,但其涉及到的问题还有很多,比如:失败重试、负载均衡的健康检查、请求的熔断降级等等一系列的问题,我们都将一一说明。