Nacos + OpenFeign 的使用方式

一.OpenFeign 介绍

OpenFeign 的全称是 Spring Cloud OpenFeign,它是 Spring 官方推出的一种声明式服务调用和负载均衡组件。它的出现就是为了替代已经进入停更维护状态的 Feign(Netflix Feign)的。也就是说 OpenFeign(Spring Cloud OpenFeign)是 Feign 的升级版.

二.OpenFeign 常用注解

OpenFeign 声明式服务调用和负载均衡组件,因此它的核心是使用注解 + 接口的方式实现服务调用,所以了解 OpenFeign 的注解就至关重要了。 对于 Feign 框架来说,它只支持 Feign 注解和 JAX-RS 注解,但 OpenFeign 在 Feign 的基础上还增加了对 Spring MVC 注解的支持,例如 @RequestMapping、@GetMapping 和 @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 是用在服务消费端的,有消费端就得有服务提供端,它们的关系如下图所示:
Nacos + OpenFeign 的使用方式_第1张图片
创建服务提供者
1.先创建一个 Spring Boot 项目(Spring Cloud 项目是基于 Spring Boot 创建的),添加 spring-web 和 nacos-discovery 依赖,具体依赖信息如下:

<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
  <groupId>com.alibaba.cloudgroupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>

2.设置 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 # 项目启动端口号

3.添加服务方法,如下代码所示:

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
@RestController
public 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.创建一个 Spring Boot 项目,添加 spring-web、nacos-discovery 和 openfeign 依赖,具体依赖内容如下:


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-openfeignartifactId>
dependency>

2.设置 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 # 项目启动端口号

3.在 Spring Boot 项目的启动文件上添加 @EnableFeignClients 注解,开启 OpenFeign,具体实现代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
 
@SpringBootApplication
@EnableFeignClients // 启用 OpenFeign
public class OpenfeignConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OpenfeignConsumerApplication.class, args);
    }
}

4.最重要的一步,创建 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 服务 id
public interface SpringCloudNacosProviderClient {
    @GetMapping("/call/{name}") // 使用 get 方式,调用服务提供者的 /call/{name} 接口
    public String call(@PathVariable(value = "name") String name);
}

5.编写服务调用者代码,经过了上一步对服务提供者的封装之后,在控制器中我们可以像调用本地方法一样调用远程接口了,具体实现代码如下:

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;
 
@RestController
public class ConsumerController {
    @Resource
    private SpringCloudNacosProviderClient providerClient; // 加载 openfeign client
    
    @GetMapping("/consumer")
    public String consumer(@RequestParam String name) {
        // 向调用本地方法一样,调用 openfeign client 中的方法
        return providerClient.call(name);
    }
}

然后分别启动服务提供者和服务调用者程序,执行结果如下图所示:
在这里插入图片描述

你可能感兴趣的:(spring,java,mvc)