微服务之间接口调用(FeignClient)

第一步:加载jar包 



   org.springframework.cloud
   spring-cloud-starter-feign

第二步:配置类

server.port=9001

spring.application.name=feign-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

第三步:服务层

@FeignClient(name = "微服务名称",fallback = XxxFeignServiceImpl.class)
public interface XxxxFeignService {

    @GetMapping(value="/xxxx/getXxx/{taskId}")
    List getXxxx(@PathVariable("xxx") Integer xxx);
} 
  

fallback :断路器,当时接口执行异常时,调用该接口实现方法

@Service
public class XxxxxFeignServiceImpl implements XxxxFeignService{
   
    @Override
    public List getXxxx(Integer xxx) {
        log.error("调用{}异常:{}", "根据ID获取列表", xxx);
        return null;
    }
}

第四步:启动类配置

@EnableAsync
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@ComponentScan(basePackages = {"Xxxx.xxx.xxx", "xxx.xxx.xxx"})
public class XxxApplication {
    public static void main(String[] args) {
        SpringApplication.run(XxxxApplication.class, args);
    }
}

 

 

 

你可能感兴趣的:(spring,cloud)