Feign 简介
Feign 是声明式的服务调用工具,我们只需创建一个接口并用注解的方式来配置它,就可以实现对某个服务接口的调用,简化了直接使用 RestTemplate 来调用服务接口的开发量。
接下来,我们将新建product-service和order-service,实现product-service以HTTP Rest方式调用order-service的服务。
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注册服务。
spring-cloud-starter-openfeign作用为实现服务调用。
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进行服务间调用完成。