0
点赞
收藏
分享

微信扫一扫

微服务feign.RetryableException: Load balancer does not contain an instance for the service解决方案


全程只有图,文字很少描述!

遇到的问题~​​feign.RetryableException: connect timed out executing POST http://xxx ........ Load balancer does not contain an instance for the service​

依赖的版本~

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>

环境配置

运行环境--win10 
Ide--->>Idea 2019
springboot 2.6.6
jdk版本 11
测试工具 postman,用浏览器也可以

昨天搜的解决方案~说什么可能是版本的问题,这里我也没有尝试换版本,而是坚持采用最新版本;

晚上睡觉前终于想通了,画图来阐述一下

微服务feign.RetryableException: Load balancer does not contain an instance for the service解决方案_spring cloud

这里解析一下背后的逻辑

微服务feign.RetryableException: Load balancer does not contain an instance for the service解决方案_spring_02


注意的地方----hostname那里写服务提供者的地址,不写那么就要在本机里修改host,考虑到分布式又不是部署在一个机器上,所以最好还是写;

放上部分代码以供参考~
首先是 服务提供者~

package com.gavin.repository.service.impl;

import com.gavin.enity.OrderInfo;
import com.gavin.repository.pojo.wfwOrderInfo;
import com.gavin.repository.wfwOrderInfoRepository;
import com.gavin.repository.service.wfwOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @Description: UP up UP
* @author: Gavin
* @date:2022/4/5 10:31
*/

@Service
public class wfwOrderInfoServiceImpl implements wfwOrderInfoService {

@Autowired
private wfwOrderInfoRepository wfwOrderInfoRepository;

/**
*
* @param orderInfo 从commons传来的参数
* @return
*/
@Override
public int createOrder(OrderInfo orderInfo) {
// 有可能传参的时候出错
if (orderInfo==null){
return -1;
}
wfwOrderInfo wfwOrderInfo = new wfwOrderInfo();
wfwOrderInfo save =null;
try {
wfwOrderInfo.setNumber(orderInfo.getNumber());
wfwOrderInfo.setPrice(orderInfo.getPrice());
//又可能save的时候出错
save= wfwOrderInfoRepository.save(wfwOrderInfo);
}
catch(Exception ex){

System.out.println(ex.getMessage());
return -1;
}
return save.getOid();
}
}

配置文件

server:
port: 8001
spring:
application:
name: gavin-order-client
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://172.23.141.26:3306/gavin
username: gavin
password: 955945
druid:
db-type: mysql
type: com.alibaba.druid.pool.DruidDataSource
eureka:
instance:
hostname: 192.168.135.1 #实例名
client:
service-url:
defaultZone: http://192.168.135.1:7000/eureka #eureka 地址
register-with-eureka: true
fetch-registry: true

远程调用接口~

/**
* @FeignClient(name ="gavin-order-client" )调用的哪一个服务
* <p>
* eureka:
* instance:
* hostname: gavin-order-client #这是服务提供者中被调用的服务名
*/

@Service
@FeignClient(value="gavin-order-client" /*,url = "localhost:8001"*/)
//@LoadBalancerClient(name = "mail-service", configuration = LoadBalancerConfiguration.class)
public interface OrderFeignClient {


/**
* @param orderInfo
* @return
* @PostMapping("/order/add") 是被调用者中控制器的路径
*/
@PostMapping("/order/add")
String add(OrderInfo orderInfo);


// 远程调用
@PostMapping("/order/addOrderInfo")
OrderInfo addOrder(@RequestBody OrderInfo orderInfo);


}

服务调用配置~
这里还配置了负载均衡

server:
port: 8200
spring:
application:
name: gavin-eurake-server

eureka:
instance:
hostname: gavin-eurake-server #实例名
client:
service-url:
defaultZone: http://192.168.135.1:7000/eureka #eureka 地址
register-with-eureka: false
fetch-registry: true
gavin-order-client:
ribbon:

#NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置规则 随机
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置规则 重试
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略
ConnectTimeout: 500 #请求连接超时时间
ReadTimeout: 1000 #请求处理的超时时间
OkToRetryOnAllOperations: true #对所有请求都进行重试
MaxAutoRetriesNextServer: 2 #切换实例的重试次数
MaxAutoRetries: 1 #对当前实例的重试次数


举报

相关推荐

0 条评论