OpenFeign讲解+面试题

编程入门 行业动态 更新时间:2024-10-13 08:21:54

OpenFeign讲解+<a href=https://www.elefans.com/category/jswz/34/1769418.html style=面试题"/>

OpenFeign讲解+面试题

一:OpenFeign是什么?

是一个声明式的web客户端,只需要创建一个接口,添加注解即可完成微服务之间的调用

二:调用微服务的方式?

  1. ribbon +restTemplate方式调用
  2. openFeign通过接口+注解的方式调用

三:如何使用OpenFeign?

  1. pom文件添加依赖
  2. yaml配置文件
  3. 主启动类,标注@EnableFeignClients注解
  4. 编写调用接口并标注@FeignClient注解
  5. 接口中的方法为实际想要调用的服务的方法

四:OpenFeign超时机制

因为OpenFeign的底层是ribbon进行负载均衡,所以它的超时时间是由ribbon控制

五:底层核心原理

底层通过JDK动态代理获取到接口中的服务信息,使用Ribbon管理后的RestTemplate进行调用

@SpringBootTest
class ApplicationTests {@Autowiredprivate RestTemplate restTemplate;@Testvoid contextLoads() {UserOrderFeign o = (UserOrderFeign) Proxy.newProxyInstance(UserOrderFeign.class.getClassLoader(), new Class[]{UserOrderFeign.class}, new InvocationHandler() {@Overridepublic 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();// 拼接URLString url = "http://"+applicationName+"/"+path;// 使用Ribbon托管后的RestTemplate进行调用return restTemplate.getForObject(url, String.class);}});String s = o.doOrder();System.out.println(s);}
}

六:面试题

  1. Feign和openFeign有什么区别?

更多推荐

OpenFeign讲解+面试题

本文发布于:2023-11-16 01:38:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1611058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:面试题   OpenFeign

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!