第一步:创建项目
参考 nacos入门案例的《第三部分:编写订单服务》拷贝一份,修改项目名为:order-openfeign第二步:修改pom.xml
<?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>springcloudalibaba</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-openfeign</artifactId>
<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依赖,此场景父类必须有springcloud依赖管理器,如没有,需要指定版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
第三步:新建StockFeignService
package com.example.order.feign;
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接口对应的方法
* 保持跟被调用方的controller一致,去掉方法体和public
* 下面注释的地方就是该场景要调用方的controller接口(rest接口)
*/
@RequestMapping("/reduck")
String reduck();
/**
* @RestController
* @RequestMapping("/stock")
* public class StockController {
*
* @Value("${server.port}")
* private String port;
*
* @RequestMapping("/reduck")
* public String reduck(){
* System.out.println("扣减库存");
* return "扣减库存" + port;
* }
* }
*/
}
第四步:修改OrderApplication
删除不用的restTemplate和添加启动openFeign组件
package com.example.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;
@SpringBootApplication
@EnableFeignClients //启动openFeign组件
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
第五步:修改OrderController
package com.example.order.controller;
import com.example.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;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private StockFeignService stockFeignService;
@RequestMapping("/add")
public String add(){
System.out.println("下单成功");
String msg = stockFeignService.reduck();
return "Hello World " + msg;
}
}
第六步:修改application.yml
server:
port: 8086
#应用名称(nacos会将该名称当做服务名称)
spring:
application:
name: order-service
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
username: nacos
password: nacos
namespace: public
第七步:启动服务测试
访问:http://localhost:8086/order/add
feign这种远程调用方式它也会自动的帮我们集成ribbon,集成负载均衡器,同样的也会帮我们集成nacos,会根据我@FeignClient(name = “stock-service”,path = “/stock”)注解的name中的服务名去nacos中获取对应的所有的实例,然后在结合负载均衡器来进行调用,feign使用了动态代理。