4.open feign
一、OpenFeign是什么
OpenFeign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可
二、OpenFeign能干什么
Feign旨在使编写Java Http客户端变得更容易。
前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud.Ribbon时,自动封装服务调用客户端的开发量。
三、Feign集成了Ribbon
- 利用Ribon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用
四、OpenFeign使用步骤
- 创建工程cloud-consumer-feign-order80
- 修改pom.xml文件,添加openfeign依赖以及一些必要依赖
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-openfeign</artifactId> 4 </dependency>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>SpringCloudStudy</artifactId> 7 <groupId>com.junfu.springcloud</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>cloud-consumer-feign-order80</artifactId> 13 14 <properties> 15 <maven.compiler.source>8</maven.compiler.source> 16 <maven.compiler.target>8</maven.compiler.target> 17 </properties> 18 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-web</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-actuator</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-devtools</artifactId> 31 <scope>runtime</scope> 32 <optional>true</optional> 33 </dependency> 34 <dependency> 35 <groupId>org.projectlombok</groupId> 36 <artifactId>lombok</artifactId> 37 <optional>true</optional> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-test</artifactId> 42 <scope>test</scope> 43 </dependency> 44 <dependency> 45 <groupId>com.junfu.springcloud</groupId> 46 <artifactId>cloud-api-commons</artifactId> 47 <version>${project.version}</version> 48 </dependency> 49 50 <dependency> 51 <groupId>org.springframework.cloud</groupId> 52 <artifactId>spring-cloud-starter-openfeign</artifactId> 53 </dependency> 54 </dependencies> 55 </project>
- 修改application.yaml文件
1 server: 2 port: 80 3 4 5 eureka: 6 client: 7 register-with-eureka: false 8 service-url: 9 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
- 创建主启动类:OpenFeignMain80
1 package com.junfu.springcloud; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.openfeign.EnableFeignClients; 6 7 @SpringBootApplication 8 @EnableFeignClients 9 public class OrderFeignMain80 { 10 public static void main(String[] args) { 11 SpringApplication.run(OrderFeignMain80.class,args); 12 } 13 }
- 创建接口
package com.junfu.springcloud.service; import com.junfu.springcloud.entities.CommonResult; import com.junfu.springcloud.entities.Payment; import feign.Param; 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; @Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentService { @GetMapping(value = "/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id); }
- 创建controller
1 package com.junfu.springcloud.controller; 2 3 import com.junfu.springcloud.entities.CommonResult; 4 import com.junfu.springcloud.entities.Payment; 5 import com.junfu.springcloud.service.PaymentFeignService; 6 import lombok.extern.slf4j.Slf4j; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.GetMapping; 9 import org.springframework.web.bind.annotation.PathVariable; 10 import org.springframework.web.bind.annotation.RestController; 11 12 import javax.annotation.Resource; 13 14 @RestController 15 @Slf4j 16 public class OrderFeignController { 17 @Resource 18 private PaymentFeignService paymentFeignService; 19 20 @GetMapping(value = "/consumer/payment/get/{id}") 21 public CommonResult getPaymentById(@PathVariable("id") Long id){ 22 log.info("feign测试"); 23 CommonResult paymentById = paymentFeignService.getPaymentById(id); 24 return paymentById; 25 } 26 }
- 测试