SpringCloud:Feign实现微服务之间相互请求

上篇文章说了通过RestTemplate实现微服务之间访问,这篇文章将通过Feign实现微服务之间访问。
代码基于RestTemplate实现微服务之间访问基础上进行修改。

🍀Feign简介

Github:github.com/OpenFeign/f…

Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地实现微服务之间的调用。
1.Feign可帮助我们更加便捷,优雅的调用HTTP API。
2.在SpringCloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。
3.Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。
4.SpringCloud对Feign进行了增强,使Feign支持了SpringMVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便。

🍀Spring Cloud 组件依赖版本

官网文档:github.com/alibaba/spr…

SpringCloud:Feign实现微服务之间相互请求-每日运维
本文参考使用组件依赖如下
在这里插入图片描述

🍀Feign实现服务之间访问

创建微服务项目,结构如下图所示
在这里插入图片描述

root pom.xml

4.0.0
com.ber
SpringCloud-Feign
1.0-SNAPSHOT
pom
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
8
8
1.8
2.3.12.RELEASE
2.2.8.RELEASE
Hoxton.SR12
org.springframework.cloud
spring-cloud-starter-bootstrap
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibaba.version}
pom
import
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
ber-nacos

ber-nacos pom.xml

SpringCloud-Feign
com.ber
1.0-SNAPSHOT
4.0.0
ber-nacos
pom
nacos-consumer
nacos-provider
nacos-consumer-feign

☘创建nacos-consumer-feign微服务

nacos-consumer-feign pom.xml

ber-nacos
com.ber
1.0-SNAPSHOT
4.0.0
nacos-consumer-feign
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-loadbalancer
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-openfeign

application.properties

spring.application.name=nacos-consumer-feign
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
server.port=8895
feign.hystrix.enabled=true

启动类,注意添加注解@EnableFeignClients、@EnableDiscoveryClient

package com.ber.nacos.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author: ber
* @date: 2022/6/25 0025 22:43
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}

controller接口,通过/queryMsg/{msgStr}访问provider微服务接口

package com.ber.nacos.feign.controller;
import com.ber.nacos.feign.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: ber
* @date: 2022/6/25 0025 22:46
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@RestController
public class MsgController {
@Autowired
MsgService msgService;
/**
* 获取消息
*
* @param msgStr 消息
* @return
*/
@GetMapping("/queryMsg/{msgStr}")
public String getMsg(@PathVariable(value = "msgStr") String msgStr) {
return msgService.getMsg(msgStr);
}
}

创建feign client
FeignClient 默认集成了 Ribbon,使用@FeignClient注解将MsgService 接口作为 FeignClient ,其属性名称与服务名称nacos-provider对应

package com.ber.nacos.feign.service;
import com.ber.nacos.feign.service.fallback.MsgServiceFallback;
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;
/**
* @author: ber
* @date: 2022/6/25 0025 22:54
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@Component
@FeignClient(name = "nacos-provider", fallback = MsgServiceFallback.class)
public interface MsgService {
@GetMapping("/getMsg/{msgStr}")
String getMsg(@PathVariable("msgStr") String msgStr);
}

Feign的Fallback机制,在网络请求时,可能会出现异常请求,如果还想再异常情况下使系统可用,那么就需要容错处理,执行设置的容错业务代码。
当接口请求不成功时,就会调用MsgServiceFallback类这里的实现

package com.ber.nacos.feign.service.fallback;
import com.ber.nacos.feign.service.MsgService;
import org.springframework.stereotype.Component;
/**
* @author: ber
* @date: 2022/6/25 0025 22:58
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@Component
public class MsgServiceFallback implements MsgService {
@Override
public String getMsg(String msgStr) {
return "请求失败";
}
}

☘nacos-provider微服务

代码参考上篇文章《RestTemplate实现微服务之间访问》

在这里插入图片描述

🍀Feign微服务之间访问测试

启动nacos-consumer-feignnacos-provider,访问http://localhost:8848/nacos,使用 nacos/nacos 登陆后,可以发现服务列表中,两个微服务已经注册,如下图所示。
SpringCloud:Feign实现微服务之间相互请求-每日运维
访问http://localhost:8895/queryMsg/hello,即consumer-feign的接口,可以得到如下图所示的界面
在这里插入图片描述

☘Feign容错机制

修改feign client代码如下所示
改为@GetMapping("/getMsg-error/{msgStr}"),即原本的queryMsg会被请求到getMsg-error,而provider并没有提供getMsg-error,此时feign会进入容错机制。

package com.ber.nacos.feign.service;
import com.ber.nacos.feign.service.fallback.MsgServiceFallback;
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;
/**
* @author: ber
* @date: 2022/6/25 0025 22:54
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@Component
@FeignClient(name = "nacos-provider", fallback = MsgServiceFallback.class)
public interface MsgService {
@GetMapping("/getMsg-error/{msgStr}")
String getMsg(@PathVariable("msgStr") String msgStr);
}

再次访问http://localhost:8895/queryMsg/hello,即consumer-feign的接口,可以得到如下图所示的界面
SpringCloud:Feign实现微服务之间相互请求-每日运维
源码已上传github

本文介绍到此,博客仅记录博主学习过程,如存在错误之处,请指教。