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 版权协议,转载请附上原文出处链接及本声明。
相关推荐
猜你还喜欢这些内容,不妨试试阅读一下评论与留言
以下内容均由网友提交发布,版权与真实性无法查证,请自行辨别。微信订阅号
扫码关注「任霏博客」微信订阅号- 大佬 引入jar包那里的 driver class 怎么选的?
- 我也遇到了这个问题,已经解决了,在此分享一下 1、宿主机也要创建kingbase的用户和用户组,并且要查看一下用户和用户组的ID(这个很重要) 2、把data目录的用户和用户组设置为kingbase 3、先不要把data路径挂载到宿主机上,这时就可以正常启动,启动后进入容器,查看一下容器内的kingbase的用户和用户组ID是多少,和第一步的ID是否一致,如果ID一致,那正常挂载目录就行;如果ID不一致,那就需要修改Dockerfile文件,在构建镜像时,修改容器内的用户和用户组ID,必须和宿主机的保持一致。然后重新构建镜像,就可以正常挂载宿主机目录了 4、其实直接修改宿主机的用户和用户组ID也是可以的,但是容器内的ID一般是1000,但是宿主机的这个ID很可能已经被占用了,无法修改,就只能修改容器内的ID
- 接口已经允许跨域请求,也就是说你可以在你的页面上调用,获取用户的公网 IP。 如果你还需要其他需求,可以提交 Issue 给我。
- V008R003C002B0320 这个对应的jdbc链接驱动你在哪里找到的?我也遇到了这个问题。
- WARNING: max_connections should be less than orequal than 10 (restricted by license) HINT: the value of max_connect is set 10 WARNING: max_connections should be less than orequal than 10 (restricted by license) HINT: the value of max_connect is set 10 kingbase: superuser_reserved_connections must be less than max_connections 我按照文档修改了以后,不知道如何重启。
- 然后把数字都改成 1 再启动。 如何重新启动?
- ksql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.KINGBASE.54321"
- 进入容器查看一下日志,是不是启动失败了,日志文件在:/opt/kingbase/logfile
- ksql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.KINGBASE.54321"?
- 先通过 docker exec -it 容器名/id /bin/bash 进入容器,然后在容器中使用 ksql 客户端进行连接数据库:/opt/kingbase/Server/bin/ksql -U system test
- 免费.ml域名10年委托合同到期被马里共和国收回域名经营权
- 从极狐Gitlab看各种中间件技术选型
- 时隔十年首次收到 Google AdSense 的付款
- ga域名被加蓬共和国从Freenom公司手中收回域名经营权
- Freenom 被 Meta(Facebook) 起诉导致暂停 .tk/.ga/.ml/.cf/.gq 等新域名注册
- 生花妙笔信手来 – 基于 Amazon SageMaker 使用 Grounded-SAM 加速电商广告素材生成 [1]
- github.renfei.net 不再完整代理 Github 页面改为代理指定文件
- 优雅的源代码管理(三):本地优雅的使用 Git Rebase 变基
- 优雅的源代码管理(二):Git 的工作原理
- 优雅的源代码管理(一):版本控制系统 VCS(Version Control System)与软件配置管理 SCM(Software Configuration Management)