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

2022-06-25 14:18:53

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

遇到的问题~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,用浏览器也可以

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

晚上睡觉前终于想通了,画图来阐述一下
在这里插入图片描述这里解析一下背后的逻辑在这里插入图片描述
注意的地方----hostname那里写服务提供者的地址,不写那么就要在本机里修改host,考虑到分布式又不是部署在一个机器上,所以最好还是写;

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

packagecom.gavin.repository.service.impl;importcom.gavin.enity.OrderInfo;importcom.gavin.repository.pojo.wfwOrderInfo;importcom.gavin.repository.wfwOrderInfoRepository;importcom.gavin.repository.service.wfwOrderInfoService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;/**
 * @Description: UP up UP
 * @author: Gavin
 * @date:2022/4/5 10:31
 */@Servicepublicclass wfwOrderInfoServiceImplimplements wfwOrderInfoService{@Autowiredprivate wfwOrderInfoRepository wfwOrderInfoRepository;/**
     *
     * @param orderInfo   从commons传来的参数
     * @return
     */@OverridepublicintcreateOrder(OrderInfo orderInfo){//        有可能传参的时候出错if(orderInfo==null){return-1;}
        wfwOrderInfo wfwOrderInfo=newwfwOrderInfo();
        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:8001spring:application:name: gavin-order-clientdatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://172.23.141.26:3306/gavinusername: gavinpassword:955945druid:db-type: mysqltype: com.alibaba.druid.pool.DruidDataSourceeureka:instance:hostname: 192.168.135.1#实例名client:service-url:defaultZone: http://192.168.135.1:7000/eureka#eureka 地址register-with-eureka:truefetch-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)publicinterfaceOrderFeignClient{/**
     * @param orderInfo
     * @return
     * @PostMapping("/order/add") 是被调用者中控制器的路径
     */@PostMapping("/order/add")Stringadd(OrderInfo orderInfo);//    远程调用@PostMapping("/order/addOrderInfo")OrderInfoaddOrder(@RequestBodyOrderInfo orderInfo);}

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

server:port:8200spring:application:name: gavin-eurake-servereureka:instance:hostname: gavin-eurake-server#实例名client:service-url:defaultZone: http://192.168.135.1:7000/eureka#eureka 地址register-with-eureka:falsefetch-registry:truegavin-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#对当前实例的重试次数
  • 作者:CodeMartain
  • 原文链接:https://blog.csdn.net/weixin_54061333/article/details/124012570
    更新时间:2022-06-25 14:18:53