广东科技网站建设,阳春网站开发,企业网站如何去做优化,新榜数据平台一#xff1a;OpenFeign是什么#xff1f;
是一个声明式的web客户端,只需要创建一个接口,添加注解即可完成微服务之间的调用
二#xff1a;调用微服务的方式#xff1f;
ribbon restTemplate方式调用openFeign通过接口注解的方式调用
三#xff1a;如何使用OpenFeignOpenFeign是什么
是一个声明式的web客户端,只需要创建一个接口,添加注解即可完成微服务之间的调用
二调用微服务的方式
ribbon restTemplate方式调用openFeign通过接口注解的方式调用
三如何使用OpenFeign
pom文件添加依赖yaml配置文件主启动类标注EnableFeignClients注解编写调用接口并标注FeignClient注解接口中的方法为实际想要调用的服务的方法
四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);}
}六面试题
Feign和openFeign有什么区别