0

I'm creating a load balance feature on my project in which I have three server that will simultaneously ping for 15 seconds. However, when I already run my client-side, it always goes to the fallback page and received an error of "LoadBalancer [server]: Error choosing server for key default" even if the servers are already running.

Here are the codes in my project:

app.properties

server.port=8788

server.ribbon.eureka.enabled=false
server.ribbon.listOfServers=localhost:8787,localhost:8789,localhost:8790

#every 15 seconds
server.ribbon.ServerListRefreshInterval=15000

client service (wherein it is my fallback method)

    private LoadBalancerClient loadBalancer;

    private RestTemplate restTemplate;

    public ClientService(RestTemplate rest) {
        this.restTemplate = rest;
    }

    @HystrixCommand(fallbackMethod = "reliable")
    public String login() {
        ServiceInstance instance = loadBalancer.choose("server");
        URI uri = URI.create(String.format("http://%s:%s/admin/ping", instance.getHost(), instance.getPort()));
        //URI uri = URI.create("http://localhost:8787/admin/ping");
        return this.restTemplate.getForObject(uri, String.class);
    }

MainController

public class MainController{
    private final static Logger LOGGER = LoggerFactory.getLogger(MainController.class);

    @Autowired
    private ClientService clientService;
    @LoadBalanced
    @Bean
    public RestTemplate rest(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Autowired
    RestTemplate restTemplate;

...

Client client = new Client();
            WebResource resource = client.resource("http://%s:%s/auth/loginvalidate");
            ClientResponse response = resource.type(MediaType.APPLICATION_JSON)
                    .header("Authorization", "Basic " + encodePw)
                    .get(ClientResponse.class);

1 Answers1

0

I got rid of that error by doing two things:

1) Add the following properties to the remote service:

  management.endpoints.web.exposure.include: "*"
  management.endpoint.health.enabled: "true"
  management.endpoint.restart.enabled: "true"
  management.endpoint.info.enabled: "true"

2) Make sure that there is a ping endpoint in the remote service:

public class MainController{
    @RequestMapping("/")
    public String ribbonPing() {
        return this.hostName;
    }
}

I added a few amendments to the example provided by Kubernetes Circuit Breaker & Load Balancer Example to test this scenario and put in here.

I suggest that you follow those links as a kind of "best practises" guide in order to build your Hystrix/Ribbon solution. Pay special attention to:

  • the starters/dependencies added to the pom files
  • the structure of the Java classes (how and where each bean is declared and injected)
  • how you configure your (micro-)services (in this case with K8s ConfigMaps)
lubumbax
  • 255
  • 2
  • 9