Spring Cloud Ribbon 抛出名为 No instances available for serverurl 的异常
Spring Cloud Ribbon throws exception called No instances available for serverurl
我正在使用 spring 引导和 spring 功能区。我做了功能区的所有配置。但是当我向 rest 控制器发送请求时,它会抛出一个名为 No instances available for serverurl 的异常。我该如何解决这个问题?
这些是我的配置
application.yml
port: 8888
serverurl:
ribbon:
eureka:
enabled: false
listOfServers: localhost:8081,localhost:8082,localhost:8083
ServerListRefreshInterval: 15000
Spring 引导主程序 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);
}
}
休息控制器
@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
1) 确保 http://localhost:8081/api/v1/clients/1234
(8081/8082/8083) 响应。
2) 添加RibbonConfiguration
文件例如:
@Configuration
public class RibbonConfiguration{
@Bean
public IRule ribbonRule() {
return new BestAvailableRule();
}
@Bean
public IPing ribbonPing() {
return new PingUrl();
}
}
3) 确保你有这种 pom 依赖(例如):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
和
<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>
我正在使用 spring 引导和 spring 功能区。我做了功能区的所有配置。但是当我向 rest 控制器发送请求时,它会抛出一个名为 No instances available for serverurl 的异常。我该如何解决这个问题?
这些是我的配置
application.yml
port: 8888
serverurl:
ribbon:
eureka:
enabled: false
listOfServers: localhost:8081,localhost:8082,localhost:8083
ServerListRefreshInterval: 15000
Spring 引导主程序 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);
}
}
休息控制器
@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
1) 确保 http://localhost:8081/api/v1/clients/1234
(8081/8082/8083) 响应。
2) 添加RibbonConfiguration
文件例如:
@Configuration
public class RibbonConfiguration{
@Bean
public IRule ribbonRule() {
return new BestAvailableRule();
}
@Bean
public IPing ribbonPing() {
return new PingUrl();
}
}
3) 确保你有这种 pom 依赖(例如):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
和
<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>