Spring Cloud实战小贴士:Feign的继承特性(伪RPC模式)

通过之前发布的《Spring Cloud构建微服务架构:服务消费者(Feign)》,我们已经学会如何使用Spring MVC的注解来绑定服务接口。我们几乎完全可以从服务提供方的Controller中依靠复制操作,来构建出相应的服务接口客户端,或是通过Swagger生成的API文档来编写出客户端,亦或是通过Swagger的代码生成器来生成客户端绑定。即便如此,有很多的方式来产生Feign的客户端程序,依然有很多开发者热衷于利用公共的依赖接口来连接服务提供者和服务消费者的方式。由此,Feign的继承特性就能很好的派上用处。下面,我们来详细看看如何使用Spring Cloud Feign的继承特性。

动手试一试

接下来的示例将分为三个模块:

  • 服务接口定义模块:通过Spring MVC注解定义抽象的interface服务接口
  • 服务接口实现模块:实现服务接口定义模块的interface,该模块作为服务提供者注册到eureka
  • 服务接口消费模块:服务接口定义模块的客户端实现,该模块通过注册到eureka来消费服务接口

服务接口的定义

  • 创建一个Spring Boot项目:eureka-feign-api,pom.xml的主要内容如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
  • 使用Spring MVC注解来定义服务接口:
public interface HelloService {

@GetMapping("/hello")
String hello(@RequestParam(value = "name") String name);

}
  • 完成了上述构建之后,我们使用mvn install将该模块构建到本地的Maven仓库中。

服务接口的实现

  • 创建一个Spring Boot项目:eureka-feign-client,pom.xml的主要内容如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>com.didispace</groupId>
<artifactId>eureka-feign-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

该模块需要依赖上面定义的eureka-feign-api,将使用上述定义的HelloService接口来实现对应的REST服务。同时依赖Eureka是为了将该服务注册到Eureka上供服务消费者发现。

  • 创建应用主类。使用@EnableDiscoveryClient注解开启服务注册与发现,并实现HelloService接口的REST服务:
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

@RestController
class HelloController implements HelloService {
@Override
public String hello(String name) {
return "hello " + name;
}
}

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}

}
  • 编辑application.properties配置内容:
spring.application.name=eureka-feign-client
server.port=2101

eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/

配置了服务提供者的名称eureka-feign-client,服务提供者的端口号2101,并将该服务注册到我的公益Eureka注册中心上。启动该项目,我们可以通过访问:http://eureka.didispace.com/ ,在该页面中找到它。

服务接口的消费

  • 创建一个Spring Boot项目:eureka-feign-consumer,pom.xml的主要内容如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>com.didispace</groupId>
<artifactId>eureka-feign-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

该模块较服务提供者的依赖增加了Feign的依赖,因为这里将使用Feign来绑定服务接口的客户端。下面我们将使用Feign的继承特性来轻松的构建Feign客户端。

  • 创建应用主类。使用@EnableDiscoveryClient注解开启服务注册与发现,并通过@FeignClient注解来声明服务绑定客户端:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

@FeignClient("eureka-feign-client")
interface HelloServiceClient extends HelloService {
}

@RestController
class TestController {
@Autowired
private HelloServiceClient helloServiceClient;
@GetMapping("/test")
public String test(String name) {
return helloServiceClient.hello(name);
}
}

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}

从上述代码中我们可以看到,利用Feign的继承特性,@FeignClient注解只需要通过声明一个接口来继承在API模块中定义的公共interface就能产生服务接口的Feign客户端了。而@FeignClient中的值需要填写该服务的具体服务名(服务提供者的spring.application.name配置值)。

  • 编辑服务消费者的application.properties配置内容,将服务消费者注册到eureka上来消费服务:
spring.application.name=eureka-feign-consumer
server.port=2102

eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/
  • 启动eureka-feign-consumer之后,我们可以通过访问:http://localhost:2102/test ,来实验eureka-feign-consumereureka-feign-client接口的调用。

本文示例

  • 码云
  • GitHub

程序清单:

  • eureka-feign-api:服务接口定义
  • eureka-feign-client:服务接口实现的提供方
  • eureka-feign-consumer:服务接口的调用方

欢迎使用公益Eureka调试您的Spring Cloud程序:http://eureka.didispace.com/

