1、在消费端的项目中引入openfeign依赖
首先需要确保引入了springcloud,因为openfeign依赖与springcloud
在消费端的pom.xml中引入openfeign,父项目中已经引入了springcloud了
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springalibabanew</artifactId>
<groupId>com.chinasofti</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-openfeign</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos-服务注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--添加openfeign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
父项目的pom.xml已经引入了springcloud
2、添加openfeign接口和方法
openfeign是解决服务之间调用的,我们之前在订单中调用库存的服务了,我们尽量让feign的名字和库存的服务的名字保持一致,这样方便使用,同样的方法也用controller中的方法,controller中怎么定义的这边就怎么调用,比如原先的StockController
package com.chinasofti.stock.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author mxx
* @Date 2023/6/12 16:48
* @Version 1.0
*/
@RestController
@RequestMapping("/stock")
public class StockController {
@Value("${server.port}")
String port;
@RequestMapping("/reduct")
public String reduct(){
System.out.println("扣减库存");
return "扣减库存:"+port;
}
}
对应的openfeign接口如下:
package com.chinasofti.order.feign;
/**
* @Author mxx
* @Date 2023/6/15 11:00
* @Version 1.0
*/
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* name指定调用rest接口所对应的服务名
* path指定调用rest接口所在的StockController指定的RequestMapping
*/
@FeignClient(name="stock-service",path="/stock")
public interface StockFeignService {
//声明需要调用的rest接口对应的方法,将原先的StockController中的方法复制过来
@RequestMapping("/reduct")
String reduct();
}
3、客户端调用代码修改
原先使用了restTemplate,现在将这个删除掉
将创建的feign注入到OrderController中,然后调用里面的方法,如下:
package com.chinasofti.order.controller;
import com.chinasofti.order.feign.StockFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Author mxx
* @Date 2023/6/12 16:44
* @Version 1.0
*/
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
StockFeignService stockFeignService;
@RequestMapping("/add")
public String add(){
System.out.println("下单成功");
String msg = stockFeignService.reduct();
return "Hello Feign!"+msg;
}
}
但是这样stockFeignService下边有个波浪线,如下
我们需要在启动类中加入@EnableFeignClients注解,如下:
package com.chinasofti.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @Author mxx
* @Date 2023/6/12 16:55
* @Version 1.0
*/
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
这样那个波浪线就消失了。修改端口为8040,然后启动订单和stock服务
启动成功后通过浏览器访问,如下:
openfeign就调用成功了。