Reputation: 70
I am using spring boot with spring could ribbon. I did all configuration of ribbon. But when I send a request to rest controller, it throws an exception called No instances available for serverurl. How can I fix this?
These are my configurations
application.yml
port: 8888
serverurl:
ribbon:
eureka:
enabled: false
listOfServers: localhost:8081,localhost:8082,localhost:8083
ServerListRefreshInterval: 15000
Spring Boot Main Class
@SpringBootApplication
@RibbonClient(name = "serverurl", configuration = RibbonCongisuration.class)
public class Server {
@LoadBalanced
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(Server.class,args);
}
}
Rest Controller
@RestController
@RequestMapping(value = "api/v1/clients")
public class ClientController {
@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/{ID}")
public ClientDTO findByID(@PathVariable("ID") String clientID){
return restTemplate.getForEntity("http://serverurl/api/v1/clients/"+clientID,ClientDTO.class).getBody();
}
}
URL
http://localhost:8888/api/v1/clients/1234
Upvotes: 1
Views: 632
Reputation: 614
1) Make sure http://localhost:8081/api/v1/clients/1234
(8081/8082/8083) response.
2) Add RibbonConfiguration
file for example:
@Configuration
public class RibbonConfiguration{
@Bean
public IRule ribbonRule() {
return new BestAvailableRule();
}
@Bean
public IPing ribbonPing() {
return new PingUrl();
}
}
3) Make sure you have this kind of pom dependency (for example):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
And
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
Upvotes: 1