第一步:加载jar包 

<!--spring-cloud-starter-feign-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

第二步:配置类

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<Object> getXxxx(@PathVariable("xxx") Integer xxx);
}

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

@Service
public class XxxxxFeignServiceImpl implements XxxxFeignService{
   
    @Override
    public List<JobInfoVo> 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);
    }
}

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