什么是Feign
Feign是一个声明式WebService客户端,使用Feign能让编写WebService客户端变得简单。
使用方法
定义一个服务接口然后在上面添加注解。Feign也支持可拔插式的编码器和解码器。SpringCloud对Feign进行啦封装,使其支持SpringMVC标准注解和HttpMessageConverters。Feign可以与eureka和Ribbon组合使用支持负载均衡。
实际应用
在实际开发应用中,由于对服务的依赖调用不止一处,往往一个接口会被多出调用,所以通常都会争对每一个微服务自行封装来包装这些依赖的调用。所以Feign在此基础上做了进一步封装由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需要创建一个接口并使用注解方式来配置。即可完成对服务提供方的接口绑定,简化了SpringCloud Ribbon时,自动化封装了客户端的并发量。
代码实现
目录结构
8001服务提供接口
package ink.awz.springcloud.controller;
import ink.awz.springcloud.entity.JsonResult;
import ink.awz.springcloud.entity.Payment;
import ink.awz.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author 杨杰
* @version 1.0
* @date 2022/7/17 20:17
*/
@RestController
@Slf4j
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@Autowired
private DiscoveryClient discoveryClient;
@PostMapping(value = "/payment/create")
public JsonResult create(@RequestBody Payment payment) {
int resultNumber = paymentService.create(payment);
if (resultNumber > 0) {
return new JsonResult("200","插入成功,port"+serverPort);
} else {
return new JsonResult("444","插入失败,port"+serverPort);
}
}
@GetMapping(value = "/payment/get/{id}")
public JsonResult getPaymentById(@PathVariable Long id) {
return new JsonResult(paymentService.getPaymentById(id),"200","查询成功,port"+serverPort);
}
@GetMapping("/payment/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
for (String element : services) {
log.info("****element"+element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId()+"\t"+instance.getPort()+"\t"+instance.getHost()+"\t"+instance.getUri());
}
return this.discoveryClient;
}
}
Feign客户端
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value = "/payment/get/{id}")
public JsonResult getPaymentById(@PathVariable("id") Long id) ;
}
说明@FeignClient(value = “CLOUD-PAYMENT-SERVICE”)
绑定CLOUD-PAYMENT-SERVICE服务
@GetMapping(value = "/payment/get/{id}")
public JsonResult getPaymentById(@PathVariable("id") Long id)
绑定具体对应提供方接口
CLOUD-PAYMENT-SERVICE 服务名称
Feign客户端Controller层调用
@RestController
@Slf4j
public class OrderFeignController {
@Autowired
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/get/{id}")
public JsonResult getPaymentById(@PathVariable("id") Long id) {
return new JsonResult<>(paymentFeignService.getPaymentById(id),"200","数据查询成功");
}
}
Feign超时控制
在实际业务中有可能提供方做复杂查询,需要花费几秒钟时间,而Feign客户端默认连接提供方的时间为1秒,否则就会抛出异常。可以通过配置文件设置连接时间
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
logging:
level:
ink.awz.springcloud.service.PaymentFeignService: debug
为什么是Ribbon,因为Feign底层使用啦Ribbon做负载均衡,可以通过配置Ribbon连接时间来控制Feign的连接时间。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/133773.html