Springcloud 启用 OpenFeign

96 阅读1分钟

前言:由于Feign停止维护,后来有人开发openfeign,继续维护openfeign。
未特殊说明,在本文中Feign 和 OpenFeign是同一含义,Feign 代指 OpenFeign。
OpenFeign 有两个作用:

  1. 自带 Ribbon 负载均衡
  2. 提供优雅的远程服务调用方式,来替换 restTemplate

1. 在父级项目 pom.xml添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>

2. 在当前模块 pom.xml添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3. 在当前模块 开启 Feign 扫描

在当前模块的启动类,添加如下注解:

@EnableFeignClients

参看截图:

image.png

4. 定义远程服务接口

注意:Feign 只需要定义远程服务接口,而不需要实现 /order/src/main/java/org/example/service/ProductService.java

package org.example.service;

import org.example.entities.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value="service-product") // value 填写远程服务名称,与nacos列表内的服务名称一致
public interface ProductService {

    @RequestMapping("/product/{pid}")
    Product findByPid(@PathVariable Integer pid);
    
}

5. 调用远程服务

//注入
@Autowired
private ProductService productService;
//调用远程服务
Product product = productService.findByPid(pid);

完整代码如下:

package org.example.controller;


import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.entities.Order;
import org.example.entities.Product;
import org.example.service.OrderService;
import org.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private ProductService productService;


    /**
     * 下单--自定义负载均衡
     */
    @PostMapping("/order/prod/{id}")
    public Order order(@PathVariable("id") Integer pid) {
        log.info("进行{}号商品下单请求,接下来调用商品微服务查询此商品信息", pid);

        //调用商品微服务查询商品信息
        Product product = productService.findByPid(pid);
        log.info("查询到{}号商品信息,内容是:{}", pid, JSON.toJSONString(product));
        Order order = new Order();
        if (product != null) {
            order.setUid(1);
            order.setUsername("测试用户1");
            order.setProductId(pid);
            order.setProductName(product.getProductName());
            order.setProductPrice(product.getProductPrice());
            order.setNumber(1);
            orderService.createOrder(order);
            log.info("创建订单成功,订单信息:{}", JSON.toJSONString(order));
        } else {
            log.info("商品查询无该商品");
        }
        return order;
    }
}

6. 验证

接口调用请求: http://127.0.0.1:8001/order/prod/1

image.png

成功!