相关阅读

  • Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)
  • Spring Cloud构建微服务架构:服务消费者(基础)
  • Spring Cloud构建微服务架构:服务消费者(Ribbon)
  • Spring Cloud构建微服务架构:服务消费者(Feign)
  • Spring Cloud构建微服务架构:分布式配置中心
  • Spring Cloud构建微服务架构:服务容错保护(hystrix服务降级)
  • Spring Cloud构建微服务架构:服务容错保护(hystrix依赖隔离)
  • Spring Cloud构建微服务架构:服务容错保护(hystrix断路器)
  • Spring Cloud构建微服务架构:Hystrix监控面板
  • Spring Cloud构建微服务架构:Hystrix监控数据聚合
  • 更多Spring Cloud内容…

money.jpg

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/477529.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

这个北航妹子也太卷了...

大家好&#xff0c;今天介绍一位好朋友&#xff0c;毕业于北航的AI算法小姐姐 rumor。rumor刚毕业时从事量化工作&#xff0c;后来转岗到互联网做算法&#xff0c;斩获了众多大厂offer&#xff0c;下面是她跳槽时整理的算法工程师面试知识点&#xff1a;就拿其中的数据结构节点…

LeetCode 1138. 字母板上的路径

1. 题目 我们从一块字母板上的位置 (0, 0) 出发&#xff0c;该坐标对应的字符为 board[0][0]。 在本题里&#xff0c;字母板为board [“abcde”, “fghij”, “klmno”, “pqrst”, “uvwxy”, “z”]. 我们可以按下面的指令规则行动&#xff1a;如果方格存在&#xff0c;U…

开源开放 | 中药说明书实体识别数据集TCM-NER

OpenKG地址&#xff1a;http://openkg.cn/dataset/tcm-ner阿里云天池&#xff1a;https://tianchi.aliyun.com/dataset/dataDetail?dataId86819开放许可协议&#xff1a;CC BY-SA 4.0 &#xff08;署名相似共享&#xff09;贡献者&#xff1a;阿里云&#xff08;陈漠沙&#x…

【公益】开放一台Eureka注册中心给各位Spring Cloud爱好者

这是一篇博客福利&#xff01; 相信很多关注Spring Cloud的爱好者们&#xff0c;不论是读我的系列文章和书籍还是看其他朋友们写的博客佳文&#xff0c;都不可避免的启动多个项目来体验Spring Cloud带来的整套微服务架构方案。其中&#xff0c;Eureka注册中心几乎是每个试验都必…

首个视觉-语言预训练综述来了!

文 | Feilong Chen等编 | 陈萍源 | 机器之心一文了解视觉 - 语言预训练最新进展和新领域。让机器做出与人类相似的反应一直是 AI 研究不懈追求的目标。为了让机器具有感知和思考的能力&#xff0c;研究人员进行了一系列相关研究&#xff0c;如人脸识别、阅读理解和人机对话&…

LeetCode 1233. 删除子文件夹

1. 题目 你是一位系统管理员&#xff0c;手里有一份文件夹列表 folder&#xff0c;你的任务是要删除该列表中的所有 子文件夹&#xff0c;并以 任意顺序 返回剩下的文件夹。 我们这样定义「子文件夹」&#xff1a; 如果文件夹 folder[i] 位于另一个文件夹 folder[j] 下&…

学术会议 | 欢迎注册参加第21届国际语义网大会ISWC-会议日程发布

ISWC(International Semantic Web Conference)是语义网和知识图谱领域的国际顶级学术会议&#xff0c;2022年10月23-27日&#xff0c;ISWC将通过线上的方式举办&#xff0c;汇聚全世界相关的科研工作者、从业者和领域专家&#xff0c;共同探讨、推进、塑造知识图谱与语义网技术…

Spring Cloud实战小贴士:Zuul统一异常处理(三)【Dalston版】

本篇作为《Spring Cloud微服务实战》一书关于Spring Cloud Zuul网关在Dalston版本对异常处理的补充。没有看过本书的读书也不要紧&#xff0c;可以先阅读我之前的两篇博文&#xff1a;《Spring Cloud实战小贴士&#xff1a;Zuul统一异常处理&#xff08;一&#xff09;》和《Sp…

LeetCode 1288. 删除被覆盖区间(lambda排序)

1. 题目 给你一个区间列表&#xff0c;请你删除列表中被其他区间所覆盖的区间。 只有当 c < a 且 b < d 时&#xff0c;我们才认为区间 [a,b) 被区间 [c,d) 覆盖。 在完成所有删除操作后&#xff0c;请你返回列表中剩余区间的数目。 示例&#xff1a; 输入&#xff1…

共同一作,会被认可吗?

