docs/Spring全家桶/SpringCloud/SpringCloudOpenFeign.md
Netflix Feign Netflix ˾һʵָؾͷõĿԴSpring Cloud Netflix еԴ EurekaRibbon Լ Hystrix ȣһϽ Spring Cloud Netflix ģУϺȫΪ Spring Cloud Netflix Feign
Feign Ribbon ˼ɣ Ribbon άһݿ÷嵥ͨ Ribbon ʵ˿ͻ˵ĸؾ⡣
Feign һʽ RestTemplate Ļ˽һķװͨ FeignֻҪһӿڲͨעмã Dao ӿ Mapper עһʵֶ HTTP ӿڵİ
ͨ FeignǿñطһԶ̷ȫоڽԶ̵á
Feign ֶ֧ע⣬ Feign ԴעԼ JAX-RS עȣź Feign ֧ Spring MVC ע⣬ɻ Spring û㡣
2019 Netflix ˾ Feign ʽͣά״̬ Spring ٷƳһΪ OpenFeign Ϊ Feign
OpenFeign ȫ Spring Cloud OpenFeign Spring ٷƳһʽ븺ؾij־Ϊͣά״̬ Feign
OpenFeign Spring Cloud Feign Ķηװ Feign йܣ Feign Ļ˶ Spring MVC ע֧֣ @RequestMapping@GetMapping @PostMapping ȡ
ʹ OpenFegin Զ̷ʱע±
| ע | ˵ |
|---|---|
| @FeignClient | ע֪ͨ OpenFeign @RequestMapping עµĽӿڽн̬ͨķʽʵ࣬ʵָؾͷá |
| @EnableFeignClients | עڿ OpenFeign ܣ Spring Cloud ӦʱOpenFeign ɨ @FeignClient עĽӿڣɴעᵽ Spring С |
| @RequestMapping | Spring MVC ע⣬ Spring MVC ʹøעӳָͨControllerԴЩ URL ൱ Servlet web.xml á |
| @GetMapping | Spring MVC ע⣬ӳ GET һע⣬൱ @RequestMapping(method = RequestMethod.GET) |
| @PostMapping | Spring MVC ע⣬ӳ POST һע⣬൱ @RequestMapping(method = RequestMethod.POST) |
Spring Cloud Finchley ϰ汾һʹ OpenFeign Ϊ OpenFeign 2019 Feign ͣάƳģ˴ 2019 꼰ԺĿʹõĶ OpenFeign 2018 ǰĿһʹ Feign
ǾԱ Feign OpenFeign ͬ
Feign OpenFegin ͬ㣺
Feign OpenFeign ²ͬ
Ǿͨһʵʾͨ OpenFeign ʵԶ̷õġ
1. spring-cloud-demo2 ´һΪ micro-service-cloud-consumer-dept-feign Spring Boot ģ飬 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-cloud-demo2</artifactId>
<groupId>net.biancheng.c</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>net.biancheng.c</groupId>
<artifactId>micro-service-cloud-consumer-dept-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>micro-service-cloud-consumer-dept-feign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>net.biancheng.c</groupId>
<artifactId>micro-service-cloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<!--Eureka Client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- OpenFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. micro-service-cloud-consumer-dept-feign µ· /resources Ŀ¼£һ application.yml¡
server:
port: 80
eureka:
client:
register-with-eureka: false #߿Բעע
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
fetch-registry: true #߿ͻҪȥ
3. net.biancheng.c.service ´һΪ DeptFeignService Ľӿڣڸýӿʹ @FeignClient עʵֶԷӿڵİ¡
package net.biancheng.c.service;
import net.biancheng.c.entity.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
//Ϊڵһ
@Component
// ṩṩķƣ application.name
@FeignClient(value = "MICROSERVICECLOUDPROVIDERDEPT")
public interface DeptFeignService {
//Ӧṩߣ800180028003Controller жķ
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") int id);
@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
public List<Dept> list();
}
ڱдӿʱҪע 2 㣺
4. net.biancheng.c.controller £һΪ DeptController_Consumer Controller ࣬¡
package net.biancheng.c.controller;
import net.biancheng.c.entity.Dept;
import net.biancheng.c.service.DeptFeignService;
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;
import java.util.List;
@RestController
public class DeptController_Consumer {
@Resource
private DeptFeignService deptFeignService;
@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Integer id) {
return deptFeignService.get(id);
}
@RequestMapping(value = "/consumer/dept/list")
public List<Dept> list() {
return deptFeignService.list();
}
}
5. @EnableFeignClients ע OpenFeign ܣ¡
package net.biancheng.c;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients // OpenFeign
public class MicroServiceCloudConsumerDeptFeignApplication {
public static void main(String[] args) {
SpringApplication.run(MicroServiceCloudConsumerDeptFeignApplication.class, args);
}
}
Spring Cloud ӦʱOpenFeign ɨ @FeignClient עĽӿɴע˵ Spring С
6. עļȺṩԼ micro-service-cloud-consumer-dept-feignɺʹʡhttp://eureka7001.com/consumer/dept/listͼ
ͼ1OpenFeign ʵԶ̷
7. ηʡhttp://eureka7001.com/consumer/dept/listͼ
ͼ2OpenFeign ؾ
ͼ 2 Կ OpenFeign RibbonҲʵ˿ͻ˵ĸؾ⣬ĬϸؾΪѯԡ
OpenFeign ͻ˵ĬϳʱʱΪ 1 ӣ˴ʱ䳬 1 ͻᱨΪ˱Ҫ OpenFeign ͻ˵ijʱʱпơ
Ǿͨһʵʾ OpenFeign νгʱƵġ
1. еķṩߣˣ DeptController һӦʱΪ 5 ķ¡
//ʱ,÷ӦʱΪ 5
@RequestMapping(value = "/dept/feign/timeout")
public String DeptFeignTimeout() {
//ͣ 5
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
return serverPort;
}
2. micro-service-cloud-consumer-dept-feign DeptFeignService ӿ´룬˸ոӵijʱ
@RequestMapping(value = "/dept/feign/timeout")
public String DeptFeignTimeout();
3. micro-service-cloud-consumer-dept-feign DeptController_Consumer ´롣
@RequestMapping(value = "/consumer/dept/feign/timeout")
public String DeptFeignTimeout() {
// openFeign-ribbon ͻһĬϵȴһӣʱͻᱨ
return deptFeignService.DeptFeignTimeout();
}
ͼ3ṩߵijʱ
5. micro-service-cloud-consumer-dept-feignʹʡhttp://eureka7001.com/consumer/dept/feign/timeoutͼ
ͼ4OpenFeign ʱ
6. micro-service-cloud-consumer-dept-feign application.yml ãʱʱΪ 6 롣
ribbon:
ReadTimeout: 6000 #õʱ䣬״£õʱ
ConnectionTimeout: 6000 #ӺȡԴʱ
ע OpenFeign Ribbon Լؾڵײ㶼 Ribbon ʵֵģ OpenFeign ʱҲͨ Ribbon ʵֵġ
7. ٴ micro-service-cloud-consumer-dept-feignʹʡhttp://eureka7001.com/consumer/dept/feign/timeoutͼ
ͼ5OpenFeign ʱ
OpenFeign ṩ־ӡܣǿͨõ־˽ϸڡ
Feign Ϊÿһ FeignClient ṩһ feign.Logger ʵͨԶ OpenFeign ӿڵĵмء
OpenFeign ־ӡܵĿʽȽϼǾͨһʵʾ
1. micro-service-cloud-consumer-dept-feign application.yml ݡ
logging:
level:
#feign ־ʲôļظýӿ
net.biancheng.c.service.DeptFeignService: debug
˵£
õĺǣOpenFeign debug net.biancheng.c.service.DeptFeignService ӿڡ
package net.biancheng.c.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigBean {
/**
* OpenFeign ־ǿ
* OpenFeign ¼Щ
*/
@Bean
Logger.Level feginLoggerLevel() {
return Logger.Level.FULL;
}
}
õͨõ Logger.Level OpenFeign ¼Щ־ݡ
Logger.Level ľ弶£
3. micro-service-cloud-consumer-dept-feignʹʡhttp://eureka7001.com/consumer/dept/list̨¡
2021-10-12 14:33:07.408 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] ---> GET http://MICROSERVICECLOUDPROVIDERDEPT/dept/list HTTP/1.1
2021-10-12 14:33:07.408 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] ---> END HTTP (0-byte body)
2021-10-12 14:33:07.983 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] <--- HTTP/1.1 200 (574ms)
2021-10-12 14:33:07.983 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] connection: keep-alive
2021-10-12 14:33:07.983 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] content-type: application/json
2021-10-12 14:33:07.983 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] date: Tue, 12 Oct 2021 06:33:07 GMT
2021-10-12 14:33:07.983 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] keep-alive: timeout=60
2021-10-12 14:33:07.983 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] transfer-encoding: chunked
2021-10-12 14:33:07.983 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list]
2021-10-12 14:33:07.991 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] [{"deptNo":1,"deptName":"","dbSource":"bianchengbang_jdbc"},{"deptNo":2,"deptName":"²","dbSource":"bianchengbang_jdbc"},{"deptNo":3,"deptName":"","dbSource":"bianchengbang_jdbc"},{"deptNo":4,"deptName":"г","dbSource":"bianchengbang_jdbc"},{"deptNo":5,"deptName":"ά","dbSource":"bianchengbang_jdbc"}]
2021-10-12 14:33:07.991 DEBUG 13388 --- [p-nio-80-exec-2] n.biancheng.c.service.DeptFeignService : [DeptFeignService#list] <--- END HTTP (341-byte body)```
https://lijunyi.xyz/docs/SpringCloud/SpringCloud.html#_2-2-x-%E5%88%86%E6%94%AF https://mp.weixin.qq.com/s/2jeovmj77O9Ux96v3A0NtA https://juejin.cn/post/6931922457741770760 https://github.com/D2C-Cai/herring http://c.biancheng.net/springcloud https://github.com/macrozheng/springcloud-learning