服務(wù)間調(diào)用-openFeign

Feign 簡介

Feign 是聲明式的服務(wù)調(diào)用工具,我們只需創(chuàng)建一個接口并用注解的方式來配置它,就可以實現(xiàn)對某個服務(wù)接口的調(diào)用,簡化了直接使用 RestTemplate 來調(diào)用服務(wù)接口的開發(fā)量。
接下來,我們將新建product-service和order-service,實現(xiàn)product-service以HTTP Rest方式調(diào)用order-service的服務(wù)。

1.新建Spring boot Web項目,application name 為 product-service

在pom.xml中引入依賴

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

spring-cloud-starter-alibaba-nacos-discovery作用為向Nacos server注冊服務(wù)。
spring-cloud-starter-openfeign作用為實現(xiàn)服務(wù)調(diào)用。

2.修改application.yml配置文件

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: product-service
server:
  port: 8083

3.在啟動類上添加@EnableDiscoveryClient、@EnableFeignClients注解

package com.example.productservice;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ProductServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }

}

4.編寫OrderClient Interface

注:/api/v1/order/test 會在下面order-service聲明。
OrderClient.java

package com.example.productservice.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Component
@FeignClient("order-service")
public interface OrderClient {
    @RequestMapping(method = RequestMethod.GET, value = "/api/v1/order/test")
    String callOrder();
}

5.編寫Controller和service

ProductController.java

package com.example.productservice.controller;

import com.example.productservice.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {
    @Autowired
    ProductService productService;

    @RequestMapping(value = "testCallOrder", method = RequestMethod.GET)
    public String testCallOrder() {
        return productService.callOrder();
    }
}

ProductService.java

package com.example.productservice.service;

import com.example.productservice.client.OrderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductService {

    @Autowired
    OrderClient orderClient;

    public String callOrder() {
        String response = orderClient.callOrder();
        return response;
    }
}

6.新建Spring boot Web項目,application name 為 order-service

同樣在pom.xml中引入依賴

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

7.修改application.yml配置文件

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: -service
server:
  port: 8082

8.在啟動類上添加@EnableDiscoveryClient注解

package com.example.orderservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }

}

9.新建Controller

package com.example.orderservice.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/order")
public class OrderController {
    @RequestMapping("/test")
    public Object test(){
        return "Hello,this is order-service";
    }
}

測試

啟動product-service和order-service
訪問 http://localhost:8083/testCallOrder

openFeign.png

至此,利用openFeign進行服務(wù)間調(diào)用完成。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容