overview
OpenFeign is a
声明式
WEB service client that makes it easier for WEB service clients. With可插拔
annotation support, Spring Cloud has added support for SpringMVC annotations. SpringCloud integrates Ribbon and Eureka, as well as SpringCloud LoadBalance, to provide a load-balanced HTTP client when using Feign. Feign is a远程调用
component that integrates Ribbon. The default load balancing strategy is轮询
OpenFeign use cases
① Call the design diagram:
② order-service
server: port: 8080 spring: application: name: order-service eureka: client: service-url: defaultZone: http://localhost:8761/eureka instance: lease-renewal-interval-in-seconds: 5
provide API
@RestController public class OrderController { @GetMapping("doOrder") public String doOrder(){ return "鱼香肉丝"; } }
③ user-service
As a consumer needs to make a remote call, add OpenFeign dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
configuration file
server: port: 8081 spring: application: name: user-service eureka: client: service-url: defaultZone: http://localhost:8761/eureka instance: lease-renewal-interval-in-seconds: 5
Add annotations to the startup class
@EnableOpenFeignClients
to enable the remote call function of the OpenFeign client@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); } }
Define an interface, add
@FeignClient
annotations and specify the provider's service nameThe methods in the interface are part of the API signature of the provider
/** * @FeignClient:value=提供者的服务名 */ @FeignClient("order-service") public interface UserOrderFeign { /** * 提供者的方法签名 * 除方法体之外的全部属性 */ @GetMapping("doOrder") String doOrder(); }
Create a Controller to inject this interface and provide an API for the browser to request
@RestController public class UserController { @Autowired private UserOrderFeign userOrderFeign; @GetMapping("userDoOder") public String userDoOder(){ return userOrderFeign.doOrder(); } }
④ Set timeout
The default timeout is 1s, modify the Ribbon configuration, query DefaultClientConfigImpl
ribbon: ReadTimeout: 3000 #调用超时时间 3s ConnectTimeout: 3000 #连接超时时间 3s
underlying core principles
The bottom layer
JDK动态代理
obtains the service information in the interface and uses Ribbon to manage theRestTemplate
call@SpringBootTest class ApplicationTests { @Autowired private RestTemplate restTemplate; @Test void contextLoads() { UserOrderFeign o = (UserOrderFeign) Proxy.newProxyInstance(UserOrderFeign.class.getClassLoader(), new Class[]{UserOrderFeign.class}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 获取目标方法上的注解 GetMapping MethodAnnotation = method.getAnnotation(GetMapping.class); // 获取注解上的请求路径 String path = MethodAnnotation.value()[0]; // 获取目标方法所在的类 Class<?> aClass = method.getDeclaringClass(); // 获取类上面的注解 FeignClient classAnnotation = aClass.getAnnotation(FeignClient.class); // 获取注解上的value值(服务名) String applicationName = classAnnotation.value(); // 拼接URL String url = "http://"+applicationName+"/"+path; // 使用Ribbon托管后的RestTemplate进行调用 return restTemplate.getForObject(url, String.class); } }); String s = o.doOrder(); System.out.println(s); } }
log
level
NONE: default, no logging
BASE: record request method, URL, response status code, execution time
HEADERS: Added request and response headers on BASE
FULL: Added request and response body, no data on HEADERS
Create a configuration class
@Configuration public class OpenFeignLevelConfig { @Bean public Logger.Level level(){ return Logger.Level.FULL; } }
Modify the configuration file
logging: level: com.jiuxiao.controller.feign.UserOrderFeign: debug
Summarize
1. openFeign is an HTTP client that integrates springmvc annotations so that it can use REST-style mapping to request forwarding.
2. OpenFegin can be understood as a controller layer or a service layer. It can replace the springmvc control layer as a request mapping, or as a service layer processing logic, but here, openFeign is just a logical operation of request forwarding.
3. openFeign integrates hystrix for fuse processing. At the same time, it can be used in conjunction with ribbon client load balancing and Eureka registration center to achieve load balancing client.
4. openFeign has a very important function: fallback, in fact, it is a feature of hystrix
interview related
1. How to use it?
First, both the caller and the called microservice should be registered with the registry .
Annotations are marked on the Spring Boot startup APP
@EnableFeignClients
.Write the remote call interface and annotate
@FeignClient
it. (Add the name of the microservice to be called in brackets)The method in the interface is the method signature of the service you actually want to call, and uses
@PostMapping
annotations to map to a post-type HTTP request.What is the difference between Feign and openFeign?
Feign openFiegn Feign is a lightweight RESTful HTTP service client in the SpringCloud component. Feign has a built-in Ribbon for client load balancing to call services in the service registry. The way to use Feign is: use Feign's annotations to define the interface, and call this interface to call the service of the service registration center OpenFeign is that SpringCloud supports SpringMVC annotations on the basis of Feign, such as @RequestMapping and so on. OpenFeign's @FeignClient can parse the interface under SpringMVC's @RequestMapping annotation, and generate an implementation class through a dynamic proxy, implement load balancing and call other services in the implementation class.