由于作者觉得用RestTemplate调用服务比较low,
而且Fegin和之后讲的GateWay网关都带着Ribbon的功能,
所以直接跳过RestTemplate和Ribbon。
需要了解RestTemplate,Ribbon的可以自行再找找资料。
之前我们接触的Fegin,SpringCloud公司在Fegin基础上又开发出了OpenFegin。其实OpenFegin可以理解为Fegin的升级版。

上一讲我们已经将提供服务注册到Eureka7001里,如图。

首先我们先看看8080服务提供了什么。这里展示controller,一个是访问成功,一个是延迟3s返回。不懂的看上一章节。如图:


了解完毕开始进入正题,创建我们的openFegin工程。
编辑 pom.xml文件,添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>com.cloudH</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>fegin80</artifactId>
<description>订单消费者之feign</description>
<dependencies>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
添加application.yml
register-with-eureka:不向注册中心注册自己
fetch-registry: 不屏蔽注册信息
defaultZone: http://localhost:7001/eureka 消费7001服务注册中心的服务
server:
port: 80
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
重点来了,创建我们常用的service类。

FeignClient:注释标明我们这个service消费哪个hostname的服务,我们可以通过访问我们的注册中心查看,localhost:7001(不懂的看前面几章)如下图:

/**
* @author zzyy
* @create 2020-02-19 23:59
*/
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentFeignService {
/**
* 根据id查询
*
* @param id
* @return
*/
@GetMapping(value = "payment/get/{id}")
CommonResult getPaymentById(@PathVariable("id") Long id);
/**
* 模拟feign超时
*
* @return
*/
@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeout();
/**
* 正常访问
*
* @param id
* @return
*/
@GetMapping(value = "payment/hystrix/ok/{id}")
String paymentInfo_OK(@PathVariable("id") Long id);
}
而此service的GetMapping注释则遵循服务提供方中Controller的Mapping对应起来。如paymentInfo_OK这个接口,对应着服务提供的:

接下来则是OpenFegin工程的Controller
@RestController
@Slf4j
public class OrderFeignClientController {
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
return paymentFeignService.getPaymentById(id);
}
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout() {
// openfeign-ribbon, 客户端一般默认等待1秒钟
return paymentFeignService.paymentFeignTimeout();
}
@GetMapping(value = "payment/hystrix/ok/{id}")
public String<Payment> paymentInfo_OK(@PathVariable("id") Long id) {
// 成功访问
return paymentFeignService.paymentInfo_OK(id);
}
}
启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author zzyy
* @date 2020/02/18 17:20
**/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class, args);
}
}
启动访问:http://localhost/payment/hystrix/ok/1

说明我们的openFegin第一步成功。
第二步:超时控制
openFegin超时时间默认为2s,当我们访问服务提供的timeOut服务时会报超时。我们看一下我们的服务提供。
image.png
我们继续用OpenFegin来访问此服务
http://localhost/consumer/payment/feign/timeout

发现报错超时,因为openFegin默认超时时间为2s,而我们的服务提供睡眠了3s。
修改OpenFegin的yml文件
server:
port: 80
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
#,http://localhost:7002/eureka
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
# 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
ReadTimeout: 5000
# 指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
修改超时时间为5s,重启openFegin工程。再访问
http://localhost/consumer/payment/feign/timeout
成功访问,不再报错。

第三步:刚才说了oepnFegin也带Ribbon负载均衡。
我们已经启动了8080的服务提供,我们再启动一个同样的工程,端口改为8082的服务提供。
启动后我们再访问localhost:7001查看服务注册中心,发现CLOUD-PROVIDER-HYSTRIX-PAYMENT服务已经跑着两个端口的服务了。如图:

这时我们再访问我们的OpenFegin
http://localhost/payment/hystrix/ok/1
交替出现 8080,8082


说明OpenFegin确实带着Ribbon负载均衡的效果
End。。。。
网友评论