配置文件

eureka:
  client:
    register-with-eureka: false # 不注册eureka为客户端
    fetch-registry: false

ribbon:
  eureka:
   enabled: false

local-server.ribbon.listOfServers: http://localhost:8080,http://localhost:8081

  • 禁止注册eureka
  • ribbon负载信息不从eureka读取
  • 配置ribbon service-id

feign

@FeignClient(name = "local-server")
public interface TestClient {
    @GetMapping("/im-account/test")
    JSONObject getIm();
}

  • 通过@FeignClient(name = "local-server")指定使用的service-id

feign请求方法

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    @GetMapping
    public void test() {
        log.info("im-account");
    }
}

测试接口

@RestController
@RequestMapping("/ribbon/test")
@Slf4j
public class RibbonTestController {

    @Autowired
    private TestClient client;
    @GetMapping
    public void test() {
        client.getIm();
    }
}

测试

curl -X GET   http://localhost:8081/im-account/ribbon/test 

负载调用成功

超时

local-server:
  ribbon:
    ConnectTimeout: 500

可通过上述配置,配置服务超时时间

报错

2.xspring boot报错

java.lang.NoClassDefFoundError: Could not initialize class com.netflix.client.config.CommonClientConfigKey

如果需要使用eureka,配置eureka信息即可

手动获取负载

    @Autowired
    private LoadBalancerClient loadBalancer;
    
    ServiceInstance instance = loadBalancer.choose("local-server");
    log.info(instance.getHost() + ":" + instance.getPort());