2

我用 Ribbon 和 Netflix Eureka 创建了一个简单的项目。尤里卡工作正常,我可以看到所有注册的服务。但是,功能区负载均衡器看不到这些服务。实际上,在日志中它打印出它在“当前服务器列表”中看到了服务器,但是我得到了没有找到实例的异常。我将非常感谢一些提示,我花了很多时间(和几天)来弄清楚。

日志(我用 替换了我的 ip <MY_IP>):

2017-10-14 18:13:18.118  INFO 6417 --- [nio-8080-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : Client:reservationManagement instantiated a LoadBalancer:DynamicServerListLoadBalancer:{NFLoadBalancer:name=reservationManagement,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2017-10-14 18:13:18.123  INFO 6417 --- [nio-8080-exec-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2017-10-14 18:13:18.165  INFO 6417 --- [nio-8080-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: reservationManagement.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2017-10-14 18:13:18.474  INFO 6417 --- [nio-8080-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client reservationManagement initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=reservationManagement,current list of Servers=[<MY_IP>:8082],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;       Instance count:1;       Active connections count: 0;  Circuit breaker tripped count: 0;       Active connections per server: 0.0;]
},Server stats: [[Server:<MY_IP>:8082;    Zone:defaultZone;       Total Requests:0;       Successive connection failure:0;     Total blackout seconds:0;        Last connection made:Thu Jan 01 01:00:00 CET 1970;      First connection made: Thu Jan 01 01:00:00 CET 1970;  Active Connections:0;   total failure count in last (1000) msecs:0;     average resp time:0.0;  90 percentile resp time:0.0; 95 percentile resp time:0.0;     min resp time:0.0;      max resp time:0.0;      stddev resp time:0.0]


2017-10-14 18:13:18.634  WARN 6417 --- [nio-8080-exec-1] com.netflix.loadbalancer.RoundRobinRule  : No up servers available from load balancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=reservationManagement,current list of Servers=[<MY_IP>:8082],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;       Instance count:1;       Active connections count: 0;    Circuit breaker tripped count: 0;     Active connections per server: 0.0;]
},Server stats: [[Server:<MY_IP>:8082;    Zone:defaultZone;       Total Requests:0;       Successive connection failure:0;     Total blackout seconds:0;        Last connection made:Thu Jan 01 01:00:00 CET 1970;      First connection made: Thu Jan 01 01:00:00 CET 1970;  Active Connections:0;   total failure count in last (1000) msecs:0;     average resp time:0.0;  90 percentile resp time:0.0; 95 percentile resp time:0.0;     min resp time:0.0;      max resp time:0.0;      stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@1fd42bb7
2017-10-14 18:13:18.655 ERROR 6417 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for reservationManagement] with root cause

java.lang.IllegalStateException: No instances available for reservationManagement

代码:

package project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import configuration.ReservationConfiguration;

@RestController
@RibbonClient(name = "reservationManagement", configuration = ReservationConfiguration.class)
public class MyController {

    @Autowired
    private Environment env;

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Autowired
    RestTemplate restTemplate;


    @RequestMapping(value = "/info")
    public String home() {
        final String reservationResponse = this.restTemplate.getForObject("http://reservationManagement/info",String.class);
        final String flightResponse =  env.getProperty("spring.application.name") + " port: " + env.getProperty("server.port");
        return flightResponse + " from " + reservationResponse;
    }
}


package configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.AvailabilityFilteringRule;

public class ReservationConfiguration {

  @Autowired
  IClientConfig ribbonClientConfig;

  @Bean
  /*The default IPing is a NoOpPing (which doesn’t actually ping server instances, instead always reporting that they’re stable), */
  public IPing ribbonPing(IClientConfig config) {
    return new PingUrl();
  }

  @Bean
  public IRule ribbonRule(IClientConfig config) {
    return new AvailabilityFilteringRule();
  }

}

应用程序.yml

server:
  port: 8080
eureka.client.fetchRegistry: true
reservationManagement:
  ribbon:
    eureka:
      enabled: true
    ServerListRefreshInterval: 15000

Eureka 服务器位于默认端口上。我错过了什么?

编辑:尤里卡看到这项服务:

屏幕

4

2 回答 2

0

添加上述依赖项时它可以工作:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    <version>1.4.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>ribbon-eureka</artifactId>
    <version>2.2.5</version>
</dependency>
于 2018-05-08T09:53:39.510 回答
0

确保您的应用程序 reservationManagement 在 Eureka 注册表中注册。也使用https://reservationManagement/info

于 2017-10-14T17:50:29.790 回答