5-SpringCloud整合Feign调用
2019-12-21 本文已影响0人
杨庆祥
springcloud目录:
1.Spring Cloud简介
2.SpringCloud整合eureka注册中心
3.SpringCloud整合服务注册
4.SpringCloud整合服务间的调用即负载均衡
5.SpringCloud整合Feign调用
6.SpringCloud整合config配置中心
7.SpringCloud整合zuul路由网关
在我们yqx-order项目的基础上,加入feign的pom,由原来的通过RestTemplate+ribbon实现负载均衡改为由feign实现负载均衡
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
#服务名称
spring.application.name=yqx-feign
#端口号
server.port=8300
#在注册中心中进行注册
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8000/eureka
在启动中开启feign的使用

编写一个feign的地调用接口

在controller中增加通过feign调用的方法

启动注册中心、两个yqx-user实例、yqx-feign

在浏览器中输入 http://127.0.0.1:8300/getFeignUser,刷新几次查看结果


其实feign就是封装我们之前的RestTemplate+Ribbon
因为是http调用就存在超时的情况,feign默认的超时是1秒,可以通过配置文件修改,我们可以修改yqx-user的hello方法加一个sleep5秒就会出现超时报错,我们将调用时间修改为5秒
#设置feign客户端超时时间
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。
ribbon.ReadTimeout=5000
#指的是建立连接后从服务器读取到可用资源所用的时间。
ribbon.ConnectTimeout=5000