一、介绍
(1)是一款客户端组件,SpringCloud在Feign的基础上进行二次开发得到兼容SpringMVC的OpenFegin。
(2)简化了RestTemplate的请求操作代码(如编写URL、参数等),可提前编写好接口指定调用哪个微服务上的接口,即一处编写多处使用。
(3)OpenFeign集成了Ribbon,即支持负载均衡,自动启用。
二、使用
(1)新建子模块
(2)编写pom文件
<?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>demo20220821</artifactId>
<groupId>com.wsh.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-order80</artifactId>
<dependencies>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.wsh.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般基础通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(3)编写yml文件
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
# 客户端设置为true
register-with-eureka: true
# 客户端设置为true
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
(4)编写启动类
@SpringBootApplication
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class, args);
}
}
(5)开启OpenFeign功能
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class, args);
}
}
(6)编写Feign要调用其他服务的接口
@FeignClient("CLOUD-PAYMENT-SERVICE")
@Component
public interface OrderService {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CommonResult<Payment> selectById(@PathVariable("id") Long id);
}
对应
(7)编写controller
@Slf4j
@RequestMapping("/order")
@RestController
public class OrderFeignController {
@Autowired
private OrderFeignSerice orderFeignSerice;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CommonResult<Payment> selectById(@PathVariable("id") Long id){
return orderFeignSerice.selectById(id);
}
}
(8)测试