Spring Cloud入门系列(七)- 负载均衡之Open Feign
OpenFeign是什么![在这里插入图片描述](https://img-blog.csdnimg.cn/6a2162123e8c455fab8c0c6742de161a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBALmxheW1hbg==,size_20,color_FFFFFF,t_70,g_se,x_16)
OpenFeign能做什么
OpenFeign和Feign的区别
如何使用
代码演示
定义新的模块 cloud-consumer-openfeign-order80
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>cloud201230</artifactId>
<groupId>com.banana</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-openfeign-order80</artifactId>
<dependencies>
<!--引入自定义的api通用包,可以使用Payment支付Entity-->
<dependency>
<groupId>com.banana</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--open feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>
</dependencies>
</project>
application.yml
server:
port: 80
eureka:
client:
#是否将自己注册到注册中心, 默认true
register-with-eureka: false
#是否从EurekaServer抓取已有的注册信息,单机无所谓,集群必须设置为true配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka #单机版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
#设置feign客户端超时时间(OpenFeign 默认支持ribbon)
ribbon:
#指的是建立连接所用时间,适用于网络状况正常情况下,两端连接所用时间
ReadTimeout: 5000
#指的是连接建立后,从服务器获取到可用资源所用时间
ConnectTimeout: 5000
#feign 日志以什么级别监控那个接口
logging:
level:
com.banana.springcloud.service: debug
主启动类上,添加@EnableFeignClients
注解
package com.banana.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author layman
* @date 2021/1/17
*/
@SpringBootApplication
@EnableFeignClients
public class OpenFeignOrder80 {
public static void main(String[] args) {
SpringApplication.run(OpenFeignOrder80.class,args);
}
}
使用 接口+注解 的形式,调用服务
package com.banana.springcloud.service;
import com.banana.springcloud.entity.CommonResult;
import com.banana.springcloud.entity.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* FeignClient注解中的value属性指定了要调用的{服务}名
* @author layman
* @date 2021/1/17
*/
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface OpenFeignOrderService {
/**
* 查询payment
* @param id:主键
* @return CommonResult<Payment>
*/
@GetMapping(value = "/payment/get/{id}")
CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
/**
* 模拟openFeign访问超时
* @return String
*/
@GetMapping("/payment/openfeign/timeout")
String openFeignTimeOut();
}
@FeignClient(value = “CLOUD-PAYMENT-SERVICE”) :表示 该接口调用的是CLOUD-PAYMENT-SERVICE
当访问getPaymentById()方法时,就相当于访问:http://cloud-payment-service/payment/get/{id}
OpenFeignOrderController
package com.banana.springcloud.controller;
import com.banana.springcloud.entity.CommonResult;
import com.banana.springcloud.entity.Payment;
import com.banana.springcloud.service.OpenFeignOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author layman
* @date 2021/1/17
*/
@RestController
@Slf4j
@RequestMapping("/consumer/payment")
public class OpenFeignOrderController {
@Resource
private OpenFeignOrderService openFeignOrderService;
@GetMapping("/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
return openFeignOrderService.getPaymentById(id);
}
/**
* openfeign-ribbon 客户端一般默认等待1秒钟
*/
@GetMapping("/openfeign/timeout")
public String TestOpenFeignTimeOut(){
return openFeignOrderService.openFeignTimeOut();
}
}