Spring cloud feign 实现远程调用

97 阅读1分钟

在实现远程调用的时候需要至少两个微服务

前期准备 例如 订单微服务 feign远程调用微服务
我先启动一个微服务,通过另外一个feign进行远程调用微服务
启动之后的接口测试为 http://127.0.0.1:8087/order/list
order/list: response: {
    "code": 1,
    "message": "success",
    "data": {
        "pageNum": 1,
        "pageSize": 10,
        "total": 12,
        "rows": [
            {
                "Id": "0acc893a-91d3-4d67-ba07-9cfb3d2a7470",
                "codeName": "SUNSANG",
                "orderCreateTime": "2023-05-19 17:37:46",
            },
            {
                "Id": "0c1e8995-55d9-4f5b-ac5d-1f488d910518",
                "codeName": "HUAWEI",
                "orderCreateTime": "2023-05-20 21:47:46",
            }
        ]
    }

}

现在编写feign的远程接口 引入maven依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>midea.org.springframework.cloud</groupId>
        <artifactId>spring-cloud-openfeign-core</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>

</dependencies>
<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>
    </dependencies>
</dependencyManagement>

spring cloud 结构 spring boot 2.6.7

image.png



package com.aaa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class FeignServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignServerApplication.class, args);
    }

}

com.aaa.feign

package com.aaa.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "activeService", url= "http://localhost:8087")
public interface ServiceFeignClient {
    @GetMapping("/order/list")
    String getOrderResponse();
}

com.aaa.controller

package com.aaa.controller;

import com.aaa.feign.ServiceFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @Autowired
    private ServiceFeignClient serviceFeignClient;

    @GetMapping("/feign")
    public String useService(){
        System.out.println("fegin");
        return serviceFeignClient.getOrderResponse();
    }
}

启动服务至8080端口,进行接口远程调用 curl http://localhsot:8080/feign response: 会返回和order/list的数据一样 postman:

image.png