文 | 检索宝源 | 科研大匠在我国&#xff0c;论文署名问题长期以来备受争议。尤其是共同第一作者&#xff0c;因没有统一认定标准&#xff0c;又直接影响硕博毕业、求职、考评的结果&#xff0c;而令人心神不宁。共一指的就是共同第一作者&#xff0c;也可以叫做并列第一作者&a…

Spring Cloud构建微服务架构:Hystrix监控数据聚合【Dalston版】

上一篇我们介绍了使用Hystrix Dashboard来展示Hystrix用于熔断的各项度量指标。通过Hystrix Dashboard&#xff0c;我们可以方便的查看服务实例的综合情况&#xff0c;比如&#xff1a;服务调用次数、服务调用延迟等。但是仅通过Hystrix Dashboard我们只能实现对服务当个实例的…

图谱实战 | 丁香园医疗领域图谱的构建与应用

分享嘉宾&#xff1a;付子玉 观澜网络 算法工程师编辑整理&#xff1a;孙佩霞 中国电信研究院出品平台&#xff1a;DataFunTalk导读&#xff1a;丁香园大数据组旨在为用户提供更优质的内容与服务&#xff0c;使用知识/概念图谱、预训练模型挖掘更深层次的用户意图。本文介绍了丁…

LeetCode 722. CPP删除注释(逻辑题)

1. 题目 给一个 C 程序&#xff0c;删除程序中的注释。这个程序source是一个数组&#xff0c;其中source[i]表示第i行源码。 这表示每行源码由\n分隔。 在 C 中有两种注释风格&#xff0c;行内注释和块注释。 字符串// 表示行注释&#xff0c;表示//和其右侧的其余字符应该被…

CVPR 2022 | 天大本科生论文入选!深度学习长尾分类新SOTA

文 | 丰色 发自 凹非寺 源 | 量子位&#xff08;QbitAI&#xff09;本科生搞科研到底能做出什么成绩&#xff1f;最新被CVPR 2022收录的一篇论文中&#xff0c;提供了一种新思路来解决深度学习中长尾分布数据的分类问题&#xff0c;最终实现了新SOTA。论文&#xff1a;https://…

Spring Cloud构建微服务架构:Hystrix监控面板【Dalston版】

在上一篇《服务容错保护&#xff08;hystrix断路器&#xff09;》的介绍中&#xff0c;我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的。而这些请求情况的指标信息都是HystrixCommand和HystrixObservableCommand实例在执行过程中记录的重要度…

学术会议 | 欢迎注册参加第11届国际知识图谱联合会议

11th International Joint Conference On Knowledge Graphs(IJCKG 2022)IJCKG(International Joint Conference On Knowledge Graphs)会议之前是国际语义技术联合会议&#xff08;the Joint International Semantic Technology Conference (JIST)&#xff09;&#xff0c;JIST …

LeetCode 396. 旋转函数(数学)

1. 题目 给定一个长度为 n 的整数数组 A 。 假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组&#xff0c;我们定义 A 的“旋转函数” F 为&#xff1a; F(k) 0 * Bk[0] 1 * Bk[1] ... (n-1) * Bk[n-1]。计算F(0), F(1), …, F(n-1)中的最大值。 注意: 可以认为 n 的值小…

敬你一杯调参人生

文 | 不会的选C源 | LA PAUSE乐泊一杯上好的手冲咖啡也是作品&#xff0c;就像算法工程师调参已久的模型&#xff0c;建筑师终于定稿的一份设计&#xff0c;一首制作人编排已久的歌曲。从第一次喝到风味开始喝咖啡的人很多&#xff0c;有的喝速溶&#xff0c;有的追星巴克新品&…

Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)【Dalston版】

前言 在前两篇《Spring Cloud构建微服务架构&#xff1a;服务容错保护&#xff08;Hystrix服务降级&#xff09;》和《Spring Cloud构建微服务架构&#xff1a;服务容错保护&#xff08;Hystrix依赖隔离&#xff09;》中&#xff0c;我们对Hystrix提供的服务降级和依赖隔离有了…

LeetCode 934. 最短的桥(2次BFS)

1. 题目 在给定的二维二进制数组 A 中&#xff0c;存在两座岛。&#xff08;岛是由四面相连的 1 形成的一个最大组。&#xff09; 现在&#xff0c;我们可以将 0 变为 1&#xff0c;以使两座岛连接起来&#xff0c;变成一座岛。 返回必须翻转的 0 的最小数目。&#xff08;可…