通过Feign进行调用@FeignClient 找不到的解决方案
作者:人在旅途我渐行渐远 时间:2023-08-18 19:06:52
Feign进行调用@FeignClient 找不到
通过Feign 进行调用
这里配置spring-cloud 版本为 M8的
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
</properties>
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.0.RC1</version>
</dependency>
注意:spring-cloud 版本一定要选好,不然可能会找不到FeignClient,
再网上看到这样一句话:
Spring Cloud对Feign的支持由org.springframework.cloud:spring-cloud-netflix-core
移到org.springframework.cloud:spring-cloud-openfeign-core下,
Finchley.M8版本下的spring-cloud-starter-openfeign:2.0.0.M2的pom依赖文件中导入的是
spring-cloud-netflix-core而非spring-cloud-openfeign-core,
需要我们在pom文件中添加对应依赖管理使spring-cloud-starter-openfeign版本
更新到2.0.0.RC1
那么接下来我们进行代码整理
编写接口
@FeignClient(name="eureka-client-1")
public interface EurekaClient {
@GetMapping("/info")
String getInfo();
}
调用
@GetMapping("getMessage")
public String getMessage(){
String res = eurekaClient.getInfo();
return res;
}
Feign通过服务名调用服务,找不到服务
报错环境
eureka注册中心在远程服务器上
本地服务注册到远程的eureka注册中心
本地服务通过Fegin组件+服务名调用服务
报错时,注册中心的情况
Application Status
SERVICE-HOT 192.168.22.180:8308
SERVICE-REDIS 127.0.0.1:4046
SERVICE-ZUUL 127.0.0.1:4041
报错情况
feign.RetryableException: connect timed out executing GET http://SERVICE-REDIS/redis/NaviKey-24149-1
at feign.FeignException.errorExecuting(FeignException.java:84)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113)
排查后发现:是因为注册服务ip问题,导致本地服务无法通过服务名字找到相应的服务。
远程项目和eureka服务部署在同一台服务器上,所以通过127.0.0.1去服务中心注册服务是没问题的
本地项目通过远程服务器ip来注册服务,也没问题
在同一个注册中心上,可以互相发现服务,就是通过名字可以找到对方,没错
问题在于, 当本地SERVICE-HOT服务在eureka中心找到SERVICE-REDIS服务后,redis服务告诉他,我的服务ip地址是127.0.0.1:4046.那么,这就有问题了!!!
本地服务和远程服务不在一台服务器上,你的127.0.0.1是自己的服务器ip,本地服务肯定找不到
所以,注册服务时,统一都使用服务器ip来注册就行了,修改后的配置中心如下:
Application Status
SERVICE-HOT 192.168.22.180:8308
SERVICE-REDIS 192.168.22.180:4046
SERVICE-ZUUL 192.168.22.180:4041
不修改服务注册配置,还有一种办法:使用fegin时,通过url指定访问服务的ip
@FeignClient(value = "SERVICE-REDIS",
url="http://192.168.22.180:4041/service-redis" ,
fallback = RedisSvcFallback.class)
public interface RedisSvc {
来源:https://blog.csdn.net/jun_1990/article/details/92836738
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android UI控件ExpandableListView基本用法详解
![](https://img.aspxhome.com/file/2023/8/137548_0s.jpg)
Java读写文件,在文件中搜索内容,并输出含有该内容的所有行方式
![](https://img.aspxhome.com/file/2023/7/94207_0s.jpg)
java配置多个过滤器优先级以及几个常用过滤器操作
Android 动画之ScaleAnimation应用详解
![](https://img.aspxhome.com/file/2023/0/122220_0s.png)
100行C#代码实现经典扫雷游戏
![](https://img.aspxhome.com/file/2023/0/70400_0s.png)
unity实现场景切换进度条显示
![](https://img.aspxhome.com/file/2023/5/104305_0s.jpg)
Eclipse快速添加get、set方法的操作技巧
![](https://img.aspxhome.com/file/2023/7/78937_0s.jpg)
简单实现安卓里百度地图持续定位
java解析XML详解
![](https://img.aspxhome.com/file/2023/4/101994_0s.jpg)
超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)
![](https://img.aspxhome.com/file/2023/0/104290_0s.png)
Android编程实现下载时主界面与详细界面一致更新的方法
java后台验证码生成的实现方法
![](https://img.aspxhome.com/file/2023/3/125653_0s.png)
Java 8 lambda表达式引入详解及实例
java中的前++和后++的区别示例代码详解
![](https://img.aspxhome.com/file/2023/8/70358_0s.png)
C#中使用Split方法拆分字符串实例
MyBatis中映射文件的使用案例代码
![](https://img.aspxhome.com/file/2023/0/60910_0s.png)
Spring+MyBatis实现数据读写分离的实例代码
C#泛型实例详解
![](https://img.aspxhome.com/file/2023/1/69231_0s.png)
Android开发入门环境快速搭建实战教程
![](https://img.aspxhome.com/file/2023/3/139473_0s.jpg)
详解Java String字符串获取每一个字符及常用方法
![](https://img.aspxhome.com/file/2023/6/85486_0s.jpg)