springcloud 整合openFeign

使用Feign可以完成服务间调用,但是总存在一种情况:服务提供方没有注册到注册中心、服务提供方还没开发完成(因此也就无法调用)等等 。此时如果我们需要完成服务之间调用该如何做呢?
Feign提供了fallback机制,也就是当对方服务还没ready(一般情况是服务提供方在注册中心上没有可用的实例),可以返回一些信息供服务进行下,也就是服务降级 。
openFeign特性1、实现服务之间的调用,并且底层封装了ribbon插件,可以实现负载均衡
2、可以实现服务降级
服务搭建

  1. 添加依赖包
【springcloud 整合openFeign】 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-openfeign-core</artifactId></dependency>2、
@FeignClient(contextId = "remoteAdminOutService", value = https://www.isolves.com/it/cxkf/jiagou/2022-03-03/ServiceNameConstants.VIDEO_GATEWAY_ADMIN_OUT, fallbackFactory = RemoteAdminOutFallbackFactory.class)public interface RemoteAdminOutService {}3、
@Component@Slf4jpublic class RemoteAdminOutFallbackFactory implements FallbackFactory<RemoteAdminOutService> {@Overridepublic RemoteAdminOutService create(Throwable throwable) {return null;}} 
4、请求方:
@Bean@ConditionalOnMissingBeanpublic HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));}5、请求测试
 
@GetMApping("test1")public void test1() {ResponseEntity<String> forEntity = restTemplate.getForEntity("http://video-gateway-dispose-inner/test1", String.class);log.info("test1 ---- {}", forEntity.getBody());String test = remoteDisposeInnerService.test();System.out.println(test);}



    推荐阅读