我正在使用带弹簧的弹簧靴。我做了所有配置的功能区。但是当我向休息控制器发送请求时,它会抛出一个名为No instance for serverurl的异常。我怎样才能解决这个问题?
这些是我的配置
application.yml
port: 8888
serverurl:
ribbon:
eureka:
enabled: false
listOfServers: localhost:8081,localhost:8082,localhost:8083
ServerListRefreshInterval: 15000
Spring Boot主类
@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();
}
}
网址
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>