Springcloud基于OpenFeign实现服务调用的案例
更新:HHH   时间:2023-1-7


这篇文章给大家分享的是有关Springcloud基于OpenFeign实现服务调用的案例的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

1.依赖

  <!--引入open feign依赖-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
</dependencies>

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 #配置所有服务等待超时

感谢各位的阅读!关于Springcloud基于OpenFeign实现服务调用的案例就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

返回开发技术教程...