本篇文章小编给大家分享一下Springcloud基于OpenFeign实现服务调用代码实例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1.依赖
org.springframework.cloud spring-cloud-starter-openfeign
2.启动注解
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class UserservicesApplication { public static void main(String[] args) { SpringApplication.run(UserservicesApplication.class, args); } }
3.接口
@FeignClient("productservices") public interface ProductClient { @RequestMapping("/product/findAll") public Map findAll(); }
4.服务调用
@Autowired private ProductClient productClient; @RequestMapping("/user/showProductMsg") public Map showProductMsg() { Map msg = productClient.findAll(); return msg; }
5.超时设置
feign.client.config.default.connectTimeout=5000 #配置所有服务连接超时
feign.client.config.default.readTimeout=5000 #配置所有服务等待超时