Nacos Spring Cloud 使用@FeignClient 呼叫其他工程的介面

bulingbuling^_^發表於2020-11-02

1. 新增依賴檔案(父工程)

1.1 版本管理:

 <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
        <spring-cloud-alibaba.version>0.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <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>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

1.2 服務提供: 

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

2. 呼叫端

2.1 服務消費者: 直接把父工程的pom.xml 複製即可。

在父工程配置服務名稱和Nacos地址:bootstrap.yml

spring:
  cloud:
    nacos:
      discovery:
        server-addr: nacos-headless.h5-web:8848

  application:
    name: login-service

父工程方法

@RequestMapping("/aa")
public class Controller {
    @PostMapping(value = "login")
    public Response login(@RequestBody User user) {
}
}

 呼叫服務提供方的介面:

@FeignClient(name = "login-service", contextId = "xxId", path = "/aa")
public interface xxService {
   
    @PostMapping(value = "login")
    public Response login();
}

 

 

相關文章