Spring Cloud 微服务入门教程(四):微服务间的调用消费-FeignClient
上一节《Spring Cloud 微服务入门教程(三):微服务的注册》我们讲了服务的注册,本节我们讲服务之间的调用,也就是作为服务消费者去消费其他服务,以及使用FeignClient来快速高效的调用其他服务。
新建一个服务消费者模块
新建一个名为「democlient」的模块作为消费者去调用「demoservice」提供的服务,如何新建模块参见上一章节文章,代码也上传到Github了,再次不再赘述,只关注新增的东西。「democlient」要比之前的「demoservice」多一个依赖:spring-cloud-starter-openfeign,POM文件是:
<?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>cloud</artifactId>
<groupId>net.renfei</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>net.renfei</groupId>
<artifactId>democlient</artifactId>
<version>1.0.0</version>
<name>demo-client</name>
<description>演示服务</description>
<dependencies>
<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>net.renfei</groupId>
<artifactId>apicenter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
关于这个Feign
先说咱们使用的OpenFeign,是Spring Cloud 在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等,OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。
Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端,内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。
在「democlient」模块中新建一个包net.renfei.democlient.client包名,然后在包名下新建一个interface,继承统一接口中心的net.renfei.apicenter.service.DemoService,并添加@FeignClient(name = "DemoService")注解,这里的name = "DemoService",就是服务提供者的application.name的应用名称,FeignClient会去注册中心寻找服务提供者,代码:
package net.renfei.democlient.client;
import net.renfei.apicenter.service.DemoService;
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name = "DemoService")
public interface DemoServiceClient extends DemoService {
}
在「democlient」模块中新建一个包net.renfei.demoservice.controller包名,然后在包名下新建一个类:DemoClientController,去消费DemoService提供的服务,代码如下:
package net.renfei.democlient.controller;
import net.renfei.apicenter.request.DemoRquest;
import net.renfei.apicenter.result.Result;
import net.renfei.democlient.client.DemoServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoClientController {
@Autowired
private DemoServiceClient demoServiceClient;
@GetMapping("/")
public String getDemoService() {
DemoRquest demoRquest = new DemoRquest();
demoRquest.setMsg("This DemoRquest.Msg From DemoClientController.");
Result result = demoServiceClient.sayMsg(demoRquest);
return "You're visiting DemoClient. Call DemoService:{" + result.getMessage() + "}";
}
}
测试结果
到这里,一个服务消费者就搭建完成了,下面我们开始测试一下我们的微服务,启动是有顺序的,先启动eureka注册中心,然后启动demoservice服务提供者,最后启动democlient服务消费者,打开注册中心可以看到两个注册上来的服务,我们访问democlient服务消费者的地址,在我的案例中是:http://192.168.8.113:18081,可以看到返回了“You're visiting DemoClient. Call DemoService:{This is DemoService, Your Mag is: This DemoRquest.Msg From DemoClientController.}”,说明我们已经可以把消息传递给demoservice服务提供者,并且从demoservice服务提供者拿到了返回结果。
其他的问题
如果你看到democlient服务消费者有报错“com.netflix.client.ClientException: Load balancer does not have available server for client: DemoService”,那你可以尝试重启一下democlient服务消费者,原因是demoservice服务提供者可能还没注册上去,你就访问了消费者,这样消费者在注册中心找不到提供者,因为注册机制是心跳,有个频率,并不是马上实时的就注册上去了,在我的示例中遇到这个错误,一般是因为服务提供者刚刚启动,还没注册上去,你就马上访问了服务消费者导致的。


商业用途请联系作者获得授权。
版权声明:本文为博主「任霏」原创文章,遵循 CC BY-NC-SA 4.0 版权协议,转载请附上原文出处链接及本声明。
相关推荐
猜你还喜欢这些内容,不妨试试阅读一下评论与留言
以下内容均由网友提交发布,版权与真实性无法查证,请自行辨别。微信订阅号
扫码关注「任霏博客」微信订阅号- 你好,我想问一下如果是分析型的数据库要怎么制作docker镜像呢 是修改V008R003C002B0320版本号吗
- 可以的,我也正在开发分享的程序,可以邮件或群联系我都可以,关于页面里有联系方式:https://www.renfei.net/page/about 。
- 有破解软件的需要可以私下联系您吗?
- 您好,手机APP只是个客户端,用于数据呈现展示,数据均保存在服务器上,只留个APP没有任何用处,无能为力哦。
- 老哥 看你弄了这么多软件好厉害啊。 我有个软件 我买过几个小会员 没用几天 然后商家跑路了,软件服务器关闭了,连不上去 用不了。 你能做成一个打补丁版本可以本地用的么? 方便看下么?https://haodezhe.lanzouw.com/iD0f30h9joza 谢谢老哥!
- 您好,由于版权投诉和我国知识产权法的完善,我已经下架所有破解软件的下载链接了。
- 请问怎么下载呀
- 我保存的License在:https://gitlab.com/renfei/KingbaseES-V8-R3/-/tree/master/License ,开发版是长期有效的,只不过限制连接数,现在官网好像已经下线 V8R3 的下载页面了,其他版本我也不确定是否过期
- 这个版本的license有没有
- 序列号长度不对呀