Nacos:
Nacos 支持两种 HTTP 服务请求,一个是 REST Template,另一个是 Feign Client。
Rest Template 软件开发定制的调用方式,软件开发定制主要是通过 Ribbon(负载均衡) + RestTemplate 实现 HTTP 软件开发定制服务调用的
@RestControllerpublic class ConsumerController { @Resource private RestTemplate restTemplate; @GetMapping("/consumer") public String consumer(@RequestParam String name) { // 软件开发定制请求并获取结果(springcloud-nacos-provider 是 nacos 中的服务id) String result = restTemplate.getForObject("http://springcloud-nacos-provider/call/" + name, String.class); return result; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
软件开发定制从上述的实现代码我们软件开发定制可以看出一个问题,软件开发定制虽然以上代码可以实现 HTTP 服务调用,软件开发定制但需要开发者手动拼接软件开发定制调用地址和参数,软件开发定制并且远程服务调用和客软件开发定制户端自身的业务逻辑实现是混合在一起,不利于后期的维护与扩展
:
OpenFeign 的全称是 Spring Cloud OpenFeign,它是 Spring官方推出的一种声明式服务调用和负载均衡组件。它的出现就是为了替代已经进入停更维护状态的 Feign( Feign)的。也就是说OpenFeign(Spring Cloud OpenFeign)是 Feign 的升级版,它们的关系如下图所示:
因为 Feign 停更维护了,所以 Spring 官方需要推出了一个新的新的框架来对 Feign 功能进行升级和扩展。
OpenFeign常用注解:
OpenFeign 声明式服务调用和负载均衡组件,因此它的核心是使用注解 + 接口的方式实现服务调用,所以了解 OpenFeign 的注解就至关重要了。对于 Feign 框架来说,它只支持 Feign 注解和 JAX-RS 注解,但 OpenFeign 在 Feign 的基础上还增加了对 Spring MVC 注解的支持,例如 @RequestMapping、@ 和@PostMapping 等注解。
OpenFeign 常用注解有以下几个:
- @EnableFeignClients:该注解用于开启 OpenFeign 功能,当 Spring Cloud 应用启动时,OpenFeign 会扫描标有 @FeignClient 注解的接口,生成代理并注册到 Spring 容器中。
- FeignClient:该注解用于通知 OpenFeign 组件对 @RequestMapping 注解下的接口进行解析,并通过动态代理的方式产生实现类,实现负载均衡和服务调用。
- RequestMapping:向服务提供者发起 Request 请求(默认为 GET 方式请求),这里需要注意 @RequestMapping/@GetMapping/@PostMapping 和 Spring MVC 中的同名注解的含义是完全不同的。
- @GetMapping:向服务提供者发起 GET 请求。
- @PostMapping:向服务提供者发起 POST 请求。
OpenFeign使用:
OpenFeign 是用在服务消费端的,有消费端就得有服务提供端,它们的关系如下图所示:
所以我们先要创建一个服务提供者 Provider,创建步骤如下。
一、创建服务提供者
- 第一步:先创建一个 Spring Boot 项目(Spring Cloud 项目是基于 Spring Boot 创建的),添加 spring-web 和 nacos-discovery 依赖,具体依赖信息如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><!-- 添加 Nacos 支持 --><dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 第二步:设置 Nacos 相关配置,在 application.yml 中添加以下配置:
spring: application: name: springcloud-nacos-provider # 项目名称(nacos 注册的服务名) cloud: nacos: discovery: username: nacos # nacos 登录用户名 password: nacos666 # nacos 密码 server-addr: 127.0.0.1:8848 # nacos 服务端地址server: port: 8081 # 项目启动端口号
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 第三步:添加服务方法,如下代码所示:
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class HttpProviderApplication { public static void main(String[] args) { SpringApplication.run(HttpProviderApplication.class, args); } /** * 为客户端提供可调用的接口 */ @RequestMapping("/call/{name}") public String call(@PathVariable String name) { return LocalTime.now() + "——服务提供者1:" + name; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
二、创建服务消费者
- 第一步:创建一个 Spring Boot 项目,添加 spring-web、nacos-discovery 和 openfeign 依赖,具体依赖内容如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><!-- 添加 nacos 框架依赖 --><dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- 添加 openfeign 框架依赖 --><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 第二步:设置 Nacos 相关配置,在 application.yml 中添加以下配置:
spring: application: name: springcloud-nacos-consumer # 项目名称(nacos 注册的服务名) cloud: nacos: discovery: username: nacos # nacos 登录用户名 password: nacos666 # nacos 密码 server-addr: 127.0.0.1:8848 # nacos 服务端地址server: port: 8093 # 项目启动端口号
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 第三步:在 Spring Boot 项目的启动文件上添加 @EnableFeignClients 注解,开启 OpenFeign,具体实现代码如下:
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableFeignClients // 启用 OpenFeignpublic class OpenfeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(OpenfeignConsumerApplication.class, args); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 第四步:最重要的一步,创建 OpenFeign 与服务提供者的调用接口,实现代码如下:
import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;@FeignClient("springcloud-nacos-provider") // nacos 服务 idpublic interface SpringCloudNacosProviderClient { @GetMapping("/call/{name}") // 使用 get 方式,调用服务提供者的 /call/{name} 接口 public String call(@PathVariable(value = "name") String name);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 第五步:编写服务调用者代码,经过了上一步对服务提供者的封装之后,在控制器中我们可以像调用本地方法一样调用远程接口了,具体实现代码如下:
import com.example.openfeignconsumer.feignclient.SpringCloudNacosProviderClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestControllerpublic class ConsumerController { @Resource private SpringCloudNacosProviderClient providerClient; // 加载 openfeign client @GetMapping("/consumer") public String consumer(@RequestParam String name) { // 向调用本地方法一样,调用 openfeign client 中的方法 return providerClient.call(name); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
然后分别启动服务提供者和服务调用者程序,执行结果如下图所示:
注意事项:
OpenFeign 默认的接口超时时间为 1s,所以如果接口的执行时间超过 1s,那么程序调用就会报错。接下来,我们编写程序测试一下,将服务提供者的代码休眠 2s,具体实现代码如下:
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.time.LocalTime;import java.util.concurrent.TimeUnit;@SpringBootApplication@RestControllerpublic class HttpProviderApplication { public static void main(String[] args) { SpringApplication.run(HttpProviderApplication.class, args); } /** * 为客户端提供可调用的接口 */ @RequestMapping("/call/{name}") public String call(@PathVariable String name) throws InterruptedException { // 让程序休眠 2s TimeUnit.SECONDS.sleep(2); return LocalTime.now() + "——服务提供者1:" + name; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
之后使用 OpenFeign 客户端访问服务,就会出现如下报错信息:
解决方案:通过修改配置文件中的超时时长,也就是手动调节接口的超时时长来解决此问题,因为 1s 确
实太短了,修改的配置信息如下:ribbon: ReadTimeout: 5000 # 请求连接的超时时间 ConnectionTimeout: 10000 # 请求处理的超时时间
- 1
- 2
- 3
- 4
总结
OpenFeign 是基于 Feign 实现的,是 Spring Cloud 官方提供的注解式调用 REST 接口框架,OpenFeign/Feign 底层是基于 Ribbon 实现负载均衡的。使用 OpenFeign 有三个关键步骤,首先在 Spring Boot 启动类上使用注解 @EnableFeignClients 开启 OpenFeign;第二,使用 @FeignClient + @GetMapping/@PostMapping 调用服务提供者的接口;第三,在客户端中注入 Feign Client 对象,像调用本地方法一样调用远程接口。