07-SpringCloud 之 Feign
什么是 Feign
- Feign 即:基于接口的服务调用,是一个声明式的 Web Service 客户端
- Feign 集成了 Ribbon 可以使用 Ribbon 的负载均衡算法
Feign 的使用
-
新建 Module:spring-cloud-consumer-dept-feign-80
-
编写 pom 引入依赖
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>spring-cloud-netflix</artifactId> <groupId>com.kaishen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-consumer-dept-feign-80</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <!-- 引入该模块所需依赖 --> <dependencies> <!-- api --> <dependency> <groupId>com.kaishen</groupId> <artifactId>spring-cloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--SpringBoot Web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot 监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Eureka Client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- Feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> </project> -
编写 application.yml 配置注册中心
application.yml
server: port: 80 # Eureka 配置 eureka: client: register-with-eureka: false # Consumer 无需注册 service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ -
编写启动类,开启 Feign 扫描:@EnableFeignClients(basePackages = {"com.kaishen"})
DeptConsumerFeignApplication80
package com.kaishen; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; /** * 部门服务消费者启动类 * Feign 实现远程调用 * @author : toby * Create in 11:23 2022/5/9 */ @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = {"com.kaishen"}) public class DeptConsumerFeignApplication80 { public static void main(String[] args) { SpringApplication.run(DeptConsumerFeignApplication80.class, args); } } -
定义接口,开启注解:@FeignClient(value = "SPRING-CLOUD-PROVIDER-DEPT", contextId = "1")
DeptFeignService
package com.kaishen.feign.service; import com.kaishen.pojo.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * 部门客户端服务 * @author : toby * Create in 11:26 2022/5/9 */ @Component @FeignClient(value = "SPRING-CLOUD-PROVIDER-DEPT", contextId = "1") public interface DeptFeignService { /** * 根据部门编号查询部门信息 * @param id 部门编号 * @return 返回部门信息 */ @GetMapping("/feign/dept/get/{id}") Dept feignQueryDeptById(@PathVariable("id") Long id); }注意:
- 通过 @FeignClient 注解调用同一个微服务时,会出现 Bean 重复注册的情况,可使用 contextId = "1" 来区分其唯一性
- // 此处的 @PathVariable 注解,必须指定("id")
-
编写 Controller 使用 Feign 实现远程调用
DeptConsumerFeignController
package com.kaishen.controller; import com.kaishen.feign.service.DeptFeignService; import com.kaishen.feign.service.DeptHystrixDemotionService; import com.kaishen.feign.service.DeptHystrixFusingService; import com.kaishen.pojo.Dept; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * 部门服务消费者 Feign 控制层 * @author : toby * Create in 11:43 2022/5/9 */ @RestController @Slf4j public class DeptConsumerFeignController { /** * Feign 实现远程服务调用 */ @Resource private DeptFeignService deptFeignService; // region Feign 实现远程服务调用 /** * 根据部门编号查询部门信息 * -- Feign 实现远程服务调用 * @param id 部门编号 * @return 返回部门信息 */ @RequestMapping("/feign/dept/get/{id}") public Dept feignQueryDeptById(@PathVariable Long id) { // Feign 实现远程服务调用 Dept dept = deptFeignService.feignQueryDeptById(id); log.info("Feign 查询结果:" + dept); return dept; } // endregion }

浙公网安备 33010602011771号