集成Ribbon的客户端调用工具——Feign
- 2019 年 10 月 6 日
- 筆記
什么是Feign?
客户端调用工具
- Ribbon+RestTemplate
- Feign
Feign特性:
- Feign本身包含Ribbon
- Fegin是一个采用基于接口的注解的声明式客户端调用工具,更加简便
基于上个环境来做一些改造:
https://github.com/HCJ-shadow/Ribbon
创建msc-consumer-feign-80工程

pom依赖
主要是spring-cloud-starter-openfeign
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- Eureka客户端启动需要依赖web模块--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
定义service接口
package zkrun.top.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("msc-provider") //@FeignClient("服务名") public interface FeignService { @RequestMapping(value = "/info/get") public String request(); }
定义Controller调用Service接口
package zkrun.top.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import zkrun.top.service.FeignService; @RestController public class FeignController { @Autowired FeignService feignService; @RequestMapping(value = "/feign/info/get") public String request() { return this.feignService.request(); } }
主启动类
package zkrun.top; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients @EnableEurekaClient public class App_Consumer_Feign_80 { public static void main(String[] args) { SpringApplication.run(App_Consumer_Feign_80.class, args); } }
application.yaml
server: port: 80 eureka: client: service-url: defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6002.com:6002/eureka/,http://eureka6003.com:6003/eureka/ spring: application: name: feign-consumer
运行测试

集成了轮询效果。
小结:
- Feign的调用逻辑,访问Controller定义的路径,调用feignService.request()方法,而service接口层中定义了服务名和服务请求路径,最后通过服务名和请求路径实现对服务提供者的调用。
- Feign的接口在实际开发中经常抽取出一个单独模块,以便复用。
代码参考:https://github.com/HCJ-shadow/Feign